Skip to content
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

Default subcommand with options #461

Closed
oscarotero opened this issue Oct 22, 2015 · 10 comments
Closed

Default subcommand with options #461

oscarotero opened this issue Oct 22, 2015 · 10 comments
Milestone

Comments

@oscarotero
Copy link

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'
@natew
Copy link

natew commented Jan 6, 2016

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');
}

@SomeKittens
Copy link
Collaborator

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

@natew
Copy link

natew commented Jan 6, 2016

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.

@SomeKittens
Copy link
Collaborator

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

@natew
Copy link

natew commented Jan 6, 2016

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

@alexcanessa
Copy link

alexcanessa commented Sep 19, 2017

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".

@shadowspawn
Copy link
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. :-)

@shadowspawn
Copy link
Collaborator

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

@shadowspawn
Copy link
Collaborator

v4.0.0-0 prerelease published: #1067

@shadowspawn
Copy link
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
Projects
None yet
Development

No branches or pull requests

5 participants