The cobra classfile has the file suffix _cmd.gox.
First let us initialize a hellocli project:
gop mod init hellocliThen we have it reference the cobra classfile as the CLI framework:
gop get github.com/goplus/cobra@latestCreate a file named version_cmd.gox with the following content:
run => {
echo "command: version"
}Execute the following commands:
gop mod tidy
gop install .
hellocliYou may get the following output:
Usage:
hellocli [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
version
Flags:
-h, --help help for hellocli
Use "hellocli [command] --help" for more information about a command.Continue to modify the version command:
// short sets the short description shown in the 'help' output.
short "print Go version"
// long sets the long message shown in the 'help <this-command>' output.
long `Version prints the build information for Go binary files.
`
run => {
echo "command: version"
}The cobra classfile uses tags of class fields to specify command flags.
var (
Verbose bool `flag:"verbose, short: v, val: false, usage: print verbose information"`
)
run => {
echo "command: version", "verbose:", Verbose
}If a command has non-flag arguments, use run args => { ... } instead of run => { ... } to get non-flag arguments:
run args => {
echo "version:", args
}Create a file named mod_cmd.gox with the following content:
run => {
help
}And create a file named mod_init_cmd.gox with the following content:
run => {
echo "subcommand: mod init"
}