-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: parsing, revisit short option groups, add support for comb…
…ined short and value (#75) 1) Refactor parsing to use independent blocks of code, rather than nested cascading context. This makes it easier to reason about the behaviour. 2) Split out small pieces of logic to named routines to improve readability, and allow extra documentation and examples without cluttering the parsing. (Thanks to @aaronccasanova for inspiration.) 3) Existing tests untouched to make it clear that the tested functionality has not changed. 4) Be more explicit about short option group expansion, and ready to throw error in strict mode for string option in the middle of the argument. (See #11 and #74.) 5) Add support for short option combined with value (without intervening `=`). This is what Commander and Open Group Utility Conventions do, but is _not_ what Yargs does. I don't want to block PR on this and happy to comment it out for further discussion if needed. (I have found some interesting variations in the wild.) [Edit: see also #78] 6) Add support for multiple unit tests files. Expand tests from 33 to 113, but many for internal routines rather than testing exposed API. 7) Added `.editorconfig` file Co-authored-by: Jordan Harband <[email protected]> Co-authored-by: Aaron Casanova <[email protected]>
- Loading branch information
1 parent
b412095
commit a92600f
Showing
15 changed files
with
872 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# EditorConfig is awesome: http://EditorConfig.org | ||
|
||
# top-most EditorConfig file | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
tab_width = 2 | ||
# trim_trailing_whitespace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
/* eslint max-len: 0 */ | ||
|
||
const test = require('tape'); | ||
const { parseArgs } = require('../index.js'); | ||
|
||
// The use of `-` as a positional is specifically mentioned in the Open Group Utility Conventions. | ||
// The interpretation is up to the utility, and for a file positional (operand) the examples are | ||
// '-' may stand for standard input (or standard output), or for a file named -. | ||
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html | ||
// | ||
// A different usage and example is `git switch -` to switch back to the previous branch. | ||
|
||
test("dash: when args include '-' used as positional then result has '-' in positionals", (t) => { | ||
const passedArgs = ['-']; | ||
const expected = { flags: {}, values: {}, positionals: ['-'] }; | ||
|
||
const result = parseArgs({ args: passedArgs }); | ||
|
||
t.deepEqual(result, expected); | ||
t.end(); | ||
}); | ||
|
||
// If '-' is a valid positional, it is symmetrical to allow it as an option value too. | ||
test("dash: when args include '-' used as space-separated option value then result has '-' in option value", (t) => { | ||
const passedArgs = ['-v', '-']; | ||
const passedOptions = { v: { type: 'string' } }; | ||
const expected = { flags: { v: true }, values: { v: '-' }, positionals: [] }; | ||
|
||
const result = parseArgs({ args: passedArgs, options: passedOptions }); | ||
|
||
t.deepEqual(result, expected); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
/* eslint max-len: 0 */ | ||
|
||
const test = require('tape'); | ||
const { findLongOptionForShort } = require('../utils.js'); | ||
|
||
test('findLongOptionForShort: when passed empty options then returns short', (t) => { | ||
t.equal(findLongOptionForShort('a', {}), 'a'); | ||
t.end(); | ||
}); | ||
|
||
test('findLongOptionForShort: when passed short not present in options then returns short', (t) => { | ||
t.equal(findLongOptionForShort('a', { foo: { short: 'f', type: 'string' } }), 'a'); | ||
t.end(); | ||
}); | ||
|
||
test('findLongOptionForShort: when passed short present in options then returns long', (t) => { | ||
t.equal(findLongOptionForShort('a', { alpha: { short: 'a' } }), 'alpha'); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
/* eslint max-len: 0 */ | ||
|
||
const test = require('tape'); | ||
const { isLoneLongOption } = require('../utils.js'); | ||
|
||
test('isLoneLongOption: when passed short option then returns false', (t) => { | ||
t.false(isLoneLongOption('-s')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneLongOption: when passed short option group then returns false', (t) => { | ||
t.false(isLoneLongOption('-abc')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneLongOption: when passed lone long option then returns true', (t) => { | ||
t.true(isLoneLongOption('--foo')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneLongOption: when passed single character long option then returns true', (t) => { | ||
t.true(isLoneLongOption('--f')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneLongOption: when passed long option and value then returns false', (t) => { | ||
t.false(isLoneLongOption('--foo=bar')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneLongOption: when passed empty string then returns false', (t) => { | ||
t.false(isLoneLongOption('')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneLongOption: when passed plain text then returns false', (t) => { | ||
t.false(isLoneLongOption('foo')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneLongOption: when passed single dash then returns false', (t) => { | ||
t.false(isLoneLongOption('-')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneLongOption: when passed double dash then returns false', (t) => { | ||
t.false(isLoneLongOption('--')); | ||
t.end(); | ||
}); | ||
|
||
// This is a bit bogus, but simple consistent behaviour: long option follows double dash. | ||
test('isLoneLongOption: when passed arg starting with triple dash then returns true', (t) => { | ||
t.true(isLoneLongOption('---foo')); | ||
t.end(); | ||
}); | ||
|
||
// This is a bit bogus, but simple consistent behaviour: long option follows double dash. | ||
test("isLoneLongOption: when passed '--=' then returns true", (t) => { | ||
t.true(isLoneLongOption('--=')); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
/* eslint max-len: 0 */ | ||
|
||
const test = require('tape'); | ||
const { isLoneShortOption } = require('../utils.js'); | ||
|
||
test('isLoneShortOption: when passed short option then returns true', (t) => { | ||
t.true(isLoneShortOption('-s')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneShortOption: when passed short option group (or might be short and value) then returns false', (t) => { | ||
t.false(isLoneShortOption('-abc')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneShortOption: when passed long option then returns false', (t) => { | ||
t.false(isLoneShortOption('--foo')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneShortOption: when passed long option with value then returns false', (t) => { | ||
t.false(isLoneShortOption('--foo=bar')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneShortOption: when passed empty string then returns false', (t) => { | ||
t.false(isLoneShortOption('')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneShortOption: when passed plain text then returns false', (t) => { | ||
t.false(isLoneShortOption('foo')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneShortOption: when passed single dash then returns false', (t) => { | ||
t.false(isLoneShortOption('-')); | ||
t.end(); | ||
}); | ||
|
||
test('isLoneShortOption: when passed double dash then returns false', (t) => { | ||
t.false(isLoneShortOption('--')); | ||
t.end(); | ||
}); |
Oops, something went wrong.