Building a Versatile File Processing CLI with Argparse
This challenge focuses on creating a command-line interface (CLI) application in Python that can perform basic operations on text files. You will use the argparse module to handle user inputs, making your script flexible and user-friendly. This skill is fundamental for automating tasks and building robust command-line tools.
Problem Description
Your task is to create a Python script that acts as a CLI tool for processing text files. The script should accept a filename as a required argument and then process the file based on optional arguments provided by the user.
Key Requirements:
- Required Argument: The script must accept a single, mandatory argument: the path to the input text file.
- Optional Arguments:
--countor-c: If present, the script should print the total number of lines in the file.--wordsor-w: If present, the script should print the total number of words in the file. (A word is defined as a sequence of non-whitespace characters.)--charsor-l: If present, the script should print the total number of characters in the file.
- Combined Operations: The user should be able to combine multiple optional arguments. For example, they might request both line count and word count.
- Default Behavior: If no optional arguments (
--count,--words,--chars) are provided, the script should print the entire content of the file to standard output. - Error Handling:
- If the specified input file does not exist, the script should print an informative error message and exit gracefully.
- If an invalid argument is provided (e.g.,
--unknown-option),argparseshould handle this by default.
Examples
Example 1:
Input:
python my_cli.py sample.txt
sample.txt content:
Hello world!
This is a test file.
Output:
Hello world!
This is a test file.
Explanation: No optional arguments were provided, so the script prints the entire content of sample.txt.
Example 2:
Input:
python my_cli.py sample.txt -c
sample.txt content:
Hello world!
This is a test file.
Output:
2
Explanation: The --count (or -c) argument was provided, so the script prints the number of lines in the file.
Example 3:
Input:
python my_cli.py sample.txt -c -w
sample.txt content:
Hello world!
This is a test file.
Output:
Lines: 2
Words: 8
Explanation: Both --count (-c) and --words (-w) arguments were provided. The output clearly labels each count.
Example 4:
Input:
python my_cli.py non_existent_file.txt -l
Output:
Error: File 'non_existent_file.txt' not found.
Explanation: The specified file does not exist, so an error message is printed, and the script exits.
Constraints
- The input filename will be a string representing a valid or invalid file path.
- The file contents will be standard UTF-8 encoded text.
- The number of lines, words, and characters will fit within standard integer types.
- The script should be written entirely in Python 3.
- The
argparsemodule must be used for CLI argument parsing.
Notes
- Consider how you will handle line breaks when counting characters and words. A common approach is to count newline characters (
\n) as part of the character count and to split lines by whitespace for word counting. - When multiple optional arguments are present, you might want to format the output clearly, perhaps labeling each piece of information as shown in Example 3.
- The
argparsemodule provides helpful built-in error messages for invalid arguments, but you'll need to implement custom logic for file-not-found errors. - Think about how to efficiently read the file, especially for larger files, though performance is not a primary constraint here.