Skip to content

Commit

Permalink
Fix aliases to follow assign option
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnesfield committed Mar 24, 2024
1 parent 8765413 commit 8c9dc1b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
31 changes: 31 additions & 0 deletions test/argstree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion test/stringify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('stringify', () => {
args: {
foo: {},
bar: {},
baz: { args: { test: { max: 0 }, '--': {} } }
baz: { assign: true, args: { test: { max: 0 }, '--': {} } }
}
}
);
Expand Down

0 comments on commit 8c9dc1b

Please sign in to comment.