Command-Line Argument Parser with structopt
This challenge focuses on using the structopt crate in Rust to create a robust and user-friendly command-line interface (CLI) for a simple application. structopt simplifies the process of parsing command-line arguments by deriving parsing logic directly from struct fields, reducing boilerplate and improving code readability. Your task is to build a CLI tool that takes input file paths, an optional output file path, and a verbosity level, then prints a message indicating the operation.
Problem Description
You need to implement a Rust program that uses structopt to parse command-line arguments and perform a simple action based on those arguments. The program should accept the following arguments:
--input <file>(required): The path to the input file.--output <file>(optional): The path to the output file. If not provided, the program should print a message indicating that no output file was specified.--verbose <level>(optional): A verbosity level (0, 1, or 2). Higher levels produce more detailed output. If not provided, the default verbosity level is 0.
The program should then print a message to the console that includes the input file path, the output file path (if provided), and the verbosity level.
Key Requirements:
- Use the
structoptcrate for argument parsing. - The
--inputargument is mandatory. - The
--outputargument is optional. - The
--verboseargument is optional and must be one of 0, 1, or 2. - Handle invalid verbosity levels gracefully (e.g., print an error message and exit).
- Provide helpful usage information if the program is called with incorrect arguments.
Expected Behavior:
The program should parse the command-line arguments and print a message in the following format:
Processing input file: <input_file_path>
Output file: <output_file_path> (or "Not specified")
Verbosity level: <verbosity_level>
If the --output argument is not provided, the "Output file" line should read "Output file: Not specified". If an invalid verbosity level is provided, the program should print an error message to stderr and exit with a non-zero exit code.
Examples
Example 1:
Input: my_program --input input.txt --output output.txt --verbose 1
Output:
Processing input file: input.txt
Output file: output.txt
Verbosity level: 1
Example 2:
Input: my_program --input input.txt --verbose 2
Output:
Processing input file: input.txt
Output file: Not specified
Verbosity level: 2
Example 3:
Input: my_program --input input.txt --verbose 3
Output:
Error: Invalid verbosity level. Must be 0, 1, or 2.
Example 4:
Input: my_program --output output.txt --verbose 1
Output:
Error: Missing required argument: --input <file>
USAGE:
my_program --input <file> [OPTIONS]
OPTIONS:
-i, --input <file> The path to the input file
-o, --output <file> The path to the output file
-v, --verbose <level> Verbosity level [default: 0]
Constraints
- The program must compile and run without errors.
- The program must correctly parse all valid command-line arguments.
- The program must handle invalid verbosity levels and missing required arguments gracefully.
- The program should use
structopteffectively to minimize boilerplate code. - The program should exit with a non-zero exit code if an error occurs.
Notes
- You'll need to add
structoptas a dependency in yourCargo.tomlfile. - Consider using
Resultto handle potential errors during argument parsing. - The
clapcrate (whichstructoptbuilds upon) provides extensive documentation and examples. Refer to it if you need more information. - Focus on clear and concise code that is easy to understand and maintain. Error handling is important.
- The specific action performed on the input file is not important for this challenge; the focus is on argument parsing.