Repository navigation
0.0.4: actions return a status; run dispatches at any depth; usage errors go to standard error - #11
Merged
Merged
Conversation
…rors go to standard error An action is int(const ParsedArgs&), whose result is the status run returns, or void(const ParsedArgs&), whose status is 0; existing void actions are unchanged. run(parsed) descends to the deepest matched subcommand at any depth and runs its action, else the nearest ancestor's (which dispatches on subcommand_name()); a command group named without a subcommand writes its subcommands to standard error and returns 2. run(argc, argv) and the new run(Argv) write a parse error to standard error, as '<name>: <message>' and a pointer to --help, and return 2, the status of a usage error; standard output carries only help, the version and the actions' own output. parse and parse_from share one token entry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three programs built on cmdline work around the same three gaps, each in its own way:
rundispatches one level only, so nested command groups are dispatched by hand;run(argc, argv)prints a parse error to standard output and returns 1, so programs whose standard output carries data parse and report errors themselves.This change closes the three gaps in the library. It is source-compatible with 0.0.3.
Changes
actionacceptsint(const ParsedArgs&), whose result is the statusrunreturns, orvoid(const ParsedArgs&), whose status is 0. The callable is checked by a constraint: it returnsvoidor a type convertible toint.int run(const ParsedArgs&)replacesvoid run(const ParsedArgs&). It descends to the deepest matched subcommand at any depth and runs that command's action. A command without an action leaves the run to its nearest ancestor that has one; the ancestor receives its own parse and dispatches onsubcommand_name(), which is what one-level dispatchers already do. When no command on the path has an action and the named command has subcommands, the run writes the subcommands to standard error and returns 2. Otherwise it returns 0.run(argc, argv)and the newrun(std::span<const std::string>)print help and the version to standard output and return 0. A parse error is written to standard error as<name>: <message>, followed by<name>: see '<name> --help', and the run returns 2, the usual status of a usage error.parseandparse_fromshare one token entry.docs/api.mdstates the dispatch rules and the statuses; the README lists them.Compatibility
voidactions compile and behave as before.run(parsed)still compile; its result is now the status.run(argc, argv)goes to standard error and returns 2 (previously standard output and 1);Tests
mcpp testruns 51 tests, all passing. The new tests cover:run(Argv)statuses, a parse error on standard error with empty standard output, help and version returning 0, andrun(argc, argv).