Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check and external subcommands #2

Merged
merged 11 commits into from
Jul 1, 2023
Merged

Check and external subcommands #2

merged 11 commits into from
Jul 1, 2023

Conversation

bobg
Copy link
Owner

@bobg bobg commented Jun 30, 2023

This PR adds "external subcommands," the Check and CheckMap functions, and a number of error variables and types. It also 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.

@bobg bobg marked this pull request as ready for review July 1, 2023 01:51
@bobg bobg merged commit a47d0d3 into master Jul 1, 2023
1 check passed
@bobg bobg deleted the bobg/check-and-prefix branch July 1, 2023 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant