Skip to content

Commit 6a08885

Browse files
committed
fix: handle same-length command aliases
1 parent fa12818 commit 6a08885

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

lib/index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class Sade {
6060
alias(...names) {
6161
if (this.single) throw new Error('Cannot call `alias()` in "single" mode');
6262
if (!this.curr) throw new Error('Cannot call `alias()` before defining a command');
63-
this.tree[this.curr].alibi = this.tree[this.curr].alibi.concat(...names);
63+
let arr = this.tree[this.curr].alibi = this.tree[this.curr].alibi.concat(...names);
64+
arr.forEach(key => this.tree[key] = this.curr);
6465
return this;
6566
}
6667

@@ -118,20 +119,18 @@ class Sade {
118119
cmd = this.tree[DEF];
119120
} else {
120121
// Loop thru possible command(s)
121-
let k, i=1, len=argv._.length + 1;
122+
let i=1, xyz, len=argv._.length + 1;
122123
for (; i < len; i++) {
123124
tmp = argv._.slice(0, i).join(' ');
124-
if (this.tree[tmp] !== void 0) {
125-
name=tmp; idx=arr.indexOf(tmp, 1);
126-
} else {
127-
for (k in this.tree) {
128-
if (this.tree[k].alibi.includes(tmp)) {
129-
idx = arr.indexOf(tmp);
130-
arr.splice(idx, 1, ...k.split(' '));
131-
name = k;
132-
break;
133-
}
134-
}
125+
xyz = this.tree[tmp];
126+
if (typeof xyz === 'string') {
127+
idx = (name=xyz).split(' ');
128+
arr.splice(arr.indexOf(argv._[0]), i, ...idx);
129+
i += (idx.length - i);
130+
} else if (xyz) {
131+
name = tmp;
132+
} else if (name) {
133+
break;
135134
}
136135
}
137136

lib/utils.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,17 @@ exports.help = function (bin, tree, key, single) {
5353
out += section('Usage', [cmd.usage], prefix);
5454

5555
if (!single && key === DEF) {
56-
// General help :: print all non-internal commands & their 1st line of text
57-
let cmds = Object.keys(tree).filter(k => !/__/.test(k));
58-
let text = cmds.map(k => [k, (tree[k].describe || [''])[0]]);
59-
out += section('Available Commands', format(text), noop);
56+
let key, rgx=/^__/, help='', cmds=[];
57+
// General help :: print all non-(alias|internal) commands & their 1st line of helptext
58+
for (key in tree) {
59+
if (typeof tree[key] == 'string' || rgx.test(key)) continue;
60+
if (cmds.push([key, (tree[key].describe || [''])[0]]) < 3) {
61+
help += (NL + __ + __ + `${pfx} ${key} --help`);
62+
}
63+
}
6064

61-
out += (NL + __ + 'For more info, run any command with the `--help` flag');
62-
cmds.slice(0, 2).forEach(k => {
63-
out += (NL + __ + __ + `${pfx} ${k} --help`);
64-
});
65-
out += NL;
65+
out += section('Available Commands', format(cmds), noop);
66+
out += (NL + __ + 'For more info, run any command with the `--help` flag') + help + NL;
6667
} else if (!single && key !== DEF) {
6768
// Command help :: print its aliases if any
6869
out += section('Aliases', cmd.alibi, prefix);

test/fixtures/subs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sade('bin')
77
console.log('~> ran "remote" action');
88
})
99

10-
.command('remote add <name> <url>', '', { alias: 'ra' })
10+
.command('remote add <name> <url>', '', { alias: ['ra', 'remote new'] })
1111
.action((name, uri, opts) => {
1212
console.log(`~> ran "remote add" with "${name}" and "${uri}" args`);
1313
})

test/usage.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,11 @@ test('(usage) order :: subcommands', t => {
10121012
t.is(pid3.stdout.toString(), '~> ran "remote add" with "origin" and "foobar" args\n', '~> ran "add" child');
10131013
t.is(pid3.stderr.length, 0, '~> stderr is empty');
10141014

1015+
let pid4 = exec('subs.js', ['--foo', 'bar', 'remote', 'new', 'origin', 'foobar']);
1016+
t.is(pid4.status, 0, 'exits without error code');
1017+
t.is(pid4.stdout.toString(), '~> ran "remote add" with "origin" and "foobar" args\n', '~> ran "add" child');
1018+
t.is(pid4.stderr.length, 0, '~> stderr is empty');
1019+
10151020
t.end();
10161021
});
10171022

@@ -1031,6 +1036,11 @@ test('(usage) order :: subcommands :: help', t => {
10311036
t.true(pid3.stdout.toString().includes('Usage\n $ bin remote rename <old> <new> [options]'), '~> shows "remote rename" help text');
10321037
t.is(pid3.stderr.length, 0, '~> stderr is empty');
10331038

1039+
let pid4 = exec('subs.js', ['--foo', 'bar', 'remote', 'new', '--help']);
1040+
t.is(pid4.status, 0, 'exits without error code');
1041+
t.is(pid4.stdout.toString(), '\n Usage\n $ bin remote add <name> <url> [options]\n\n Aliases\n $ bin ra\n $ bin remote new\n\n Options\n -h, --help Displays this message\n\n');
1042+
t.is(pid4.stderr.length, 0, '~> stderr is empty');
1043+
10341044
t.end();
10351045
});
10361046

0 commit comments

Comments
 (0)