Skip to content

Commit

Permalink
fix: make "known" flags w/o alias or default value
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Jul 29, 2019
1 parent a7c483b commit ec5e06d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Sade {
if (val !== void 0) {
arr.push(val);
cmd.default[flag] = val;
} else if (!alias) {
cmd.default[flag] = void 0;
}

cmd.options.push(arr);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/unknown1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const sade = require('../../lib');

sade('bin')
.option('--bool', 'flag defined')
.option('-g, --global', 'global flag')

.command('foo')
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/unknown2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ const sade = require('../../lib');

sade('bin')
.option('-g, --global', 'global flag')
.option('--flag1', 'no alias or default')

.command('foo')
.option('-l, --local', 'command flag')
.option('--flag2', 'no alias or default')
.action(opts => {
console.log(`~> ran "foo" with ${JSON.stringify(opts)}`);
})
Expand Down
18 changes: 18 additions & 0 deletions test/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ test('(usage) unknown.custom', t => {
t.end();
});

test('(usage) unknown.plain', t => {
let pid1 = exec('unknown2.js', ['foo', '--flag1', '--flag2']);
t.is(pid1.status, 0, 'exits without error code');
t.is(pid1.stderr.length, 0, '~> stderr is empty');
t.is(pid1.stdout.toString(), '~> ran "foo" with {"_":[],"flag1":true,"flag2":true}\n', '~> command invoked');

let pid2 = exec('unknown2.js', ['foo', '--flag3']);
t.is(pid2.status, 1, 'exits with error code');
t.is(pid2.stdout.length, 0, '~> stdout is empty');
t.is(
pid2.stderr.toString(),
'\n ERROR\n Custom error: --flag3\n\n Run `$ bin --help` for more info.\n\n',
'~> stderr has "Custom error: --flag3" error message'
);

t.end();
});



test('(usage) subcommands', t => {
Expand Down

0 comments on commit ec5e06d

Please sign in to comment.