Skip to content

"Check" and external subcommands

Compare
Choose a tag to compare
@bobg bobg released this 01 Jul 17:35
· 4 commits to master since this release
a47d0d3

This release adds "external subcommands" and the Check and CheckMap functions, and expands the semantics of the Commands function.

External subcommands

In a call to Run(ctx, cmd, args), args[0] is the name of a subcommand to execute from the cmd.Subcmds map. If name does not appear in the map, the result is normally an UnknownSubcmdErr error.

But with this change, if cmd implements the Prefixer interface in addition to Cmd, then cmd.Prefix is called to get a prefix, and then an executable named prefixname is sought in $PATH. If found, it's executed with the remaining args as arguments, and a JSON representation of cmd in the environment variable SUBCMD_ENV (which can be parsed with the new function ParseEnv).

This feature is patterned after command-line tools such as git, which has some built-in subcommands of its own, but which can be extended by putting an executable named git-foo in $PATH (to create subcommand foo).

Check and CheckMap

These new functions perform various runtime type-safety checks on a Subcmd and a Map, respectively.

Commands

The Command function previously required arguments in groups of four:

  • subcommand name;
  • function implementing the subcommand;
  • short description;
  • parameters as a []Param.

With this change, a group may now also be a pair of arguments:

  • subcommand name;
  • Subcmd object.