Hone logo
Hone
Problems

Command Line Argument Parser in Rust

This challenge asks you to build a simple command-line argument parser in Rust. Command-line argument parsing is a fundamental skill for any developer, allowing programs to be configured and controlled through arguments passed when the program is executed. This exercise will solidify your understanding of Rust's std::env module and error handling.

Problem Description

You are tasked with creating a Rust program that accepts command-line arguments and processes them. The program should accept the following arguments:

  • -n or --name: Specifies a name to greet. This argument requires a value (e.g., -n Alice).
  • -g or --greeting: Specifies a custom greeting message. This argument requires a value (e.g., -g Hello).
  • -v or --verbose: A flag that, when present, enables verbose output. This argument does not require a value.

The program should:

  1. Parse the command-line arguments.
  2. If the -n or --name argument is provided, greet the specified name. If not, greet "World".
  3. If the -g or --greeting argument is provided, use the specified greeting. If not, use "Hello".
  4. If the -v or --verbose flag is present, print a verbose message indicating the arguments used.
  5. Handle invalid arguments gracefully, printing an error message and exiting with a non-zero exit code.

Expected Behavior:

The program should print a greeting to the console based on the provided arguments. If an invalid argument is provided, it should print an error message and exit.

Edge Cases to Consider:

  • No arguments provided.
  • Multiple occurrences of the same argument (e.g., -n Alice -n Bob). The last occurrence should take precedence.
  • Invalid argument names.
  • Missing values for arguments that require them (e.g., -n without a name).
  • Arguments in different orders (e.g., -g Hello -n Alice).

Examples

Example 1:

Input: []
Output: Hello World
Explanation: No arguments are provided, so the default name "World" and greeting "Hello" are used.

Example 2:

Input: [-n Alice]
Output: Hello Alice
Explanation: The name "Alice" is provided, so it's used in the greeting.

Example 3:

Input: [-g Good morning -n Bob]
Output: Good morning Bob
Explanation: The greeting "Good morning" and name "Bob" are provided, so they are used in the greeting.

Example 4:

Input: [-v -n Charlie -g Hi]
Output: Verbose mode enabled. Name: Charlie, Greeting: Hi
Hi Charlie
Explanation: The verbose flag is set, so a verbose message is printed. Then, the greeting "Hi Charlie" is printed.

Example 5:

Input: [-x invalid_argument]
Output: Error: Invalid argument: -x
Explanation: An invalid argument "-x" is provided, so an error message is printed.

Constraints

  • The program must be written in Rust.
  • The program should handle errors gracefully and exit with a non-zero exit code (e.g., 1) when an error occurs.
  • The program should be reasonably efficient in parsing the arguments. While performance isn't the primary focus, avoid excessively complex or inefficient algorithms.
  • The program should be well-structured and readable.

Notes

  • The std::env::args() function can be used to access the command-line arguments.
  • Consider using a library like clap for more advanced argument parsing if you want to explore that option, but for this exercise, implementing the parsing logic manually is preferred.
  • Think about how to handle the different argument types (flag vs. argument with value).
  • Error handling is crucial. Provide informative error messages to the user.
  • Focus on clarity and correctness over extreme optimization.
Loading editor...
rust