diff --git a/src/lib/parser.ts b/src/lib/parser.ts index 29db4f5..528600a 100644 --- a/src/lib/parser.ts +++ b/src/lib/parser.ts @@ -50,7 +50,7 @@ export class Parser { const split = this.parent.alias.split(match); const check = split && hasAssigned - ? this.checkArgsList(match, split.argsList) + ? this.checkArgsList(split.argsList) : { assign: !!split, options: null }; if (split && check.assign) { // treat left over from split as argument @@ -65,17 +65,19 @@ export class Parser { } } - private checkArgsList(arg: string, argsList: [string, ...string[]][]) { + private checkArgsList(argsList: [string, ...string[]][]) { // e.g. -fb=value, cases: // -b is null -> error // -b is not assignable -> treat as value // -b is assignable -> continue split - const options = - argsList.length > 0 - ? this.parent.parse(argsList[argsList.length - 1][0]) - : null; + const arg = argsList.length > 0 ? argsList[argsList.length - 1][0] : null; + const hasArg = arg != null; + const options = hasArg ? this.parent.parse(arg) : null; // allow assign if no options or if assignable - return { options, assign: !options || isAssignable(arg, options) }; + return { + options, + assign: !options || (hasArg && isAssignable(arg, options)) + }; } private saveArg(arg: string, assigned?: string) { @@ -95,7 +97,7 @@ export class Parser { const aliasArgs = this.parent.alias.getArgs(arg); const check = aliasArgs && hasAssigned - ? this.checkArgsList(arg, aliasArgs) + ? this.checkArgsList(aliasArgs) : { assign: !!aliasArgs, options: null }; const save = aliasArgs && check.assign; if (save) { diff --git a/test/argstree.spec.ts b/test/argstree.spec.ts index dae72aa..045274b 100644 --- a/test/argstree.spec.ts +++ b/test/argstree.spec.ts @@ -669,6 +669,37 @@ describe('argstree', () => { expect(node.children[0].args).to.deep.equal(['foo', 'bar']); }); + it('should handle `assign` option when parsing `=` for aliases', () => { + const node = argstree( + ['-f=0', 'f=0', '-b=0', 'b=0', '-f2=0', 'f2=0', '-b2=0', 'b2=0'], + { + alias: { + '-f': '--foo', + f: '--foo', + '-b': '--bar', + b: '--bar', + '-f2': 'foo', + f2: 'foo', + '-b2': 'bar', + b2: 'bar' + }, + args: { + '--foo': { maxRead: 0 }, + '--bar': { maxRead: 0, assign: false }, + foo: { maxRead: 0 }, + bar: { maxRead: 0, assign: true } + } + } + ); + expect(node.args).to.deep.equal(['-b=0', 'b=0', '-f2=0', 'f2=0']); + expect(node.children).to.have.length(4); + const match = ['--foo', '--foo', 'bar', 'bar']; + node.children.forEach((child, index) => { + expect(child.id).to.equal(match[index]); + expect(child.args).to.deep.equal(['0']); + }); + }); + it('should not parse `=` for combined aliases if last option is not assignable', () => { // force empty arrays const options = { diff --git a/test/stringify.spec.ts b/test/stringify.spec.ts index 8e3002f..e496d17 100644 --- a/test/stringify.spec.ts +++ b/test/stringify.spec.ts @@ -162,7 +162,7 @@ describe('stringify', () => { args: { foo: {}, bar: {}, - baz: { args: { test: { max: 0 }, '--': {} } } + baz: { assign: true, args: { test: { max: 0 }, '--': {} } } } } );