Hone logo
Hone
Problems

Go CLI Argument Parser

This challenge will guide you through creating a robust command-line argument parser in Go. Understanding how to handle command-line arguments is fundamental for building user-friendly and flexible command-line applications. You'll learn to process various types of arguments and provide helpful feedback to users.

Problem Description

Your task is to implement a Go program that accepts and processes command-line arguments. This program should be able to:

  • Parse named arguments: These are arguments preceded by flags like --name or -n.
  • Parse positional arguments: These are arguments that appear in a specific order without flags.
  • Handle different data types: Support strings, integers, and booleans.
  • Provide default values: Allow for default values for named arguments if they are not provided.
  • Display help messages: Show a usage message when the -h or --help flag is provided, or when required arguments are missing.
  • Validate arguments: Implement basic validation for integer arguments.

Expected behavior:

  • The program should gracefully handle missing required arguments by displaying a help message.
  • Boolean flags should be treated as true if present and false if absent (unless explicitly set).
  • Integer arguments should be validated to ensure they are indeed integers and within a reasonable range (e.g., non-negative).

Examples

Example 1:

Input Command: go run main.go --name "Alice" --age 30 --verbose
Output:
Hello, Alice! You are 30 years old.
Verbose mode is enabled.

Explanation: The program correctly parses the named arguments --name (string), --age (integer), and --verbose (boolean).

Example 2:

Input Command: go run main.go -n Bob -a 25
Output:
Hello, Bob! You are 25 years old.
Verbose mode is disabled.

Explanation: The program handles short flags (-n, -a) and assumes verbose mode is off if the flag is not present.

Example 3: Missing Required Argument

Input Command: go run main.go --age 40
Output:
Error: Missing required argument: --name
Usage:
  your_program [flags] <positional_arg1> <positional_arg2>

Flags:
  -n, --name string    Your name (required)
  -a, --age int        Your age (non-negative)
  -v, --verbose        Enable verbose output

Explanation: Since the required --name argument is missing, the program displays an error and the usage message.

Example 4: Invalid Integer Argument

Input Command: go run main.go --name "Charlie" --age thirty
Output:
Error: Invalid integer value for --age: "thirty"
Usage:
  your_program [flags] <positional_arg1> <positional_arg2>

Flags:
  -n, --name string    Your name (required)
  -a, --age int        Your age (non-negative)
  -v, --verbose        Enable verbose output

Explanation: The --age argument is not a valid integer, triggering an error and the usage message.

Example 5: Help Flag

Input Command: go run main.go -h
Output:
Usage:
  your_program [flags] <positional_arg1> <positional_arg2>

Flags:
  -n, --name string    Your name (required)
  -a, --age int        Your age (non-negative)
  -v, --verbose        Enable verbose output

Explanation: The -h flag triggers the display of the usage information.

Constraints

  • Your solution should be written entirely in Go.
  • You are encouraged to use the standard library's flag package for parsing.
  • The program should handle at least one required named argument (--name).
  • It should handle at least one optional named argument with a default value (--age, default 0).
  • It should handle at least one optional boolean flag (--verbose).
  • Integer arguments must be non-negative.
  • The program should exit with a non-zero status code on errors.

Notes

  • Consider how you will define and associate your arguments with variables in your Go program.
  • The flag package provides mechanisms for defining flags, setting default values, and handling argument types.
  • Think about how to display the usage message clearly, including which arguments are required and their types.
  • Error handling is crucial for a good user experience. Provide informative messages to the user when something goes wrong.
  • You might want to define your own Usage function to customize the help message displayed by the flag package.
Loading editor...
go