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
Feature/multiple usages #2147
Comments
I appreciate the examples and finding previous issue, thanks. Having more than one usage string or usage example in the synopsis section is a pattern that Commander does not directly support. |
The import { Command } from 'commander';
const program = new Command();
program.configureHelp({ subcommandTerm: (cmd) => cmd.name() + ' ' + cmd.usage() });
const stashCommand = program.command('stash');
stashCommand.command('list').usage('[<log-options>]')
stashCommand.command('show').usage('[-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]');
stashCommand.command('drop').usage('[-q | --quiet] [<stash>]');
program.parse(); % node git.mjs help stash
Usage: git stash [options] [command]
Options:
-h, --help display help for command
Commands:
list [<log-options>]
show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
drop [-q | --quiet] [<stash>]
help [command] display help for command
|
This work-around seems to work reasonably, with manual indentation? import { Command } from 'commander';
const program = new Command();
program.usage(`[options] [ script.js ] [arguments]
node inspect [options] [ script.js | host:port ] [arguments]`);
program.parse(); % node node.mjs --help
Usage: node [options] [ script.js ] [arguments]
node inspect [options] [ script.js | host:port ] [arguments]
Options:
-h, --help display help for command |
Hardcoding a 7 char indent is exactly the workaround I'm using. Sorry I didn't mention that. Smells a wee bit bad, but not a big deal ;) In my particular use case, nested commands won't work, and in fact |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Another possible approach is to include text in the long description, which is fairly freeform (and avoid the 7 char indent annoyance): program.command('stash')
.description(`Synopsis:
git stash list [<log-options>]
git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
git stash drop [-q | --quiet] [<stash>]`)
.summary('Stash the changes in a dirty working directory away'); % node description.mjs stash -h
Usage: description stash [options]
Synopsis:
git stash list [<log-options>]
git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
git stash drop [-q | --quiet] [<stash>]
Options:
-h, --help display help for command |
This has not had any likes (yet) and neither did #80, but I am impressed by the examples here. Keeping this open for more consideration. |
This comment was marked as off-topic.
This comment was marked as off-topic.
As I hinted before, this isn't a big deal. The workaround is fine. That said, since this is JS, I think it would be easy enough to support If you agree with one of the above, I could look into submitting a PR. |
I thought about this a little trying to work around it for one of my CLIs. It's implicit for all the CLIs that have multiple usages that aren't keyed by subcommands like Looking at
So one way to implement this:
In addition, it would be good to have a way to narrow the candidate list by the type of non-option argument. For example,
where the optional If you find this interesting, happy to iterate an API design with you if that helps. |
Note that custom processing of arguments is supported. See: https://github.com/tj/commander.js#custom-argument-processing Like:
|
Validating the command-line arguments using the usage (strings or otherwise) is an interesting idea, but I think going to be too much work. Your examples and comments show the complicated ways utilities overload their usage, like Simply supporting calling usage multiple times should be technically easy, but the earlier issue got no upvotes in 7 years. Since there are easy work-arounds, I don't think it is worth adding without some upvotes or interest on this issue. (Which is why I haven't closed it yet, giving it a chance to gather some support.) |
And to be clear, thanks for sharing. Both for explaining some of your own use-case, and the thoughts on possible approaches. |
Many commands have different usages wherein some options or positional args only make sense with certain commands or other options. While Commander supports expressing this using
Option.conflicts
, its expression to the user is only as an error message after-the-fact. While one can explain these in the help for each option, it leads to a lot of verbosity and redundancy. Showing multiple usages is usually the most succinct way to make this clear to the user, as shown in the examples below.This request was made at least once before as #80.
examples from popular tools
node --help
:git help rebase
:git help stash
:mocha --help
man cp
(*nix), for which the usage pattern depends on whether the last positional arg is a file or directory:workarounds
The only workaround I know of is to place all usages in a single string, using a newline followed by 7 spaces between successive usages. This works as long as the first usage is always prefixed by
Usage:
, or otherwise starts in column 8.The text was updated successfully, but these errors were encountered: