Hone logo
Hone
Problems

Rust Command Line Argument Parser

Command-line interfaces (CLIs) are a fundamental part of many software applications, allowing users to interact with programs and configure their behavior. This challenge will guide you through building a robust command-line argument parser in Rust. You'll learn how to handle various argument types, provide help messages, and ensure a user-friendly experience.

Problem Description

Your task is to create a Rust program that accepts and processes command-line arguments. The program should be able to:

  • Parse positional arguments: These are arguments that appear in a specific order.
  • Parse optional arguments (flags): These are arguments that may or may not be present, often preceded by hyphens (e.g., -v for verbose).
  • Parse arguments with values: These arguments are followed by a value (e.g., --output result.txt).
  • Provide a help message: If the user requests help (e.g., using -h or --help), the program should display a usage guide.
  • Handle invalid arguments: The program should gracefully report errors if invalid or missing arguments are provided.

You should aim to create a reusable component or structure that can be extended to handle more complex argument scenarios.

Examples

Example 1: Basic Usage

cargo run -- --name Alice --age 30
Hello, Alice! You are 30 years old.

Explanation: The program receives two named arguments: --name with the value "Alice" and --age with the value "30". It then constructs and prints a greeting.

Example 2: Missing Optional Argument

cargo run -- --name Bob
Hello, Bob! You are not specifying your age.

Explanation: The --age argument is optional. Since it's not provided, the program prints a default message.

Example 3: Help Message

cargo run -- -h
Usage: my_cli <name> [options]

Arguments:
  <name>      The name of the person.

Options:
  -h, --help  Print help information
  --name <name>  The name of the person.
  --age <age>   The age of the person.

Explanation: When the user requests help, a detailed usage message is displayed, outlining the available arguments and options.

Example 4: Invalid Argument

cargo run -- --greeting "Hi" --name Charlie
Error: Unknown argument '--greeting'

Explanation: The --greeting argument is not recognized by the parser, so an error message is printed.

Constraints

  • The program must be written entirely in Rust.
  • You should use the standard library for basic argument retrieval (std::env::args).
  • You are encouraged to explore popular third-party crates like clap or structopt for more advanced parsing, but the core challenge can be approached with standard library tools.
  • The help message should be informative and clearly list all available arguments and their descriptions.
  • Error messages for invalid or missing arguments should be clear and actionable.

Notes

Consider how you will represent the parsed arguments within your program. Structs or enums can be very helpful here. Think about how to manage different argument types (strings, integers, booleans for flags). The standard library's std::env::args provides an iterator over the arguments, but it's up to you to parse them into meaningful data.

Loading editor...
rust