-
Notifications
You must be signed in to change notification settings - Fork 10
refactor!: parsing, revisit short option groups, add support for combined short and value #75
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
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
2734d65
Support multiple unit test files
shadowspawn 96f1447
Add isPossibleOptionValue
shadowspawn 9dfd7b6
Add .editorconfig so editor knows about lint settings
shadowspawn 638e07c
Add isLoneShortOption
shadowspawn c8c9e0c
Add isLongOption
shadowspawn 04d4d95
Add separate dash tests
shadowspawn 512afe5
Update signature for running new tests to arrow functions
shadowspawn 83f0e02
isShortOptionGroup
shadowspawn 686cbe3
Merge branch 'main' into feature/refactor-parse
shadowspawn bc45095
Update to new calling signature
shadowspawn 0a9c04c
Add findLongOptionForShort
shadowspawn 042d957
Start updating main parsing loop, and rework some utils.
shadowspawn 8f85ecd
Switch loop to shift
shadowspawn cb93bfa
Add isShortOptionAndValue
shadowspawn 4683053
Form expanded, clearer
shadowspawn aeff889
Fixes
shadowspawn 0c399bc
Improve comments
shadowspawn db3e06e
New tests for short option group (and fixes)
shadowspawn 3a7ea3c
Add tests for combining short and value
shadowspawn bb22e5a
Update package.json
shadowspawn eac5ecf
Update utils.js
shadowspawn 419060c
Update utils.js
shadowspawn 11bd06f
Add import
shadowspawn 1c1d047
Add exports to keep utils private
shadowspawn 3fcdcb3
AAA: Arrange, Act, Assert
shadowspawn cf48248
Add tests for failure duck typing
shadowspawn d2a1bc4
Add another dash example
shadowspawn 22fd538
Make test for undefined more robust
shadowspawn a409102
Update utils.js
shadowspawn 1bf4cfb
Update utils.js
shadowspawn 228056b
Update utils.js
shadowspawn bc6dae7
Update package.json
shadowspawn 6153fd2
Update index.js
shadowspawn eff783e
Comment improvements
shadowspawn 90f9864
Update index.js
shadowspawn 6013dc4
Update test/dash.js
shadowspawn e4b4f28
Expand test description per feedback
shadowspawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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('--=')); | ||
shadowspawn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.end(); | ||
}); |
This file contains hidden or 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(); | ||
}); | ||
shadowspawn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.