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

Inherit arguments to default subcommand #614

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/pm
Expand Up @@ -8,6 +8,7 @@ program
.command('search [query]', 'search with optional query').alias('s')
.command('list', 'list packages installed')
.command('publish', 'publish the package').alias('p')
.command('default', 'default command', { noHelp: true, isDefault: true })
.parse(process.argv);

// here .command() is invoked with a description,
Expand All @@ -25,3 +26,4 @@ program
// ./examples/pm install -h
// ./examples/pm install foo bar baz
// ./examples/pm install foo bar baz --force
// ./examples/pm -t 800
9 changes: 9 additions & 0 deletions examples/pm-default.js
@@ -0,0 +1,9 @@
#!/usr/bin/env node
var program = require('..');

program
.option('-t --test <number>', 'Test inherit subcommand option', /^\d+$/, 500)
.parse(process.argv);

console.log('default');
console.log(program.test);
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -634,7 +634,8 @@ Command.prototype.parseArgs = function(args, unknown) {

// If there were no args and we have unknown options,
// then they are extraneous and we need to error.
if (unknown.length > 0) {
// Inherit unknown args to 'defaultExecutable'
if (unknown.length > 0 && !this.defaultExecutable) {
this.unknownOption(unknown[0]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -16,7 +16,7 @@
},
"devDependencies": {
"should": ">= 0.0.1 <9.0.0",
"sinon": ">=1.17.1"
"sinon": ">=1.17.1 <=1.17.7"
},
"scripts": {
"test": "make test"
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/pm-default
@@ -1,2 +1,9 @@
#!/usr/bin/env node
var program = require('../../');

program
.option('-t --test <number>', 'Test inherit subcommand option', /^\d+$/, 500)
.parse(process.argv);

console.log('default');
console.log(program.test);
14 changes: 12 additions & 2 deletions test/test.command.executableSubcommandDefault.js
Expand Up @@ -7,12 +7,22 @@ var exec = require('child_process').exec
var bin = path.join(__dirname, './fixtures/pm')
// success case
exec(bin + ' default', function(error, stdout, stderr) {
stdout.should.equal('default\n');
stdout.should.equal('default\n500\n');
});

// success case (default)
exec(bin, function(error, stdout, stderr) {
stdout.should.equal('default\n');
stdout.should.equal('default\n500\n');
});

// success case (default) and alisa option
exec(bin + ' -t 600', function(error, stdout, stderr) {
stdout.should.equal('default\n600\n');
});

// success case (default) and option
exec(bin + ' --test 800', function(error, stdout, stderr) {
stdout.should.equal('default\n800\n');
});

// not exist
Expand Down