Comparing version 2.0.0-1 to 2.0.0-2
@@ -1,2 +0,2 @@ | ||
import { AliasArgRef, ArgRef, PrimitiveConstructor, OptionsReaderState, OptionsReaderNext, OptionResult } from './types'; | ||
import { AliasArgRef, ArgRef, PrimitiveConstructor, OptionsReaderState, OptionsReaderRead, OptionResult } from './types'; | ||
/** | ||
@@ -16,3 +16,3 @@ * Describe argument with aliases. | ||
*/ | ||
export declare function option<T extends string, K extends PrimitiveConstructor>(argRef: ArgRef<T>, type: K): (option: string, next: OptionsReaderNext, options: OptionsReaderState) => OptionResult<T, K> | null; | ||
export declare function option<T extends string, K extends PrimitiveConstructor>(argRef: ArgRef<T>, type: K): (option: string, read: OptionsReaderRead, options: OptionsReaderState) => OptionResult<T, K> | null; | ||
//# sourceMappingURL=args.d.ts.map |
@@ -104,7 +104,7 @@ const ARGV_START_INDEX = 2; | ||
if (type === String) { | ||
return (option1, next)=>{ | ||
return (option1, read)=>{ | ||
const argName = matchArgName(argRef, option1); | ||
if (argName) { | ||
return { | ||
[argName]: next(true) | ||
[argName]: read() | ||
}; | ||
@@ -116,7 +116,7 @@ } | ||
if (type === Number) { | ||
return (option2, next)=>{ | ||
return (option2, read)=>{ | ||
const argName = matchArgName(argRef, option2); | ||
if (argName) { | ||
return { | ||
[argName]: parseFloat(next(true)) | ||
[argName]: parseFloat(read()) | ||
}; | ||
@@ -128,7 +128,7 @@ } | ||
if (type === Array) { | ||
return (option3, next, options)=>{ | ||
return (option3, read, options)=>{ | ||
const argName = matchArgName(argRef, option3); | ||
if (argName) { | ||
const prevValues = options[argName]; | ||
const values = next(true).split(','); | ||
const values = read().split(','); | ||
return { | ||
@@ -163,3 +163,3 @@ [argName]: Array.isArray(prevValues) ? prevValues.concat(values) : values | ||
} | ||
return (option, next, options)=>readOption(option, next, options) ?? readNextOption(option, next, options) | ||
return (option, read, options)=>readOption(option, read, options) ?? readNextOption(option, read, options) | ||
; | ||
@@ -174,12 +174,23 @@ }, null); | ||
*/ function readOptions(...optionReaders) { | ||
if (!argv.length || !optionReaders.length) { | ||
if (!argv.length) { | ||
return {}; | ||
} | ||
const readOption = createOptionReader(optionReaders); | ||
if (!readOption) { | ||
return {}; | ||
} | ||
const options = {}; | ||
let i = 0; | ||
let arg = argv[i]; | ||
const next = (required = false)=>{ | ||
let optionResult; | ||
const next = ()=>{ | ||
arg = argv[++i]; | ||
if (required && !arg) { | ||
}; | ||
const remove = ()=>{ | ||
argv.splice(i--, 1); | ||
}; | ||
const read = ()=>{ | ||
next(); | ||
remove(); | ||
if (!arg) { | ||
throw new Error('Unexpected end of arguments'); | ||
@@ -192,3 +203,7 @@ } | ||
if (isOption(arg)) { | ||
Object.assign(options, readOption === null || readOption === void 0 ? void 0 : readOption(removePrefix(arg), next, options)); | ||
optionResult = readOption(removePrefix(arg), read, options); | ||
if (optionResult) { | ||
remove(); | ||
Object.assign(options, optionResult); | ||
} | ||
} | ||
@@ -195,0 +210,0 @@ next(); |
@@ -9,7 +9,7 @@ export declare type SimpleArgRef<T extends string> = T; | ||
export declare type OptionsReaderState = Record<string, unknown>; | ||
export declare type OptionsReaderNext = <T extends boolean>(required?: T) => (T extends true ? string : string | undefined); | ||
export declare type OptionsReaderRead = () => string; | ||
export declare type PrimitiveConstructor = StringConstructor | NumberConstructor | ArrayConstructor | BooleanConstructor; | ||
export declare type OptionValueType<T extends PrimitiveConstructor> = T extends StringConstructor ? string : T extends NumberConstructor ? number : T extends ArrayConstructor ? string[] : boolean; | ||
export declare type OptionResult<T extends string, K extends PrimitiveConstructor> = Record<T, OptionValueType<K>>; | ||
export declare type OptionReader<T extends OptionResult<string, PrimitiveConstructor> = OptionResult<string, PrimitiveConstructor>> = (option: string, next: OptionsReaderNext, options: OptionsReaderState) => T | null; | ||
export declare type OptionResult<T extends string = string, K extends PrimitiveConstructor = PrimitiveConstructor> = Record<T, OptionValueType<K>>; | ||
export declare type OptionReader<T extends OptionResult = OptionResult> = (option: string, read: OptionsReaderRead, options: OptionsReaderState) => T | null; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "argue-cli", | ||
"type": "module", | ||
"version": "2.0.0-1", | ||
"version": "2.0.0-2", | ||
"description": "A thin and strongly typed CLI argument parser for Node.js.", | ||
@@ -6,0 +6,0 @@ "author": "dangreen", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
47099
517