Hone logo
Hone
Problems

Command-Line Argument Parser in Go

This challenge focuses on building a simple command-line argument parser in Go. Command-line arguments are essential for making programs flexible and configurable, allowing users to control behavior without modifying the code directly. Your task is to create a program that accepts and processes command-line arguments, demonstrating your understanding of Go's os package and argument handling.

Problem Description

You need to write a Go program that takes two required command-line arguments: name and age. The program should:

  1. Check for the correct number of arguments: The program must exit with an error message if the user provides fewer than two or more than two arguments.
  2. Parse the arguments: The first argument (name) should be treated as a string, and the second argument (age) should be treated as an integer.
  3. Validate the age: The program should check if the provided age is a positive integer. If the age is not a positive integer, the program should exit with an error message.
  4. Print a greeting: If the arguments are valid, the program should print a greeting message in the format: "Hello, [name]! You are [age] years old."

Expected Behavior

The program should behave as described above, handling invalid input gracefully and providing informative error messages. It should exit with a non-zero exit code (using os.Exit()) when an error occurs.

Edge Cases to Consider

  • Missing arguments: What happens if the user runs the program without any arguments or with only one argument?
  • Invalid age format: What happens if the user provides a non-integer value for the age (e.g., "abc", "3.14")?
  • Negative age: What happens if the user provides a negative age?
  • Zero age: Should zero be considered a valid age? (The problem specifies positive integer)
  • Too many arguments: What happens if the user provides more than two arguments?

Examples

Example 1:

Input: ./program John 30
Output: Hello, John! You are 30 years old.

Explanation: The program receives a valid name and age, so it prints the greeting.

Example 2:

Input: ./program Jane
Output: Error: Missing age argument.

Explanation: The program is missing the age argument, so it prints an error message and exits.

Example 3:

Input: ./program Peter abc
Output: Error: Invalid age format. Age must be a positive integer.

Explanation: The age argument is not a valid integer, so the program prints an error message and exits.

Example 4:

Input: ./program Alice -5
Output: Error: Age must be a positive integer.

Explanation: The age is negative, so the program prints an error message and exits.

Example 5:

Input: ./program Bob 25 extra_arg
Output: Error: Too many arguments provided.

Explanation: More than two arguments were provided, so the program prints an error message and exits.

Constraints

  • The program must be written in Go.
  • The program must use the os package to access command-line arguments.
  • The program must handle errors gracefully and provide informative error messages.
  • The program must exit with a non-zero exit code (using os.Exit()) when an error occurs.
  • The age must be a positive integer (greater than 0).

Notes

  • Consider using the strconv.Atoi() function to convert the age argument to an integer.
  • Remember to check the length of os.Args before accessing individual arguments.
  • Think about how to structure your code to make it readable and maintainable. Error handling is key!
  • The program should not use any external libraries beyond the standard Go library.
Loading editor...
go