I'm building a CLI tool to start learning some Go. I found this popular package that I wanted to use for some commands like this:
http://godoc.org/github.com/jessevdk/go-flags#Group
https://github.com/jessevdk/go-flags
ex list todos
ex list todos --completed
ex list events
ex list todos events
ex authenticate
The way I understand it is ex
would be my main command.list
and authenticate
are subcommands.
But what are todos
and events
in this scenario? And what would be --completed
be. Is --completed
called an Option
? That's the verbage I see around, but not sure if I'm reading it correctly. I'm trying to find an example that has the different options for something like this scenario, but I could not find one. I tried using urfave/cli which worked at well at first, until I found a requirement to be able to pass multiple entities to ex list
like ex list todos events
. Thanks in advance!
I have used jessevdk/go-flags on multiple projects and really like it. The documentation is sparse, but the code is clean so self-documenting in many ways.
I will try and address your questions as best I can:
ex
would be your binary, and would usually be a 'master' struct of all the top level commands (eg https://github.com/concourse/fly/blob/master/commands/fly.go). This will get parsed in your main function (eg https://github.com/concourse/fly/blob/master/main.go).
todos
and events
would actually be the first level of subcommands. You would define those on the first level command (list
) in the same way you define commands on the 'master' command struct.
--completed
is indeed an option.
One example of an open source project that uses go-flags in the Concourse CI CLI https://github.com/concourse/fly. Although it doesn't you nested commands like you want to do, it gives a pretty solid foundation for go-flags usage.
If you're trying to learn Go, don't use an external package, use the core libraries, like flag.