Using command-line arguments
The flags from the previous recipe are a type of command-line argument. This chapter will expand on other uses for these arguments by constructing a command that supports nested subcommands. This will demonstrate Flagsets and also use positional arguments passed into your application.
Like the previous recipe, this one requires a main function to run. There are a number of third-party packages to deal with complex nested arguments and flags, but we'll investigate how to do that using only the standard library.
Getting ready
Refer to the Getting ready section's steps in the Using command-line flags recipe.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create a new directory called
chapter2/cmdargs
and navigate to that directory.
- Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter2/cmdargs, or use this as an exercise to write some of your own code!
- Create a file called
cmdargs.go
with...