Comparing version 1.0.12 to 1.0.13
export declare type ParseFn<T> = (input: string) => T; | ||
export interface IArg<T> { | ||
export interface IArg<T = string> { | ||
name: string; | ||
@@ -10,15 +10,21 @@ description?: string; | ||
} | ||
export declare type Arg<T> = { | ||
export declare type ArgBase<T> = { | ||
name?: string; | ||
description?: string; | ||
required?: boolean; | ||
optional?: boolean; | ||
hidden?: boolean; | ||
value: T; | ||
parse: ParseFn<T>; | ||
default?: T; | ||
}; | ||
export declare type RequiredArg<T> = ArgBase<T> & { | ||
required: true; | ||
value: T; | ||
}; | ||
export declare type OptionalArg<T> = ArgBase<T> & { | ||
required: false; | ||
value?: T; | ||
}; | ||
export declare type Arg<T> = RequiredArg<T> | OptionalArg<T>; | ||
export declare function newArg<T>(arg: IArg<T> & { | ||
Parse: ParseFn<T>; | ||
}): Arg<T>; | ||
export declare function newArg<T>(arg: IArg<T>): Arg<string>; | ||
export declare function newArg<T = string>(arg: IArg<string>): Arg<string>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function newArg(arg) { | ||
return Object.assign({ parse: (i) => i }, arg); | ||
return Object.assign({ parse: (i) => i }, arg, { required: !!arg.required }); | ||
} | ||
exports.newArg = newArg; |
@@ -20,7 +20,11 @@ import { AlphabetLowercase, AlphabetUppercase } from './alphabet'; | ||
parse: ParseFn<T>; | ||
multiple: boolean; | ||
} | ||
export interface ISingleOptionFlag<T> extends IOptionFlagBase<T> { | ||
export interface IOptionalFlag<T> extends IOptionFlagBase<T> { | ||
multiple: false; | ||
default?: T; | ||
value?: T; | ||
} | ||
export interface IRequiredFlag<T> extends IOptionFlagBase<T> { | ||
multiple: false; | ||
default?: undefined; | ||
value: T; | ||
@@ -33,6 +37,11 @@ } | ||
} | ||
export declare type IOptionFlag<T> = ISingleOptionFlag<T> | IMultiOptionFlag<T>; | ||
export declare type IOptionFlag<T> = IOptionalFlag<T> | IRequiredFlag<T> | IMultiOptionFlag<T>; | ||
export declare type FlagBuilder<T> = { | ||
(options: Partial<IMultiOptionFlag<T>>): IMultiOptionFlag<T>; | ||
(options?: Partial<ISingleOptionFlag<T>>): ISingleOptionFlag<T>; | ||
(options: Partial<IMultiOptionFlag<T>> & { | ||
multiple: true; | ||
}): IMultiOptionFlag<T>; | ||
(options: Partial<IRequiredFlag<T>> & { | ||
required: true; | ||
}): IRequiredFlag<T>; | ||
(options?: Partial<IOptionalFlag<T>>): IOptionalFlag<T>; | ||
}; | ||
@@ -46,4 +55,6 @@ export declare function option<T>(defaults: (Partial<IOptionFlag<T>>) & { | ||
integer: FlagBuilder<number>; | ||
option: <T>(defaults: (Partial<ISingleOptionFlag<T>> & { | ||
option: <T>(defaults: (Partial<IOptionalFlag<T>> & { | ||
parse: ParseFn<T>; | ||
}) | (Partial<IRequiredFlag<T>> & { | ||
parse: ParseFn<T>; | ||
}) | (Partial<IMultiOptionFlag<T>> & { | ||
@@ -50,0 +61,0 @@ parse: ParseFn<T>; |
import { IArg, Arg } from './args'; | ||
import { IBooleanFlag, IFlag } from './flags'; | ||
export interface InputFlags { | ||
import { defaultFlags, IFlag } from './flags'; | ||
export declare type InputArgs = IArg<any>[]; | ||
export declare type InputFlags = { | ||
[name: string]: IFlag<any>; | ||
} | ||
export declare type InputArgs = IArg<any>[]; | ||
export declare type parserInput<T extends InputFlags> = { | ||
argv: string[]; | ||
flags: T; | ||
args: Arg<any>[]; | ||
strict: boolean; | ||
}; | ||
export declare type ParserInput<T extends InputFlags> = { | ||
argv?: string[]; | ||
flags?: T; | ||
args?: InputArgs; | ||
strict?: boolean; | ||
}; | ||
export declare type DefaultFlags = { | ||
color: IBooleanFlag; | ||
[P in keyof typeof defaultFlags]: typeof defaultFlags[P]['value']; | ||
}; | ||
export declare type outputFlags<T extends InputFlags> = { | ||
[P in keyof T]: T[P]['value']; | ||
}; | ||
export declare type OutputFlags<T extends InputFlags> = outputFlags<T> & DefaultFlags; | ||
export declare type OutputArgs = { | ||
[k: string]: string; | ||
[k: string]: any; | ||
}; | ||
export declare type ParserOutput<T extends InputFlags> = { | ||
export declare type OutputFlags<T extends InputFlags | undefined> = { | ||
[P in keyof T]: T[P]['value']; | ||
} & DefaultFlags; | ||
export declare type ParserOutput<T extends InputFlags | undefined> = { | ||
flags: OutputFlags<T>; | ||
args: OutputArgs; | ||
args: { | ||
[k: string]: any; | ||
}; | ||
argv: string[]; | ||
@@ -45,2 +34,14 @@ raw: ParsingToken[]; | ||
export declare type ParsingToken = ArgToken | FlagToken; | ||
export declare function parse<T extends InputFlags>(options: ParserInput<T>): ParserOutput<T>; | ||
export declare type ParserInput<T extends InputFlags | undefined> = { | ||
argv?: string[]; | ||
flags?: T; | ||
args?: InputArgs; | ||
strict?: boolean; | ||
}; | ||
export declare function parse<T extends InputFlags | undefined>(options: ParserInput<T>): ParserOutput<T>; | ||
export declare type parserInput = { | ||
argv: string[]; | ||
flags: InputFlags; | ||
args: Arg<any>[]; | ||
strict: boolean; | ||
}; |
@@ -11,3 +11,3 @@ "use strict"; | ||
argv: options.argv || process.argv.slice(2), | ||
flags: Object.assign({ color: flags_1.defaultFlags.color }, options.flags || {}), | ||
flags: Object.assign({ color: flags_1.defaultFlags.color }, (options.flags || {})), | ||
strict: options.strict !== false, | ||
@@ -135,2 +135,3 @@ }; | ||
} | ||
flag.value = flags[token.flag]; | ||
} | ||
@@ -137,0 +138,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { DefaultFlags, InputFlags, parserInput, ParserOutput } from './parse'; | ||
export declare function validate<T extends InputFlags & DefaultFlags>(expected: parserInput<T>, input: ParserOutput<T & DefaultFlags>): void; | ||
import { parserInput, ParserOutput } from './parse'; | ||
export declare function validate(expected: parserInput, input: ParserOutput<any>): void; |
@@ -12,3 +12,3 @@ "use strict"; | ||
} | ||
const requiredArgs = expected.args.filter(a => a.required || a.optional === false); | ||
const requiredArgs = expected.args.filter(a => a.required); | ||
const missingRequiredArgs = requiredArgs.slice(input.length); | ||
@@ -20,7 +20,4 @@ if (missingRequiredArgs.length) { | ||
function validateFlags(expected) { | ||
const requiredFlags = Object.keys(expected.flags) | ||
.map(k => [k, expected.flags[k]]) | ||
.filter(([, flag]) => flag.required); | ||
for (const [, flag] of requiredFlags) { | ||
if (flag.value === undefined) { | ||
for (const flag of Object.values(expected.flags)) { | ||
if (flag.required && flag.value === undefined) { | ||
throw new required_flag_1.RequiredFlagError(flag); | ||
@@ -27,0 +24,0 @@ } |
{ | ||
"name": "cli-flags", | ||
"description": "basic CLI flag parser", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"author": "Jeff Dickey", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/jdxcode/cli-flags/issues", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
17245
448
0