Skip to content

Default subcommand with options #461

Closed

Description

@oscarotero

Hi, I want to use a subcommand as default, and this subcommands has options. Something like this:

//app.js

program
    .command('run', 'Execute stuff', {isDefault: true})
    .command('list', 'List stuff')
    .parse(process.argv);
// app-run.js

program
    .option('-c, --config <file>', 'use a config file')
    .parse(process.argv);

This command works fine:

app run -c config.json

And this as well (executes the default subcommand):

app

but on execute the default subcommand with options:

app -c config.json

I have the followin error:

  error: unknown option `-c'

Activity

  1. natew commented on Jan 6, 2016

    @natew

    Big +1 on this

    My workaround is not to use isDefault and just coerce argv, for now:

    // make `flint run` default command
    var flintIndex = getFlintIndex()
    
     // find where index of flint is
    function getFlintIndex() {
      let index = 0
      for (let arg of process.argv) {
        if (arg.indexOf('flint') > 0)
          return index
    
        index++
      }
    }
    
    // make sure flags are still passed to `flint run`
    var firstFlag = process.argv[flintIndex + 1]
    
    if (flintIndex === process.argv.length - 1 || (firstFlag && firstFlag[0] === '-')) {
      process.flintArgs = [].concat(process.argv);
      process.flintArgs.splice(flintIndex + 1, 0, 'run');
    }
  2. SomeKittens commented on Jan 6, 2016

    @SomeKittens
    Collaborator

    Not sure how we'd accomplish this - you want us to skip all params if there's a default subcommand?

  3. natew commented on Jan 6, 2016

    @natew

    No, it's just to pass those options down to the subcommand.

    So flint run --cached works, and if run is default, then flint --cached would work the same.

  4. SomeKittens commented on Jan 6, 2016

    @SomeKittens
    Collaborator

    My issue is what happens if the root program (app in this case) also has a -c option.

  5. natew commented on Jan 6, 2016

    @natew

    Ah, so could we have an option isDefault: true, passOptions: true

  6. alexcanessa commented on Sep 19, 2017

    @alexcanessa

    Massive +1 on this one.

    To @SomeKittens's point:

    My issue is what happens if the root program (app in this case) also has a -c option.

    If isDefault has been set to true, means that there isn't going to be any "root program".

  7. shadowspawn commented on Aug 10, 2019

    @shadowspawn
    Collaborator

    Took me a while to get on board with this one, but I now agree this is the correct behaviour and interested in making it happen.

    The PR in flight is #614, and an easy code change. :-)

  8. added this to the v4.0.0 milestone on Sep 21, 2019
  9. shadowspawn commented on Sep 22, 2019

    @shadowspawn
    Collaborator

    There are improvements on the develop branch for the next release (#614 and #1047).

  10. shadowspawn commented on Oct 1, 2019

    @shadowspawn
    Collaborator

    v4.0.0-0 prerelease published: #1067

  11. shadowspawn commented on Nov 1, 2019

    @shadowspawn
    Collaborator

    v4.0.0 has been released which hopefully resolves this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions