Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: switch type:string option arguments to greedy, but with error for suspect cases in strict mode #88

Merged
merged 24 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a28864d
First cut at greedy with error for suspect value
shadowspawn Apr 3, 2022
831e6da
Fix checkSafeOptionValue
shadowspawn Apr 3, 2022
de21b0f
Add some unit tests for new strict error for suspect option value
shadowspawn Apr 3, 2022
2afaebc
Static example usage
shadowspawn Apr 3, 2022
5fe37b7
Tweak parameter names in response to feedback
shadowspawn Apr 9, 2022
f9ff015
Merge branch 'main' into feature/greedy
shadowspawn Apr 10, 2022
05967d4
Merge branch 'main' into feature/greedy
shadowspawn Apr 15, 2022
a7556dd
Rename routine, use custom error
shadowspawn Apr 15, 2022
da9fbcf
Add short example, and use function style matching other PR in flight.
shadowspawn Apr 15, 2022
6439e99
Refactor tests and fix for new short message
shadowspawn Apr 15, 2022
ff03bcd
Switch to node compatible test format
shadowspawn Apr 15, 2022
bc77eb2
Add end-to-end greedy tests
shadowspawn Apr 15, 2022
3b7ac89
Merge branch 'main' into feature/greedy
shadowspawn Apr 15, 2022
8487167
Move checking routines together
shadowspawn Apr 15, 2022
65c0833
Merge branch 'main' into feature/greedy
shadowspawn Apr 16, 2022
120d95a
Merge remote-tracking branch 'upstream/main' into feature/greedy
shadowspawn Apr 17, 2022
62726b8
Tweak error
shadowspawn Apr 17, 2022
4362750
Go with ambiguous, matches having two informative messages.
shadowspawn Apr 17, 2022
0eac39d
Merge branch 'main' into feature/greedy
shadowspawn Apr 17, 2022
f4ceff3
Fix more tests for null prototype
shadowspawn Apr 17, 2022
22745ac
Merge branch 'main' into feature/greedy
bcoe Apr 18, 2022
f731241
Merge remote-tracking branch 'upstream/main' into feature/greedy
shadowspawn Apr 18, 2022
e477243
Merge branch 'feature/greedy' of github.com:shadowspawn/parseargs int…
shadowspawn Apr 18, 2022
836bd9a
Move greedy related tests into index.js
shadowspawn Apr 19, 2022
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
Prev Previous commit
Move greedy related tests into index.js
  • Loading branch information
shadowspawn committed Apr 19, 2022
commit 836bd9a003a6497b05b1bc3ac689b82fc49c0a4e
36 changes: 0 additions & 36 deletions test/greedy.js

This file was deleted.

134 changes: 134 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,137 @@ test('null prototype: when --toString then values.toString is true', () => {
const result = parseArgs({ args, options });
assert.deepStrictEqual(result, expectedResult);
});

const candidateGreedyOptions = [
'',
'-',
'--',
'abc',
'123',
'-s',
'--foo',
];

candidateGreedyOptions.forEach((value) => {
test(`greedy: when short option with value '${value}' then eaten`, () => {
const args = ['-w', value];
const options = { with: { type: 'string', short: 'w' } };
const expectedResult = { values: { __proto__: null, with: value }, positionals: [] };

const result = parseArgs({ args, options, strict: false });
assert.deepStrictEqual(result, expectedResult);
});

test(`greedy: when long option with value '${value}' then eaten`, () => {
const args = ['--with', value];
const options = { with: { type: 'string', short: 'w' } };
const expectedResult = { values: { __proto__: null, with: value }, positionals: [] };

const result = parseArgs({ args, options, strict: false });
assert.deepStrictEqual(result, expectedResult);
});
});

test('strict: when candidate option value is plain text then does not throw', () => {
const args = ['--with', 'abc'];
const options = { with: { type: 'string' } };
const expectedResult = { values: { __proto__: null, with: 'abc' }, positionals: [] };

const result = parseArgs({ args, options, strict: true });
assert.deepStrictEqual(result, expectedResult);
});

test("strict: when candidate option value is '-' then does not throw", () => {
const args = ['--with', '-'];
const options = { with: { type: 'string' } };
const expectedResult = { values: { __proto__: null, with: '-' }, positionals: [] };

const result = parseArgs({ args, options, strict: true });
assert.deepStrictEqual(result, expectedResult);
});

test("strict: when candidate option value is '--' then throws", () => {
const args = ['--with', '--'];
const options = { with: { type: 'string' } };

assert.throws(() => {
parseArgs({ args, options });
}, {
code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
});
});

test('strict: when candidate option value is short option then throws', () => {
const args = ['--with', '-a'];
const options = { with: { type: 'string' } };

assert.throws(() => {
parseArgs({ args, options });
}, {
code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
});
});

test('strict: when candidate option value is short option digit then throws', () => {
const args = ['--with', '-1'];
const options = { with: { type: 'string' } };

assert.throws(() => {
parseArgs({ args, options });
}, {
code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
});
});

test('strict: when candidate option value is long option then throws', () => {
const args = ['--with', '--foo'];
const options = { with: { type: 'string' } };

assert.throws(() => {
parseArgs({ args, options });
}, {
code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
});
});

test('strict: when short option and suspect value then throws with short option in error message', () => {
const args = ['-w', '--foo'];
const options = { with: { type: 'string', short: 'w' } };

assert.throws(() => {
parseArgs({ args, options });
}, /for '-w'/
);
});

test('strict: when long option and suspect value then throws with long option in error message', () => {
const args = ['--with', '--foo'];
const options = { with: { type: 'string' } };

assert.throws(() => {
parseArgs({ args, options });
}, /for '--with'/
);
});

test('strict: when short option and suspect value then throws with whole expected message', () => {
const args = ['-w', '--foo'];
const options = { with: { type: 'string', short: 'w' } };

assert.throws(() => {
parseArgs({ args, options });
// eslint-disable-next-line max-len
}, /Error: Option '-w' argument is ambiguous\.\nDid you forget to specify the option argument for '-w'\?\nOr to specify an option argument starting with a dash use '--with=-XYZ' or '-w-XYZ'\./
);
});

test('strict: when long option and suspect value then throws with whole expected message', () => {
const args = ['--with', '--foo'];
const options = { with: { type: 'string', short: 'w' } };

assert.throws(() => {
parseArgs({ args, options });
// eslint-disable-next-line max-len
}, /Error: Option '--with' argument is ambiguous\.\nDid you forget to specify the option argument for '--with'\?\nOr to specify an option argument starting with a dash use '--with=-XYZ'\./
);
});
110 changes: 0 additions & 110 deletions test/strict-value-check.js

This file was deleted.