Comparing version 1.0.13 to 1.0.14
@@ -1,2 +0,7 @@ | ||
export declare type ParseFn<T> = (input: string) => T; | ||
export declare type ArgParseContext<T = string> = { | ||
arg: Arg<T>; | ||
} & { | ||
[k: string]: any; | ||
}; | ||
export declare type ParseFn<T> = (input: string, parseContext: ArgParseContext<T>) => T; | ||
export interface IArg<T = string> { | ||
@@ -7,4 +12,4 @@ name: string; | ||
hidden?: boolean; | ||
parse?: (input: string) => T; | ||
default?: T; | ||
parse?: ParseFn<T>; | ||
default?: T | (() => T); | ||
} | ||
@@ -16,3 +21,4 @@ export declare type ArgBase<T> = { | ||
parse: ParseFn<T>; | ||
default?: T; | ||
default?: T | (() => T); | ||
input?: string; | ||
}; | ||
@@ -19,0 +25,0 @@ export declare type RequiredArg<T> = ArgBase<T> & { |
import { AlphabetLowercase, AlphabetUppercase } from './alphabet'; | ||
import { ParserInput } from './parse'; | ||
export interface IFlagBase { | ||
name?: string; | ||
name: string; | ||
char?: AlphabetLowercase | AlphabetUppercase; | ||
@@ -13,13 +14,20 @@ description?: string; | ||
value: boolean; | ||
input: string; | ||
} | ||
export declare type ParseFn<T> = (input: string, options: { | ||
export declare type ParseContext = { | ||
[k: string]: any; | ||
}) => T; | ||
}; | ||
export declare type FlagParseContext<T, PC extends ParseContext> = { | ||
flag: IOptionFlag<T>; | ||
input: ParserInput; | ||
} & PC; | ||
export declare type ParseFn<T, PC = any> = (input: string, parseContext: FlagParseContext<T, PC>) => T; | ||
export interface IOptionFlagBase<T> extends IFlagBase { | ||
type: 'option'; | ||
parse: ParseFn<T>; | ||
input: string[]; | ||
} | ||
export interface IOptionalFlag<T> extends IOptionFlagBase<T> { | ||
multiple: false; | ||
default?: T; | ||
default?: T | ((context: ParseContext) => T); | ||
value?: T; | ||
@@ -47,5 +55,3 @@ } | ||
}; | ||
export declare function option<T>(defaults: (Partial<IOptionFlag<T>>) & { | ||
parse: ParseFn<T>; | ||
}): FlagBuilder<T>; | ||
export declare function option<T = string>(defaults?: Partial<IOptionFlag<T>>): FlagBuilder<T>; | ||
export declare type IFlag<T> = IBooleanFlag | IOptionFlag<T>; | ||
@@ -55,9 +61,3 @@ export declare const flags: { | ||
integer: FlagBuilder<number>; | ||
option: <T>(defaults: (Partial<IOptionalFlag<T>> & { | ||
parse: ParseFn<T>; | ||
}) | (Partial<IRequiredFlag<T>> & { | ||
parse: ParseFn<T>; | ||
}) | (Partial<IMultiOptionFlag<T>> & { | ||
parse: ParseFn<T>; | ||
})) => FlagBuilder<T>; | ||
option: <T = string>(defaults?: Partial<IOptionalFlag<T>> | Partial<IRequiredFlag<T>> | Partial<IMultiOptionFlag<T>>) => FlagBuilder<T>; | ||
string: FlagBuilder<string>; | ||
@@ -64,0 +64,0 @@ }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function option(defaults) { | ||
function option(defaults = {}) { | ||
return (options) => { | ||
options = options || {}; | ||
return Object.assign({}, defaults, options, { multiple: !!options.multiple, type: 'option', value: options.multiple ? [] : undefined }); | ||
return Object.assign({ parse: (i) => i }, defaults, options, { input: [], multiple: !!options.multiple, type: 'option', value: options.multiple ? [] : undefined }); | ||
}; | ||
@@ -22,3 +22,3 @@ } | ||
option, | ||
string: option({ parse: input => input }), | ||
string: option(), | ||
}; | ||
@@ -25,0 +25,0 @@ exports.defaultFlags = { |
export { IArg } from './args'; | ||
export { parse, ParserInput, ParserOutput, InputArgs, InputFlags, OutputArgs, OutputFlags, DefaultFlags } from './parse'; | ||
export { ParserOutput, OutputArgs, OutputFlags, DefaultFlags } from './parse'; | ||
export { flags, IFlag, option } from './flags'; | ||
export { flagUsages } from './help'; | ||
import { IArg } from './args'; | ||
import { IFlag } from './flags'; | ||
import { ParserOutput } from './parse'; | ||
export declare type InputArgs = IArg<any>[]; | ||
export declare type InputFlags = { | ||
[name: string]: IFlag<any>; | ||
}; | ||
export declare type ParserInput<T extends InputFlags | undefined> = { | ||
argv?: string[]; | ||
flags?: T; | ||
args?: InputArgs; | ||
strict?: boolean; | ||
parseContext?: { | ||
[k: string]: any; | ||
}; | ||
}; | ||
export declare function parse<T extends InputFlags | undefined>(options: ParserInput<T>): ParserOutput<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var parse_1 = require("./parse"); | ||
exports.parse = parse_1.parse; | ||
var flags_1 = require("./flags"); | ||
exports.flags = flags_1.flags; | ||
exports.option = flags_1.option; | ||
var help_1 = require("./help"); | ||
exports.flagUsages = help_1.flagUsages; | ||
const deps_1 = require("./deps"); | ||
function parse(options) { | ||
const input = { | ||
args: (options.args || []).map(a => deps_1.deps.args.newArg(a)), | ||
argv: options.argv || process.argv.slice(2), | ||
flags: Object.assign({ color: deps_1.deps.flags.defaultFlags.color }, (options.flags || {})), | ||
parseContext: options.parseContext || {}, | ||
strict: options.strict !== false, | ||
}; | ||
const parser = new deps_1.deps.parse.Parser(input); | ||
const output = parser.parse(); | ||
deps_1.deps.validate.validate({ input, output }); | ||
return output; | ||
} | ||
exports.parse = parse; |
@@ -1,7 +0,4 @@ | ||
import { IArg, Arg } from './args'; | ||
import { defaultFlags, IFlag } from './flags'; | ||
export declare type InputArgs = IArg<any>[]; | ||
export declare type InputFlags = { | ||
[name: string]: IFlag<any>; | ||
}; | ||
import { Arg } from './args'; | ||
import { defaultFlags } from './flags'; | ||
import { InputFlags } from '.'; | ||
export declare type DefaultFlags = { | ||
@@ -34,10 +31,3 @@ [P in keyof typeof defaultFlags]: typeof defaultFlags[P]['value']; | ||
export declare type ParsingToken = ArgToken | FlagToken; | ||
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 = { | ||
export declare type ParserInput = { | ||
argv: string[]; | ||
@@ -47,2 +37,26 @@ flags: InputFlags; | ||
strict: boolean; | ||
parseContext: { | ||
[k: string]: any; | ||
}; | ||
}; | ||
export declare class Parser { | ||
readonly input: ParserInput; | ||
private argv; | ||
private raw; | ||
private booleanFlags; | ||
constructor(input: ParserInput); | ||
parse(): { | ||
args: OutputArgs; | ||
argv: any[]; | ||
flags: any; | ||
raw: ParsingToken[]; | ||
}; | ||
private _args(argv); | ||
private _flags(); | ||
private _argv(); | ||
private _debugOutput(args, flags, argv); | ||
private _debugInput(); | ||
private readonly _argTokens; | ||
private readonly _flagTokens; | ||
private _setNames(); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ts_lodash_1 = require("ts-lodash"); | ||
const args_1 = require("./args"); | ||
const flags_1 = require("./flags"); | ||
const validate_1 = require("./validate"); | ||
function parse(options) { | ||
const input = { | ||
args: (options.args || []).map(a => args_1.newArg(a)), | ||
argv: options.argv || process.argv.slice(2), | ||
flags: Object.assign({ color: flags_1.defaultFlags.color }, (options.flags || {})), | ||
strict: options.strict !== false, | ||
}; | ||
const parser = new Parser(input); | ||
const output = parser.parse(); | ||
validate_1.validate(input, output); | ||
return output; | ||
} | ||
exports.parse = parse; | ||
const debug = require('debug')('cli-flags'); | ||
class Parser { | ||
@@ -29,2 +14,3 @@ constructor(input) { | ||
parse() { | ||
this._debugInput(); | ||
const findLongFlag = (arg) => { | ||
@@ -72,5 +58,7 @@ const name = arg.slice(2); | ||
} | ||
flag.input.push(input); | ||
this.raw.push({ type: 'flag', flag: flag.name, input }); | ||
} | ||
else { | ||
flag.input = arg; | ||
this.raw.push({ type: 'flag', flag: flag.name, input: arg }); | ||
@@ -95,9 +83,15 @@ if (!long && arg.length > 2) { | ||
} | ||
const arg = this.input.args[this._argTokens.length]; | ||
if (arg) | ||
arg.input = input; | ||
this.raw.push({ type: 'arg', input }); | ||
} | ||
const argv = this._argv(); | ||
const args = this._args(argv); | ||
const flags = this._flags(); | ||
this._debugOutput(argv, args, flags); | ||
return { | ||
args: this._args(argv), | ||
args, | ||
argv, | ||
flags: this._flags(), | ||
flags, | ||
raw: this.raw, | ||
@@ -129,3 +123,3 @@ }; | ||
else { | ||
const value = flag.parse(token.input, {}); | ||
const value = flag.parse(token.input, Object.assign({ flag }, this.input.parseContext)); | ||
if (flag.multiple) { | ||
@@ -144,3 +138,9 @@ flags[token.flag] = flags[token.flag] || []; | ||
if (flag.type === 'option' && flag.default) { | ||
flags[k] = flag.default; | ||
if (typeof flag.default === 'function') { | ||
flags[k] = flag.default({ flag, input: this.input }); | ||
} | ||
else { | ||
flags[k] = flag.default; | ||
} | ||
flag.value = flags[k]; | ||
} | ||
@@ -158,7 +158,18 @@ } | ||
if (token) { | ||
args[i] = arg ? arg.parse(token.input) : token.input; | ||
if (arg) { | ||
args[i] = arg.parse(token.input, Object.assign({ arg }, this.input.parseContext)); | ||
} | ||
else { | ||
args[i] = token.input; | ||
} | ||
} | ||
else { | ||
if (arg.default) | ||
args[i] = arg.default; | ||
if (arg.default) { | ||
if (typeof arg.default === 'function') { | ||
args[i] = arg.default(); | ||
} | ||
else { | ||
args[i] = arg.default; | ||
} | ||
} | ||
} | ||
@@ -168,2 +179,24 @@ } | ||
} | ||
_debugOutput(args, flags, argv) { | ||
if (argv.length) { | ||
debug('argv: %o', argv); | ||
} | ||
if (Object.keys(args).length) { | ||
debug('args: %o', args); | ||
} | ||
if (Object.keys(flags).length) { | ||
debug('flags: %o', flags); | ||
} | ||
} | ||
_debugInput() { | ||
debug('input: %s', this.argv.join(' ')); | ||
if (this.input.args.length) { | ||
debug('available args: %s', this.input.args.map(a => a.name).join(' ')); | ||
} | ||
if (Object.keys(this.input.flags).length) { | ||
debug('available flags: %s', Object.keys(this.input.flags) | ||
.map(f => `--${f}`) | ||
.join(' ')); | ||
} | ||
} | ||
get _argTokens() { | ||
@@ -181,1 +214,2 @@ return this.raw.filter(o => o.type === 'arg'); | ||
} | ||
exports.Parser = Parser; |
@@ -1,2 +0,6 @@ | ||
import { parserInput, ParserOutput } from './parse'; | ||
export declare function validate(expected: parserInput, input: ParserOutput<any>): void; | ||
import { ParserInput, ParserOutput } from './parse'; | ||
import { InputFlags } from '.'; | ||
export declare function validate<T extends InputFlags>(parse: { | ||
input: ParserInput; | ||
output: ParserOutput<T>; | ||
}): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const required_args_1 = require("./errors/required_args"); | ||
const required_flag_1 = require("./errors/required_flag"); | ||
const unexpected_args_1 = require("./errors/unexpected_args"); | ||
function validateArgs(expected, input) { | ||
const maxArgs = expected.args.length; | ||
if (expected.strict && input.length > maxArgs) { | ||
const extras = input.slice(maxArgs); | ||
throw new unexpected_args_1.UnexpectedArgsError(extras); | ||
const errors_1 = require("./errors"); | ||
function validate(parse) { | ||
function validateArgs() { | ||
const maxArgs = parse.input.args.length; | ||
if (parse.input.strict && parse.output.argv.length > maxArgs) { | ||
const extras = parse.output.argv.slice(maxArgs); | ||
throw new errors_1.UnexpectedArgsError({ parse, args: extras }); | ||
} | ||
const requiredArgs = parse.input.args.filter(a => a.required); | ||
const missingRequiredArgs = requiredArgs.slice(parse.output.argv.length); | ||
if (missingRequiredArgs.length) { | ||
throw new errors_1.RequiredArgsError({ parse, args: missingRequiredArgs }); | ||
} | ||
} | ||
const requiredArgs = expected.args.filter(a => a.required); | ||
const missingRequiredArgs = requiredArgs.slice(input.length); | ||
if (missingRequiredArgs.length) { | ||
throw new required_args_1.RequiredArgsError(missingRequiredArgs); | ||
function validateFlags() { | ||
const flags = Object.values(parse.input.flags).filter(f => f.required && f.value === undefined); | ||
if (flags.length) | ||
throw new errors_1.RequiredFlagError({ parse, flags }); | ||
} | ||
validateArgs(); | ||
validateFlags(); | ||
} | ||
function validateFlags(expected) { | ||
for (const flag of Object.values(expected.flags)) { | ||
if (flag.required && flag.value === undefined) { | ||
throw new required_flag_1.RequiredFlagError(flag); | ||
} | ||
} | ||
} | ||
function validate(expected, input) { | ||
const args = input.raw.filter(a => a.type === 'arg'); | ||
validateArgs(expected, args); | ||
validateFlags(expected); | ||
} | ||
exports.validate = validate; |
{ | ||
"name": "cli-flags", | ||
"description": "basic CLI flag parser", | ||
"version": "1.0.13", | ||
"version": "1.0.14", | ||
"author": "Jeff Dickey", | ||
"bugs": "https://github.com/jdxcode/cli-flags/issues", | ||
"dependencies": { | ||
"cli-ux": "^1.1.7", | ||
"chalk": "^2.1.0", | ||
"cli-ux": "^1.1.8", | ||
"lodash": "^4.17.4", | ||
@@ -13,2 +14,3 @@ "ts-lodash": "^4.0.5" | ||
"devDependencies": { | ||
"@types/chalk": "^0.4.31", | ||
"@types/jest": "^20.0.8", | ||
@@ -20,7 +22,7 @@ "@types/lodash": "^4.14.74", | ||
"husky": "^0.14.3", | ||
"jest": "^21.0.2", | ||
"lint-staged": "^4.1.3", | ||
"prettier": "^1.6.1", | ||
"jest": "^21.1.0", | ||
"lint-staged": "^4.2.1", | ||
"prettier": "^1.7.0", | ||
"remap-istanbul": "^0.9.5", | ||
"ts-jest": "^21.0.0", | ||
"ts-jest": "^21.0.1", | ||
"ts-node": "^3.3.0", | ||
@@ -27,0 +29,0 @@ "tslint": "^5.7.0", |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
22964
612
4
16
20
2
5
+ Addedchalk@^2.1.0
Updatedcli-ux@^1.1.8