Commander.js: command with empty string should be the default command

Created on 10 Dec 2014  路  7Comments  路  Source: tj/commander.js

I would like it if you could specify an empty command name and have that be what is invoked if none is specified.

    .command('test')
    .action(function () { console.log('test'); });
program
    .command('*')
    .action(function () { console.log('unknown'); });
program
    .command('') // <- not currently supported
    .action(function () { console.log('default'); });

Then:
$ example test
test
$ example foo
unknown
$ example
default

Right now I am able to replace the last command with:

if(!program.args.length) {
    console.log('default');
}

But it has to go after the call to program.parse and it feels inelegant in comparison.

Most helpful comment

Why doesn't the Commander support the default command? This is a basic behavior for any CLI application.

All 7 comments

:+1:

:+1:

We don't plan to support this right now, but there's a workaround here: https://github.com/tj/commander.js/issues/338

May I ask why not? Would you consider accepting a pull request? Could you at least add the work around from @goloroden to the wiki?

Here's an easy workaround for this issue:

if (process.argv.length === 2) {
  process.argv.push('-h')
}

This causes the help text output to be the default, but you can replace -h with whatever command you want.

Why doesn't the Commander support the default command? This is a basic behavior for any CLI application.

See #742 for the currently open issue.

Was this page helpful?
0 / 5 - 0 ratings