tiny-parse-argv
Advanced tools
Comparing version 2.5.1 to 2.6.0
@@ -103,3 +103,3 @@ /* IMPORT */ | ||
}; | ||
const parseValue = (key, value, booleans, strings) => { | ||
const parseValue = (key, value, booleans, integers, numbers, strings) => { | ||
if (booleans.has(key)) { | ||
@@ -111,2 +111,14 @@ if (value === 'true') | ||
} | ||
if (integers.has(key)) { | ||
const integer = Number(value); | ||
if (Number.isInteger(integer)) | ||
return integer; | ||
return null; | ||
} | ||
if (numbers.has(key)) { | ||
const number = Number(value); | ||
if (!Number.isNaN(number)) | ||
return number; | ||
return null; | ||
} | ||
if (!strings.has(key)) { | ||
@@ -124,2 +136,4 @@ const numberRe = /^0[xX][0-9a-fA-F]+$|^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][-+]?\d+)?$/; | ||
const booleans = getAliasedSet(aliases, options.boolean); | ||
const integers = getAliasedSet(aliases, options.integer); | ||
const numbers = getAliasedSet(aliases, options.number); | ||
const strings = getAliasedSet(aliases, options.string); | ||
@@ -131,3 +145,3 @@ const eagers = getAliasedSet(aliases, options.eager); | ||
const required = options.required || []; | ||
const known = new Set([...booleans, ...strings, ...Object.keys(defaults)]); | ||
const known = new Set([...booleans, ...integers, ...numbers, ...strings, ...Object.keys(defaults)]); | ||
const found = []; | ||
@@ -149,3 +163,3 @@ const onIncompatible = options.onIncompatible; | ||
if (isOverridable(parsed[key])) { // Maybe we are setting this option multiple times | ||
if (!strings.has(key)) { // String options shouldn't have an inferred value | ||
if (!integers.has(key) && !numbers.has(key) && !strings.has(key)) { // String options shouldn't have an inferred value | ||
const variadic = variadics.has(key); | ||
@@ -161,13 +175,17 @@ const value = variadic ? [positive] : positive; | ||
else { // Value or Argument | ||
const value = parseValue(optionPrev, arg, booleans, strings); | ||
const value = parseValue(optionPrev, arg, booleans, integers, numbers, strings); | ||
if (optionPrev && (!booleans.has(optionPrev) || isBoolean(value))) { // Regular value | ||
const variadic = variadics.has(optionPrev); | ||
setAliased(parsed, optionPrev, value, variadic, aliases); | ||
if (value !== null) { | ||
const variadic = variadics.has(optionPrev); | ||
setAliased(parsed, optionPrev, value, variadic, aliases); | ||
} | ||
} | ||
else if (optionEagerPrev && !booleans.has(optionEagerPrev)) { // Eager value | ||
const variadic = variadics.has(optionEagerPrev); | ||
setAliased(parsed, optionEagerPrev, value, variadic, aliases); | ||
if (value !== null) { | ||
const variadic = variadics.has(optionEagerPrev); | ||
setAliased(parsed, optionEagerPrev, value, variadic, aliases); | ||
} | ||
} | ||
else { // Argument | ||
parsed._.push(String(value)); | ||
parsed._.push(String(value ?? arg)); | ||
optionEagerPrev = ''; | ||
@@ -174,0 +192,0 @@ } |
type Options = { | ||
boolean?: string[]; | ||
integer?: string[]; | ||
number?: string[]; | ||
string?: string[]; | ||
@@ -4,0 +6,0 @@ eager?: string[]; |
@@ -5,3 +5,3 @@ { | ||
"description": "A tiny function for parsing process.argv, a modern rewrite of a sensible subset of minimist.", | ||
"version": "2.5.1", | ||
"version": "2.6.0", | ||
"type": "module", | ||
@@ -8,0 +8,0 @@ "main": "dist/index.js", |
@@ -18,2 +18,4 @@ # Tiny Parse Argv | ||
- `options.boolean`: the value for the listed flags will always be coerced to a boolean. | ||
- `options.integer`: the value for the listed flags will always be coerced to a integer. | ||
- `options.number`: the value for the listed flags will always be coerced to a number. | ||
- `options.string`: the value for the listed flags will always be coerced to a string. | ||
@@ -20,0 +22,0 @@ - `options.eager`: the listed flags are considered to be eager, and will consume multiple consecutive non-flag values. |
@@ -187,3 +187,3 @@ | ||
const parseValue = ( key: string, value: string, booleans: Set<string>, strings: Set<string> ): string | number | boolean => { | ||
const parseValue = ( key: string, value: string, booleans: Set<string>, integers: Set<string>, numbers: Set<string>, strings: Set<string> ): string | number | boolean | null => { | ||
@@ -198,2 +198,22 @@ if ( booleans.has ( key ) ) { | ||
if ( integers.has ( key ) ) { | ||
const integer = Number ( value ); | ||
if ( Number.isInteger ( integer ) ) return integer; | ||
return null; | ||
} | ||
if ( numbers.has ( key ) ) { | ||
const number = Number ( value ); | ||
if ( !Number.isNaN ( number ) ) return number; | ||
return null; | ||
} | ||
if ( !strings.has ( key ) ) { | ||
@@ -221,2 +241,4 @@ | ||
const booleans = getAliasedSet ( aliases, options.boolean ); | ||
const integers = getAliasedSet ( aliases, options.integer ); | ||
const numbers = getAliasedSet ( aliases, options.number ); | ||
const strings = getAliasedSet ( aliases, options.string ); | ||
@@ -228,3 +250,3 @@ const eagers = getAliasedSet ( aliases, options.eager ); | ||
const required = options.required || []; | ||
const known = new Set ([ ...booleans, ...strings, ...Object.keys ( defaults ) ]); | ||
const known = new Set ([ ...booleans, ...integers, ...numbers, ...strings, ...Object.keys ( defaults ) ]); | ||
const found: string[] = []; | ||
@@ -254,3 +276,3 @@ const onIncompatible = options.onIncompatible; | ||
if ( !strings.has ( key ) ) { // String options shouldn't have an inferred value | ||
if ( !integers.has ( key ) && !numbers.has ( key ) && !strings.has ( key ) ) { // String options shouldn't have an inferred value | ||
@@ -273,19 +295,27 @@ const variadic = variadics.has ( key ); | ||
const value = parseValue ( optionPrev, arg, booleans, strings ); | ||
const value = parseValue ( optionPrev, arg, booleans, integers, numbers, strings ); | ||
if ( optionPrev && ( !booleans.has ( optionPrev ) || isBoolean ( value ) ) ) { // Regular value | ||
const variadic = variadics.has ( optionPrev ); | ||
if ( value !== null ) { | ||
setAliased ( parsed, optionPrev, value, variadic, aliases ); | ||
const variadic = variadics.has ( optionPrev ); | ||
setAliased ( parsed, optionPrev, value, variadic, aliases ); | ||
} | ||
} else if ( optionEagerPrev && !booleans.has ( optionEagerPrev ) ) { // Eager value | ||
const variadic = variadics.has ( optionEagerPrev ); | ||
if ( value !== null ) { | ||
setAliased ( parsed, optionEagerPrev, value, variadic, aliases ); | ||
const variadic = variadics.has ( optionEagerPrev ); | ||
setAliased ( parsed, optionEagerPrev, value, variadic, aliases ); | ||
} | ||
} else { // Argument | ||
parsed._.push ( String ( value ) ); | ||
parsed._.push ( String ( value ?? arg ) ); | ||
@@ -292,0 +322,0 @@ optionEagerPrev = ''; |
@@ -8,2 +8,4 @@ | ||
boolean?: string[], | ||
integer?: string[], | ||
number?: string[], | ||
string?: string[], | ||
@@ -10,0 +12,0 @@ eager?: string[], |
@@ -85,3 +85,3 @@ | ||
it ( 'supports detecting invalid flags', t => { | ||
it ( 'supports detecting invalid integer flags', t => { | ||
@@ -91,2 +91,42 @@ t.plan ( 2 ); | ||
parse ( t, { | ||
input: ['--foo', '56.123', '--bar', 'abc', '--no-baz'], | ||
options: { | ||
integer: ['foo', 'bar', 'baz'], | ||
onInvalid ( flags ) { | ||
t.deepEqual ( flags, ['foo', 'bar', 'baz'] ); | ||
} | ||
}, | ||
output: { | ||
_: [], | ||
'--': [] | ||
} | ||
}); | ||
}); | ||
it ( 'supports detecting invalid number flags', t => { | ||
t.plan ( 2 ); | ||
parse ( t, { | ||
input: ['--foo', '--bar', 'abc', '--no-baz'], | ||
options: { | ||
number: ['foo', 'bar', 'baz'], | ||
onInvalid ( flags ) { | ||
t.deepEqual ( flags, ['foo', 'bar', 'baz'] ); | ||
} | ||
}, | ||
output: { | ||
_: [], | ||
'--': [] | ||
} | ||
}); | ||
}); | ||
it ( 'supports detecting invalid string flags', t => { | ||
t.plan ( 2 ); | ||
parse ( t, { | ||
input: ['--foo', '--bar', '--no-baz'], | ||
@@ -249,2 +289,58 @@ options: { | ||
it ( 'supports explicit integers', t => { | ||
parse ( t, { | ||
input: ['-n', '0001234'], | ||
options: { | ||
integer: ['n'] | ||
}, | ||
output: { | ||
n: 1234, | ||
_: [], | ||
'--': [] | ||
} | ||
}); | ||
parse ( t, { | ||
input: ['-n', '56'], | ||
options: { | ||
integer: ['n'] | ||
}, | ||
output: { | ||
n: 56, | ||
_: [], | ||
'--': [] | ||
} | ||
}); | ||
}); | ||
it ( 'supports explicit numbers', t => { | ||
parse ( t, { | ||
input: ['-n', '0001234'], | ||
options: { | ||
number: ['n'] | ||
}, | ||
output: { | ||
n: 1234, | ||
_: [], | ||
'--': [] | ||
} | ||
}); | ||
parse ( t, { | ||
input: ['-n', '56.123'], | ||
options: { | ||
number: ['n'] | ||
}, | ||
output: { | ||
n: 56.123, | ||
_: [], | ||
'--': [] | ||
} | ||
}); | ||
}); | ||
it ( 'defaults are expanded to aliases', t => { | ||
@@ -251,0 +347,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56348
1887
65
0