This example demonstrates file processing capabilities in WebFiori CLI, showcasing text file operations, statistics calculation, and content manipulation.
- Reading and processing text files
- File content analysis and statistics
- Text transformation operations
- Error handling for file operations
- Command argument validation
- File existence and accessibility checks
app.php- Main file processing command implementationsample.txt- Sample text file for testingREADME.md- This documentation
# Process sample file with default action (count)
php app.php process-file --file=sample.txt
# Show help
php app.php help --command=process-file# Count lines, words, and characters
php app.php process-file --file=sample.txt --action=count
# Default action is count (can be omitted)
php app.php process-file --file=sample.txt# Convert to uppercase
php app.php process-file --file=sample.txt --action=uppercase
# Reverse line order
php app.php process-file --file=sample.txt --action=reversecount- Display file statistics (lines, words, characters) - Defaultuppercase- Convert all text to uppercasereverse- Reverse the order of lines in the file
--file- Path to the file to process (Required)--action- Action to perform (optional, defaults tocount)
- File path is required
- File must exist and be readable
- Action must be one of: count, uppercase, reverse
- Invalid actions show available options
php app.php process-file --file=sample.txt --action=countFile Statistics for: sample.txt
Lines: 5
Words: 14
Characters: 82
php app.php process-file --file=sample.txt --action=uppercaseUppercase content:
HELLO WORLD
THIS IS A SAMPLE FILE
FOR TESTING FILE PROCESSING
WITH MULTIPLE LINES
php app.php process-file --file=sample.txt --action=reverseReversed content:
With multiple lines
For testing file processing
This is a sample file
Hello World
php app.php process-file --file=nonexistent.txt --action=countError: File not found: nonexistent.txt
php app.php process-file --action=countError: The following required argument(s) are missing: '--file'
php app.php process-file --file=sample.txt --action=invalidError: The following argument(s) have invalid values: '--action'
Info: Allowed values for the argument '--action':
count
uppercase
reverse
# Test all actions on sample file
php app.php process-file --file=sample.txt --action=count
php app.php process-file --file=sample.txt --action=uppercase
php app.php process-file --file=sample.txt --action=reverse# Create test files
echo -e "Line 1\nLine 2\nLine 3" > test1.txt
echo "Single line file" > test2.txt
touch empty.txt
# Test with different content
php app.php process-file --file=test1.txt --action=count
php app.php process-file --file=test2.txt --action=count
php app.php process-file --file=empty.txt --action=count# Create large file
for i in {1..100}; do echo "Line $i with some content"; done > large.txt
# Process large file
php app.php process-file --file=large.txt --action=count# Test error handling
php app.php process-file --file=nonexistent.txt --action=count
php app.php process-file --action=count
php app.php process-file --file=sample.txt --action=invalid# Test with special files
echo " " > spaces.txt # Only spaces
echo -e "\x00\x01\x02" > binary.txt # Binary content
mkdir testdir # Directory instead of file
php app.php process-file --file=spaces.txt --action=count
php app.php process-file --file=binary.txt --action=count
php app.php process-file --file=testdir --action=count # Shows warning- File Reading: Safe file content reading with error handling
- File Validation: Check file existence and accessibility
- Content Processing: Line-by-line and full content processing
- Statistics Calculation: Lines, words, and character counting
- Case Conversion: Transform text to uppercase
- Line Manipulation: Reverse line order in files
- Content Analysis: Word and character counting
- Encoding Handling: Process various text encodings
- File Not Found: Clear error messages for missing files
- Invalid Arguments: Validation with helpful suggestions
- Required Parameters: Check for mandatory arguments
- File Access Issues: Handle permission and directory errors
- Clear Output: Well-formatted results with labels
- Help Integration: Built-in help command support
- Validation Messages: Helpful error messages with suggestions
- Default Values: Sensible defaults for optional parameters
class FileProcessCommand extends Command {
public function __construct() {
parent::__construct('process-file', [
'--file' => [
ArgumentOption::OPTIONAL => false,
ArgumentOption::DESCRIPTION => 'Path to the file to process'
],
'--action' => [
ArgumentOption::OPTIONAL => true,
ArgumentOption::DEFAULT => 'count',
ArgumentOption::VALUES => ['count', 'uppercase', 'reverse'],
ArgumentOption::DESCRIPTION => 'Action to perform'
]
], 'Process text files in various ways');
}
}validateFile(): Check file existence and readabilitycountStatistics(): Calculate lines, words, characterstransformContent(): Apply text transformationshandleErrors(): Provide meaningful error messages
- Lines: Count newline characters + 1
- Words: Split by whitespace and count non-empty elements
- Characters: Total byte count including whitespace and newlines
- Required parameter checking
- File existence validation
- Action value validation with allowed options
- Clear error messages for invalid input
- Safe file reading with error checking
- Proper handling of empty files
- Binary file detection and handling
- Directory vs file differentiation
- Consistent output formatting
- Helpful error messages
- Default parameter values
- Comprehensive help documentation
- Graceful handling of missing files
- Clear validation error messages
- Suggestions for valid parameter values
- Non-zero exit codes for errors
- 03-user-input - Input validation and handling
- 04-output-formatting - Text formatting and colors
- 07-progress-bars - Progress tracking for file operations
- 10-multi-command-app - Complete CLI applications
- 01-basic-hello-world - Basic command structure
- 02-arguments-and-options - File path arguments and validation
- 07-progress-bars - Visual progress for file operations
- 06-table-display - Display file data in formatted tables
- 04-output-formatting - Formatted status messages
- 03-user-input - Interactive file selection
- 05-interactive-commands - File operation menus
- 11-masked-input - Secure file path input
- 10-multi-command-app - Full applications with file management
- 09-database-ops - Database import/export from files
- 12-command-scaffolding - Generate file processing commands