-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
List all command's arguments using Helper #1823
Comments
When you type something on the command line like
Using There are not any other methods to return the arguments from the private property As a work-around, could you add a description to your arguments so |
Thank you for your answer @shadowspawn 😄
The example is clear, but in the documentation of Commander I am just lost when speaking of sub-commands in the part of arguments while you can declare them with Is there an explanation of why a Visible Argument is one only with a description? I may have missed something in your explanation, but still, the argument is displayed in the usage part of the help 🤔 To describe my situation, I am using NestJS Console, based on top of Commander, and it seems I can't add argument description (it uses Thanks again for the answer ! |
It comes from an existing behaviour that the arguments are only described in detail if there are some descriptions. I like the symmetry with const { Command } = require('commander');
const program = new Command();
program.name('git');
program.command('clone')
.argument('repo')
.action((repo) => console.log(`Target repo is ${repo}`));
program.parse(); % node index.js clone --help
Usage: git clone [options] <repo>
Options:
-h, --help display help for command But with % node index.js clone --help
Usage: git clone [options] <repo>
Arguments:
repo target repository
Options:
-h, --help display help for command |
With the current code, I think you may need to iterate through If the |
Thanks again for your answers! I see there is a method |
There are already properties and methods on Command for:
We can't use the same pattern as |
There is a shortcoming that there is not a public way to get the full array of |
Although it is a private property, but believe it is pretty stable. just write a small tool to show all commands and options: const { program } = require('commander');
const commanderHelp = require('commander-help');
program
.name('cli-name')
.description('CLI to some JavaScript string utilities')
.argument('<username>', 'user to login')
.argument('[password...]', 'password for user, if required', 'empty')
.option('-t, --title <honorific>', 'title to use before name')
.option('-d, --debug', 'display some debugging')
.version('0.8.0', '-v, --version');
program.command('split')
.description('Split a string into substrings')
.argument('<string>', 'string to split')
.alias('s')
.option('--first', 'display just the first substring', '#')
.option('-s, --separator <char>', 'separator character', ',')
.action(() => {});
program
.command('clone <source> [dest]')
.description('clone a repository into a newly created directory')
.alias('c')
.action((source, destination) => {
console.log('clone command called');
});
program
.command('start <service>', 'start named service')
.command('stop [service...]', 'stop named service, or all if no name supplied');
// hide default help
program.helpInformation = function() {
return '';
};
// custom help
program.on('--help', function() {
console.log('Usage and help');
commanderHelp(program);
});
program.parse();
Usage and help
┌──────────────────────────────────┬───────────────────────────────────────────────────┬───────┐
│ Commands/Options │ Description │ Alias │
├──────────────────────────────────┼───────────────────────────────────────────────────┼───────┤
│ ├ cli-name │ CLI to some JavaScript string utilities │ │
│ │ ├ -t, --title <honorific> │ title to use before name │ │
│ │ ├ -d, --debug │ display some debugging │ │
│ │ ├ -v, --version │ output the version number │ │
│ │ └ -h, --help │ display help for command │ │
│ ├ cli-name <username> │ user to login │ │
│ ├ cli-name [password...] │ password for user, if required (default: empty) │ │
├──────────────────────────────────┼───────────────────────────────────────────────────┼───────┤
│ ├ cli-name split <string> │ Split a string into substrings │ s │
│ │ ├ --first │ display just the first substring (default: #) │ │
│ │ └ -s, --separator <char> │ separator character (default: ,) │ │
├──────────────────────────────────┼───────────────────────────────────────────────────┼───────┤
│ ├ cli-name clone <source> [dest] │ clone a repository into a newly created directory │ c │
├──────────────────────────────────┼───────────────────────────────────────────────────┼───────┤
│ ├ cli-name start <service> │ start named service │ │
├──────────────────────────────────┼───────────────────────────────────────────────────┼───────┤
│ └ cli-name stop [service...] │ stop named service, or all if no name supplied │ │
└──────────────────────────────────┴───────────────────────────────────────────────────┴───────┘ |
I had another think about a public accessor, and came up with |
Shipped in Commander v11.1.0 |
I want to list all the arguments (or sub-commands, not sure of the difference) of my command.
If I look the property
_args
, I can confirm my command has an argument:Screenshot of the Argument object
To not use a private property, I try to access this argument with the
Help
class and thevisibleArguments
method (see the code)The problem, I do not have any description for my argument and the implementation of
visibleArguments
requires one.Is this a problem? If not, can someone explain it to me? Is there any other clean way?
Thank you! 😁
The text was updated successfully, but these errors were encountered: