Hone logo
Hone
Problems

Command-Line Interface with argparse

This challenge focuses on building a simple command-line interface (CLI) using Python's argparse module. CLIs are essential for creating user-friendly tools that can be easily interacted with from the terminal, allowing for automation and scripting. Your task is to create a CLI that accepts various arguments, performs a basic operation based on those arguments, and provides helpful usage information.

Problem Description

You need to create a Python script that utilizes the argparse module to define and parse command-line arguments. The CLI should accept the following arguments:

  • --input_file or -i: Specifies the path to an input text file. This argument is required.
  • --output_file or -o: Specifies the path to an output text file. If not provided, the output should be printed to the console. This argument is optional.
  • --uppercase or -u: A boolean flag. If present, the content of the input file should be converted to uppercase before being written to the output file or printed. Defaults to False.
  • --line_count or -l: An integer representing the number of lines to read from the input file. If not provided, the entire file should be processed. Must be a positive integer.

The script should read the content of the specified input file, optionally convert it to uppercase, and then either write the processed content to the specified output file or print it to the console. If an error occurs (e.g., file not found, invalid line count), the script should print an informative error message to the console and exit with a non-zero exit code.

Examples

Example 1:

Input: python your_script.py -i input.txt -o output.txt
Input File (input.txt):
This is a test file.
It has multiple lines.

Output File (output.txt):
This is a test file.
It has multiple lines.

Explanation: The script reads input.txt, performs no transformations (uppercase is not specified), and writes the content to output.txt.

Example 2:

Input: python your_script.py -i input.txt -u
Input File (input.txt):
This is a test file.
It has multiple lines.

Output:
THIS IS A TEST FILE.
IT HAS MULTIPLE LINES.

Explanation: The script reads input.txt, converts the content to uppercase, and prints it to the console because no output file is specified.

Example 3:

Input: python your_script.py -i input.txt -l 1
Input File (input.txt):
This is a test file.
It has multiple lines.

Output:
This is a test file.

Explanation: The script reads the first line of input.txt and prints it to the console.

Example 4:

Input: python your_script.py -i non_existent_file.txt
Output:
error: the following arguments are required: -i/--input_file

Explanation: The script exits with an error message because the input file does not exist and the required argument is missing.

Example 5:

Input: python your_script.py -i input.txt -l -1
Output:
error: argument -l/--line_count: invalid int value: -1

Explanation: The script exits with an error message because the line count is invalid.

Constraints

  • The input file will contain plain text.
  • The output file, if specified, should be overwritten if it already exists.
  • The line_count argument must be a positive integer.
  • The script should handle file I/O errors gracefully.
  • The script should exit with a non-zero exit code if an error occurs.
  • The script should provide a helpful usage message when invoked with -h or --help.

Notes

  • Consider using try...except blocks to handle potential file I/O errors.
  • argparse provides various features for defining argument types, default values, and help messages. Utilize these features to create a robust and user-friendly CLI.
  • Think about how to handle the case where the input file is empty.
  • The script should be well-documented with comments explaining the purpose of each section of the code.
  • Focus on creating a clean, readable, and maintainable solution.
Loading editor...
python