Comparing version 1.0.1 to 1.0.2
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const cli_1 = require("../cli"); | ||
const list_1 = require("cli-ux/lib/list"); | ||
class RequiredArgsError extends Error { | ||
@@ -9,3 +9,3 @@ constructor(args) { | ||
if (namedArgs.length) { | ||
const list = cli_1.renderList(namedArgs.map(a => [a.name, a.description])); | ||
const list = list_1.renderList(namedArgs.map(a => [a.name, a.description])); | ||
msg += `:\n${list}`; | ||
@@ -12,0 +12,0 @@ } |
@@ -0,3 +1,3 @@ | ||
export { args, IArg } from './args'; | ||
export { parse } from './parse'; | ||
export { flags } from './flags'; | ||
export { args } from './args'; | ||
export { flags, Flag, ValueFlag } from './flags'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var args_1 = require("./args"); | ||
exports.args = args_1.args; | ||
var parse_1 = require("./parse"); | ||
@@ -7,3 +9,3 @@ exports.parse = parse_1.parse; | ||
exports.flags = flags_1.flags; | ||
var args_1 = require("./args"); | ||
exports.args = args_1.args; | ||
exports.Flag = flags_1.Flag; | ||
exports.ValueFlag = flags_1.ValueFlag; |
@@ -12,3 +12,10 @@ import { IArg } from './args'; | ||
output?: 'object' | 'array'; | ||
strict?: boolean; | ||
} | ||
export declare type InputOptions = IInputOptions & { | ||
argv: string[]; | ||
flags: InputFlags; | ||
args: InputArgs; | ||
strict: boolean; | ||
}; | ||
export interface IOutputArg { | ||
@@ -15,0 +22,0 @@ type: 'arg'; |
@@ -60,3 +60,3 @@ "use strict"; | ||
const arg = argv.shift(); | ||
if (arg.startsWith('-')) { | ||
if (parsingFlags && arg.startsWith('-')) { | ||
// attempt to parse as arg | ||
@@ -83,10 +83,3 @@ if (arg === '--') { | ||
} | ||
function parse(options) { | ||
const input = Object.assign({ args: [], argv: process.argv.slice(2), flags: {} }, options); | ||
setNames(input.flags); | ||
const arr = parseArray(input); | ||
validate_1.validate(input.args, input.flags, arr); | ||
if (input.output === 'array') { | ||
return arr; | ||
} | ||
function buildOutputFromArray(arr) { | ||
return arr.reduce((obj, elem) => { | ||
@@ -108,5 +101,4 @@ switch (elem.type) { | ||
obj.argv.push(elem.input); | ||
const { name } = input.args[elem.i]; | ||
if (name) { | ||
obj.args[name] = elem.input; | ||
if (elem.arg) { | ||
obj.args[elem.arg.name] = elem.input; | ||
} | ||
@@ -122,2 +114,12 @@ break; | ||
} | ||
function parse(options) { | ||
const input = Object.assign({ args: [], argv: process.argv.slice(2), flags: {} }, options, { strict: options.strict !== false }); | ||
setNames(input.flags); | ||
const arr = parseArray(input); | ||
validate_1.validate(input, arr); | ||
if (input.output === 'array') { | ||
return arr; | ||
} | ||
return buildOutputFromArray(arr); | ||
} | ||
exports.parse = parse; |
@@ -1,2 +0,2 @@ | ||
import { InputArgs, InputFlags, OutputArray } from './parse'; | ||
export declare function validate(expectedArgs: InputArgs, expectedFlags: InputFlags, input: OutputArray): void; | ||
import { InputOptions, OutputArray } from './parse'; | ||
export declare function validate(expected: InputOptions, input: OutputArray): void; |
@@ -7,8 +7,8 @@ "use strict"; | ||
function validateArgs(expected, input) { | ||
const maxArgs = expected.length; | ||
if (input.length > maxArgs) { | ||
const maxArgs = expected.args.length; | ||
if (expected.strict && input.length > maxArgs) { | ||
const extras = input.slice(maxArgs); | ||
throw new unexpected_args_1.UnexpectedArgsError(extras); | ||
} | ||
const requiredArgs = expected.filter(a => a.required); | ||
const requiredArgs = expected.args.filter(a => a.required); | ||
const missingRequiredArgs = requiredArgs.slice(input.length); | ||
@@ -20,4 +20,4 @@ if (missingRequiredArgs.length) { | ||
function validateFlags(expected, input) { | ||
const requiredFlags = Object.keys(expected) | ||
.map(k => [k, expected[k]]) | ||
const requiredFlags = Object.keys(expected.flags) | ||
.map(k => [k, expected.flags[k]]) | ||
.filter(([name, flag]) => flag.required); | ||
@@ -31,8 +31,8 @@ for (const [name, flag] of requiredFlags) { | ||
} | ||
function validate(expectedArgs, expectedFlags, input) { | ||
function validate(expected, input) { | ||
const args = input.filter(a => a.type === 'arg'); | ||
const flags = input.filter(a => a.type === 'flag'); | ||
validateArgs(expectedArgs, args); | ||
validateFlags(expectedFlags, flags); | ||
validateArgs(expected, args); | ||
validateFlags(expected, flags); | ||
} | ||
exports.validate = validate; |
{ | ||
"name": "cli-flags", | ||
"description": "basic CLI flag parser", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Jeff Dickey", | ||
"bugs": "https://github.com/jdxcode/cli-flags/issues", | ||
"dependencies": { | ||
"@heroku/linewrap": "^1.0.0", | ||
"lodash.maxby": "^4.6.0", | ||
"lodash.padend": "^4.6.1" | ||
"cli-ux": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^20.0.8", | ||
"@types/lodash.maxby": "^4.6.3", | ||
"@types/lodash.padend": "^4.6.3", | ||
"@types/node": "^8.0.28", | ||
@@ -53,2 +49,3 @@ "husky": "^0.14.3", | ||
"prettier --write", | ||
"tslint --fix", | ||
"git add", | ||
@@ -55,0 +52,0 @@ "jest --bail --findRelatedTests" |
@@ -32,2 +32,7 @@ cli-flags | ||
console.log(`input arg: ${args.input}`) | ||
// $ node example.js -f myinput --output-file=myexample.txt | ||
// --force was set | ||
// output file is: myexample.txt | ||
// input arg: myinput | ||
``` |
1
13
38
15368
30
411
+ Addedcli-ux@^1.1.1
+ Addedansi-escapes@3.2.04.3.2(transitive)
+ Addedansi-regex@3.0.1(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedansicolors@0.2.1(transitive)
+ Addedcardinal@1.0.0(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcli-ux@1.1.13(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedcross-spawn@7.0.6(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedesprima@3.0.0(transitive)
+ Addedfs-extra@4.0.3(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas-flag@2.0.03.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjsonfile@4.0.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedpassword-prompt@1.1.3(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedredeyed@1.0.1(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedstrip-ansi@4.0.0(transitive)
+ Addedsupports-color@4.5.05.5.0(transitive)
+ Addedts-lodash@4.0.11(transitive)
+ Addedtype-fest@0.21.3(transitive)
+ Addeduniversalify@0.1.2(transitive)
+ Addedwhich@2.0.2(transitive)
- Removed@heroku/linewrap@^1.0.0
- Removedlodash.maxby@^4.6.0
- Removedlodash.padend@^4.6.1
- Removedlodash.maxby@4.6.0(transitive)
- Removedlodash.padend@4.6.1(transitive)