commander.js icon indicating copy to clipboard operation
commander.js copied to clipboard

defaultAction or something...

Open jonasfj opened this issue 12 years ago • 7 comments

There should be a fall back action, if no other action is taken... Or just a way to check if another action was taken. Consider the example below:

 program
   .version('0.0.1')

 program
   .command('setup')
   .description('run remote setup commands')
   .action(function(){
     console.log('setup');
   });

 program
   .command('exec <cmd>')
   .description('run the given remote command')
   .action(function(cmd){
     console.log('exec "%s"', cmd);
   });

 program.parse(process.argv);

Say I save it in program.js and now type node program.js then nothing will happen. No action is taken and no error message is displayed. And even if I wanted to I can't detect if an action was taken... We should have something like this:

 program
   .version('0.0.1')

 program
   .command('setup')
   .description('run remote setup commands')
   .action(function(){
     console.log('setup');
   });

program
  .defaultAction(function() {
    // Only called if no other action is called...
   console.log("Usage: ./program.js setup");
  });
 program.parse(process.argv);


// Or we could just have a boolean `program.actionTaken` 
if (!program.actionTaken) {
  console.log("Usage: ./program.js setup");
}

As is right now, it's not possible to detect if an action was taken, or not... So you can't print a warning, or info message showing which actions are available. I understand that commander can be used without actions (and subcommands) at all. All I'm asking for is a boolean indicating if an action (ie. subcommand) was called. Both use-cases can be maintained.

jonasfj avatar May 08 '14 18:05 jonasfj

I'll happily implement this, and file a PR, if we can get some consensus on how to solve the problem.

jonasfj avatar May 08 '14 18:05 jonasfj

If I recall correctly there are two different things:

  1. Unknown command:
program
   .on('*', function() {
        \\\ Do something
   });
  1. No command
if (program.args.length < 1 ) {
    \\\ Do something
}

Hypercubed avatar May 09 '14 01:05 Hypercubed

I stand corrected :)

Using both program.on('*', function() {/* usage() */}); and if(program.args.length < 1) { /* usage() */ } does solve the problem. Could we either document this... Or add a way to define a fallback action if no action is provided.

Please, close this bug, if there is no interest.

jonasfj avatar May 09 '14 18:05 jonasfj

I'm seeing some inconsistencies with how @Hypercubed mentioned "No command" works. I have a command which looks like:

program
    .command('run <folder>')
    .option('-e, --env [KEY=VALUE]', 'Environment variables to set in container (repeatable)', collect, [])
    .option('-r, --restart ', 'Restart passenger/nginx on every request')
    .description('Build and run a container, watching for changes')
    .action(require('../lib/run'));

If I run this command with run <folder> -r, program.args.length is indeed > 0. However, if I run the command properly (as --help mentions it should be run) then program.args.lengh === 0. In both cases the options are correctly passed to action() with the correct flags set so I'm not sure why this is broken.

amjibaly avatar Jul 18 '14 09:07 amjibaly

@amjibaly Can you file that as a separate issue? OP had issue resolved so I'm closing this.

SomeKittens avatar Jul 18 '14 23:07 SomeKittens

@SomeKittens never mind, I realized what I was doing wrong. I had program.on('*') after program.parse(process.argv). Thanks!

amjibaly avatar Jul 20 '14 19:07 amjibaly

@amjibaly Sounds good. Thanks for the update.

SomeKittens avatar Jul 21 '14 19:07 SomeKittens