Introduction
This challenge aims to test your ability to define and use subcommands with their own set of flags in Golang.
This tutorial is from open-source community. Access the source code
This challenge aims to test your ability to define and use subcommands with their own set of flags in Golang.
You are required to create a program that supports two subcommands, foo
and bar
, each with its own set of flags. The foo
subcommand should have two flags, enable
and name
, while the bar
subcommand should have one flag, level
.
flag
package to define and parse flags.foo
subcommand should have two flags, enable
and name
, both of type string.bar
subcommand should have one flag, level
, of type int.$ go build command-line-subcommands.go
## First invoke the foo subcommand.
$ ./command-line-subcommands foo -enable -name=joe a1 a2
subcommand 'foo'
enable: true
name: joe
tail: [a1 a2]
## Now try bar.
$ ./command-line-subcommands bar -level 8 a1
subcommand 'bar'
level: 8
tail: [a1]
## But bar won't accept foo's flags.
$ ./command-line-subcommands bar -enable a1
flag provided but not defined: -enable
Usage of bar:
-level int
level
## Next we'll look at environment variables, another common
## way to parameterize programs.
In this challenge, you learned how to define and use subcommands with their own set of flags in Golang using the flag
package. You also learned how to parse the flags for each subcommand and access the trailing positional arguments.