Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions deno/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ class Sade {

const all = this.tree[ALL];
// merge all objects :: params > command > all
opts.alias = Object.assign(all.alias, cmd.alias, opts.alias);
opts.default = Object.assign(all.default, cmd.default, opts.default);
opts.alias = Object.assign({}, all.alias, cmd.alias, opts.alias);
opts.default = Object.assign({}, all.default, cmd.default, opts.default);

tmp = name.split(" ");
idx = arr.indexOf(tmp[0], 2);
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ class Sade {

let all = this.tree[ALL];
// merge all objects :: params > command > all
opts.alias = Object.assign(all.alias, cmd.alias, opts.alias);
opts.default = Object.assign(all.default, cmd.default, opts.default);
opts.alias = Object.assign({}, all.alias, cmd.alias, opts.alias);
opts.default = Object.assign({}, all.default, cmd.default, opts.default);
// mri consumes alias arrays while populating the result.
for (let key in opts.alias) {
if (Array.isArray(opts.alias[key])) opts.alias[key] = opts.alias[key].slice();
}

tmp = name.split(' ');
idx = arr.indexOf(tmp[0], 2);
Expand Down
33 changes: 33 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,36 @@ test('prog.parse :: lazy :: single', t => {

bar.handler.apply(null, bar.args); // manual bcuz lazy; +2 tests
});


test('prog.parse :: command options stay local', t => {
let ctx = sade('foo')
.option('--mode', 'Global mode', 'global')
.command('build').option('-o, --output', 'Output', 'dist').action(() => {})
.command('serve').action(() => {});

let build = ctx.parse(['', '', 'build'], { lazy:true });
t.is(build.args[0].output, 'dist', 'uses the command default');
t.is(build.args[0].o, 'dist', 'uses the command alias');

let serve = ctx.parse(['', '', 'serve', '-o', 'public'], { lazy:true });
t.same(serve.args, [{ _:[], o:'public', mode:'global' }], 'does not inherit another command alias or default');

ctx.parse(['', '', 'serve'], { lazy:true, default:{ mode:'temporary' }, alias:{ m:'mode' } });
serve = ctx.parse(['', '', 'serve', '-m', 'local'], { lazy:true });
t.same(serve.args, [{ _:[], m:'local', mode:'global' }], 'does not retain per-call defaults or aliases');
t.end();
});


test('prog.parse :: reusable short aliases', t => {
let ctx = sade('foo')
.option('-g, --global')
.command('build').option('-o, --output').action(() => {});
for (let value of ['first', 'second']) {
let result = ctx.parse(['', '', 'build', '-g', value, '-o', value], { lazy:true });
t.is(result.args[0].global, value, 'retains global alias');
t.is(result.args[0].output, value, 'retains command alias');
}
t.end();
});