Skip to content

Commit

Permalink
fix: re-increment offset when default command invoked
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Feb 13, 2020
1 parent a5a332d commit 07645f8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ class Sade {
}

parse(arr, opts={}) {
let tmp, idx, isVoid, cmd;
let offset=2, tmp, idx, isVoid, cmd;
let alias = { h:'help', v:'version' };
let argv = mri(arr.slice(2), { alias });
let argv = mri(arr.slice(offset), { alias });
let isSingle = this.single;
let bin = this.bin;
let name = '';
Expand Down Expand Up @@ -142,6 +142,7 @@ class Sade {
name = this.default;
cmd = this.tree[name];
arr.unshift(name);
offset++;
} else if (tmp) {
return $.error(bin, `Invalid command: ${tmp}`);
} //=> else: cmd not specified, wait for now...
Expand All @@ -165,7 +166,7 @@ class Sade {
idx = arr.indexOf(tmp[0], 2);
if (!!~idx) arr.splice(idx, tmp.length);

let vals = mri(arr.slice(2), opts);
let vals = mri(arr.slice(offset), opts);
if (!vals || typeof vals === 'string') {
return $.error(bin, vals || 'Parsed unknown option flag(s)!');
}
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
const sade = require('../../lib');

sade('bin')
.command('foo', null, { alias: 'f', default:true })
.action(() => console.log('~> ran "foo" action'))
.command('foo [dir]', null, { alias: 'f', default:true })
.action(dir => console.log(`~> ran "foo" action w/ "${dir || '~EMPTY~'}" arg`))

.command('bar')
.alias('b')
Expand Down
74 changes: 63 additions & 11 deletions test/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,12 @@ test('(usage) subcommands :: error :: invalid command', t => {
test('(usage) default', t => {
let pid1 = exec('default.js', []);
t.is(pid1.status, 0, 'exits without error code');
t.is(pid1.stdout.toString(), '~> ran "foo" action\n', '~> ran default command');
t.is(pid1.stdout.toString(), '~> ran "foo" action w/ "~EMPTY~" arg\n', '~> ran default command');
t.is(pid1.stderr.length, 0, '~> stderr is empty');

let pid2 = exec('default.js', ['foo']);
t.is(pid2.status, 0, 'exits without error code');
t.is(pid2.stdout.toString(), '~> ran "foo" action\n', '~> ran default command (direct)');
t.is(pid2.stdout.toString(), '~> ran "foo" action w/ "~EMPTY~" arg\n', '~> ran default command (direct)');
t.is(pid2.stderr.length, 0, '~> stderr is empty');

let pid3 = exec('default.js', ['bar']);
Expand All @@ -330,6 +330,20 @@ test('(usage) default', t => {
t.end();
});

test('(usage) default :: args', t => {
let pid1 = exec('default.js', ['hello']);
t.is(pid1.status, 0, 'exits without error code');
t.is(pid1.stdout.toString(), '~> ran "foo" action w/ "hello" arg\n');
t.is(pid1.stderr.length, 0, '~> stderr is empty');

let pid2 = exec('default.js', ['foo', 'hello']);
t.is(pid2.status, 0, 'exits without error code');
t.is(pid2.stdout.toString(), '~> ran "foo" action w/ "hello" arg\n', '~> ran default command (direct)');
t.is(pid2.stderr.length, 0, '~> stderr is empty');

t.end();
});

test('(usage) default :: help', t => {
let pid1 = exec('default.js', ['--help']);
t.is(pid1.status, 0, 'exits without error code');
Expand All @@ -338,7 +352,7 @@ test('(usage) default :: help', t => {

let pid2 = exec('default.js', ['foo', '-h']);
t.is(pid2.status, 0, 'exits without error code');
t.true(pid2.stdout.toString().includes('Usage\n $ bin foo [options]'), '~> shows command help w/ "Usage" text');
t.true(pid2.stdout.toString().includes('Usage\n $ bin foo [dir] [options]'), '~> shows command help w/ "Usage" text');
t.is(pid2.stderr.length, 0, '~> stderr is empty');

let pid3 = exec('default.js', ['bar', '-h']);
Expand Down Expand Up @@ -698,19 +712,38 @@ test('(usage) alias :: subcommands :: help', t => {
test('(usage) alias :: default', t => {
let pid1 = exec('default.js', []);
t.is(pid1.status, 0, 'exits without error code');
t.is(pid1.stdout.toString(), '~> ran "foo" action\n', '~> ran default command');
t.is(pid1.stdout.toString(), '~> ran "foo" action w/ "~EMPTY~" arg\n', '~> ran default command');
t.is(pid1.stderr.length, 0, '~> stderr is empty');

let pid2 = exec('default.js', ['f']);
t.is(pid2.status, 0, 'exits without error code');
t.is(pid2.stdout.toString(), '~> ran "foo" action\n', '~> ran default command (direct)');
t.is(pid2.stdout.toString(), '~> ran "foo" action w/ "~EMPTY~" arg\n', '~> ran default command (direct)');
t.is(pid2.stderr.length, 0, '~> stderr is empty');

let pid3 = exec('default.js', ['b']);
let pid3 = exec('default.js', ['f', 'hello']);
t.is(pid3.status, 0, 'exits without error code');
t.is(pid3.stdout.toString(), '~> ran "bar" action\n', '~> ran "bar" command');
t.is(pid3.stdout.toString(), '~> ran "foo" action w/ "hello" arg\n', '~> ran default command (direct)');
t.is(pid3.stderr.length, 0, '~> stderr is empty');

let pid4 = exec('default.js', ['b']);
t.is(pid4.status, 0, 'exits without error code');
t.is(pid4.stdout.toString(), '~> ran "bar" action\n', '~> ran "bar" command');
t.is(pid4.stderr.length, 0, '~> stderr is empty');

t.end();
});

test('(usage) default :: args', t => {
let pid1 = exec('default.js', ['hello']);
t.is(pid1.status, 0, 'exits without error code');
t.is(pid1.stdout.toString(), '~> ran "foo" action w/ "hello" arg\n');
t.is(pid1.stderr.length, 0, '~> stderr is empty');

let pid2 = exec('default.js', ['f', 'hello']);
t.is(pid2.status, 0, 'exits without error code');
t.is(pid2.stdout.toString(), '~> ran "foo" action w/ "hello" arg\n', '~> ran default command (direct)');
t.is(pid2.stderr.length, 0, '~> stderr is empty');

t.end();
});

Expand All @@ -722,7 +755,7 @@ test('(usage) alias :: default :: help', t => {

let pid2 = exec('default.js', ['f', '-h']);
t.is(pid2.status, 0, 'exits without error code');
t.true(pid2.stdout.toString().includes('Usage\n $ bin foo [options]'), '~> shows command help w/ "Usage" text');
t.true(pid2.stdout.toString().includes('Usage\n $ bin foo [dir] [options]'), '~> shows command help w/ "Usage" text');
t.is(pid2.stderr.length, 0, '~> stderr is empty');

let pid3 = exec('default.js', ['b', '-h']);
Expand Down Expand Up @@ -1005,12 +1038,12 @@ test('(usage) order :: subcommands :: help', t => {
test('(usage) order :: default', t => {
let pid1 = exec('default.js', ['--foo', 'bar']);
t.is(pid1.status, 0, 'exits without error code');
t.is(pid1.stdout.toString(), '~> ran "foo" action\n', '~> ran default command');
t.is(pid1.stdout.toString(), '~> ran "foo" action w/ "~EMPTY~" arg\n', '~> ran default command');
t.is(pid1.stderr.length, 0, '~> stderr is empty');

let pid2 = exec('default.js', ['--foo', 'bar', 'f']);
t.is(pid2.status, 0, 'exits without error code');
t.is(pid2.stdout.toString(), '~> ran "foo" action\n', '~> ran default command (direct)');
t.is(pid2.stdout.toString(), '~> ran "foo" action w/ "~EMPTY~" arg\n', '~> ran default command (direct)');
t.is(pid2.stderr.length, 0, '~> stderr is empty');

let pid3 = exec('default.js', ['--foo', 'bar', 'b']);
Expand All @@ -1021,6 +1054,25 @@ test('(usage) order :: default', t => {
t.end();
});

test('(usage) default :: args', t => {
let pid1 = exec('default.js', ['--foo', 'bar', 'hello']);
t.is(pid1.status, 0, 'exits without error code');
t.is(pid1.stdout.toString(), '~> ran "foo" action w/ "hello" arg\n');
t.is(pid1.stderr.length, 0, '~> stderr is empty');

let pid2 = exec('default.js', ['--foo', 'bar', 'foo', 'hello']);
t.is(pid2.status, 0, 'exits without error code');
t.is(pid2.stdout.toString(), '~> ran "foo" action w/ "hello" arg\n', '~> ran default command (direct)');
t.is(pid2.stderr.length, 0, '~> stderr is empty');

let pid3 = exec('default.js', ['--foo', 'bar', 'foo', 'hello']);
t.is(pid3.status, 0, 'exits without error code');
t.is(pid3.stdout.toString(), '~> ran "foo" action w/ "hello" arg\n', '~> ran default command (direct)');
t.is(pid3.stderr.length, 0, '~> stderr is empty');

t.end();
});

test('(usage) order :: default :: help', t => {
let pid1 = exec('default.js', ['--foo', 'bar', '--help']);
t.is(pid1.status, 0, 'exits without error code');
Expand All @@ -1029,7 +1081,7 @@ test('(usage) order :: default :: help', t => {

let pid2 = exec('default.js', ['--foo', 'bar', 'f', '-h']);
t.is(pid2.status, 0, 'exits without error code');
t.true(pid2.stdout.toString().includes('Usage\n $ bin foo [options]'), '~> shows command help w/ "Usage" text');
t.true(pid2.stdout.toString().includes('Usage\n $ bin foo [dir] [options]'), '~> shows command help w/ "Usage" text');
t.is(pid2.stderr.length, 0, '~> stderr is empty');

let pid3 = exec('default.js', ['--foo', 'bar', 'b', '-h']);
Expand Down

0 comments on commit 07645f8

Please sign in to comment.