command

Documentation
Login

Package command exposes CommandLine which can be used like flag.CommandLine (embedded). The idea is to provide an API nearly identical to the stdlib flag package, with extra features. This package will remain small, so consumers can either import this package or copy this file to their main package, if they prefer to avoid an additional dependency.

In addition to flag variables as provided by the stdlib, this package introduces Operation callbacks. Each operation has a function signature similar to the top level main, and can be implemented in a similar way. That is, the operation callback can define flags on the CommandLine in the same way that main does.

If a command "foo" defines operations "bar" and "baz", then when the main function calls [CommandLine.Parse], one of those callbacks will be run, depending on the args passed to the command. For example,

foo -fooarg=x # runs `main` as usual
foo -fooarg=x bar # runs the bar callback
foo baz -fooarg=x # runs the baz callback

If the operations need their own flags or arguments, they rely on the CommandLine API, exactly as the main function does. They may even define their own sub-operation callbacks.

On the command line, flags defined in main can either preceed or follow the operation name, as a convenience to the user.