diff --git a/examples/pm b/examples/pm index baac7816f..bb4deb26e 100755 --- a/examples/pm +++ b/examples/pm @@ -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, @@ -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 diff --git a/examples/pm-default.js b/examples/pm-default.js new file mode 100644 index 000000000..97b9f4b8a --- /dev/null +++ b/examples/pm-default.js @@ -0,0 +1,9 @@ +#!/usr/bin/env node +var program = require('..'); + +program + .option('-t --test ', 'Test inherit subcommand option', /^\d+$/, 500) + .parse(process.argv); + +console.log('default'); +console.log(program.test); \ No newline at end of file diff --git a/index.js b/index.js index 5344efb08..36f6f32f3 100644 --- a/index.js +++ b/index.js @@ -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]); } } diff --git a/package.json b/package.json index fb7a2bba0..d861f7f4c 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/fixtures/pm-default b/test/fixtures/pm-default index c053cca81..503f3681b 100755 --- a/test/fixtures/pm-default +++ b/test/fixtures/pm-default @@ -1,2 +1,9 @@ #!/usr/bin/env node +var program = require('../../'); + +program + .option('-t --test ', 'Test inherit subcommand option', /^\d+$/, 500) + .parse(process.argv); + console.log('default'); +console.log(program.test); \ No newline at end of file diff --git a/test/test.command.executableSubcommandDefault.js b/test/test.command.executableSubcommandDefault.js index 36796d867..547098a50 100644 --- a/test/test.command.executableSubcommandDefault.js +++ b/test/test.command.executableSubcommandDefault.js @@ -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