-
Notifications
You must be signed in to change notification settings - Fork 120
feature: add configuration option "collect-unknown-options" #181
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
10 commits
Select commit
Hold shift + click to select a range
b1b4200
feature: add configuration option "ignore-unknown-options"
henderea 5a31396
fix formatting and add a missing "let" keyword
henderea a49e4b9
don't coerce number, change to "parse-unknown-options: false", fix no…
henderea 487c62e
re-arrange tests
henderea a6a820a
test more than just numeric and boolean arguments
henderea d5641ac
implement code review feedback
henderea 727a019
clean up some stuff that i forgot to remove
henderea d3feaba
rename checkFlags to hasFlagsMatching, and handle short flags
henderea 267459b
extract the logic into an "isUnknownOption" function, and add some co…
henderea 61e6d11
update README.md
henderea 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
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 |
---|---|---|
|
@@ -26,7 +26,8 @@ function parse (args, opts) { | |
'set-placeholder-key': false, | ||
'halt-at-non-option': false, | ||
'strip-aliased': false, | ||
'strip-dashed': false | ||
'strip-dashed': false, | ||
'collect-unknown-options': false | ||
}, opts.configuration) | ||
var defaults = opts.default || {} | ||
var configObjects = opts.configObjects || [] | ||
|
@@ -142,8 +143,10 @@ function parse (args, opts) { | |
var next | ||
var value | ||
|
||
if (configuration['collect-unknown-options'] && isUnknownOption(arg)) { | ||
argv._.push(arg) | ||
// -- separated by = | ||
if (arg.match(/^--.+=/) || ( | ||
} else if (arg.match(/^--.+=/) || ( | ||
!configuration['short-option-groups'] && arg.match(/^-.+=/) | ||
)) { | ||
// Using [\s\S] instead of . because js doesn't support the | ||
|
@@ -757,6 +760,74 @@ function parse (args, opts) { | |
return isSet | ||
} | ||
|
||
function hasAnyFlag (key) { | ||
var isSet = false | ||
// XXX Switch to [].concat(...Object.values(flags)) once node.js 6 is dropped | ||
var toCheck = [].concat(...Object.keys(flags).map(k => flags[k])) | ||
|
||
toCheck.forEach(function (flag) { | ||
if (flag[key]) isSet = flag[key] | ||
}) | ||
|
||
return isSet | ||
} | ||
|
||
function hasFlagsMatching (arg, ...patterns) { | ||
var hasFlag = false | ||
var toCheck = [].concat(...patterns) | ||
toCheck.forEach(function (pattern) { | ||
var match = arg.match(pattern) | ||
if (match && hasAnyFlag(match[1])) { | ||
hasFlag = true | ||
} | ||
}) | ||
return hasFlag | ||
} | ||
|
||
// based on a simplified version of the short flag group parsing logic | ||
function hasAllShortFlags (arg) { | ||
// if this is a negative number, or doesn't start with a single hyphen, it's not a short flag group | ||
if (arg.match(negative) || !arg.match(/^-[^-]+/)) { return false } | ||
var hasAllFlags = true | ||
var letters = arg.slice(1).split('') | ||
var next | ||
for (var j = 0; j < letters.length; j++) { | ||
next = arg.slice(j + 2) | ||
|
||
if (!hasAnyFlag(letters[j])) { | ||
hasAllFlags = false | ||
break | ||
} | ||
|
||
if ((letters[j + 1] && letters[j + 1] === '=') || | ||
next === '-' || | ||
(/[A-Za-z]/.test(letters[j]) && /^-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) || | ||
(letters[j + 1] && letters[j + 1].match(/\W/))) { | ||
break | ||
} | ||
} | ||
return hasAllFlags | ||
} | ||
|
||
function isUnknownOption (arg) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this refactor makes me quite happy 😄 |
||
// ignore negative numbers | ||
if (arg.match(negative)) { return false } | ||
// if this is a short option group and all of them are configured, it isn't unknown | ||
if (hasAllShortFlags(arg)) { return false } | ||
// e.g. '--count=2' | ||
const flagWithEquals = /^-+([^=]+?)=[\s\S]*$/ | ||
// e.g. '-a' or '--arg' | ||
const normalFlag = /^-+([^=]+?)$/ | ||
// e.g. '-a-' | ||
const flagEndingInHyphen = /^-+([^=]+?)-$/ | ||
// e.g. '-abc123' | ||
const flagEndingInDigits = /^-+([^=]+?)\d+$/ | ||
// e.g. '-a/usr/local' | ||
const flagEndingInNonWordCharacters = /^-+([^=]+?)\W+.*$/ | ||
// check the different types of flag styles, including negatedBoolean, a pattern defined near the start of the parse method | ||
return !hasFlagsMatching(arg, flagWithEquals, negatedBoolean, normalFlag, flagEndingInHyphen, flagEndingInDigits, flagEndingInNonWordCharacters) | ||
} | ||
|
||
// make a best effor to pick a default value | ||
// for an option based on name and type. | ||
function defaultValue (key) { | ||
|
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.