clef-parse
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -41,27 +41,37 @@ "use strict"; | ||
else { | ||
const itemWithoutLeadingDashes = item.replace(/^-{1,2}/, ""); | ||
let propertyName; | ||
if (item.length === 2) { | ||
propertyName = item[1]; | ||
let rightHandValue; | ||
let valueComesFromNextArg; | ||
if (/=/.test(itemWithoutLeadingDashes)) { | ||
let equalsOffset = itemWithoutLeadingDashes.indexOf("="); | ||
const before = itemWithoutLeadingDashes.slice(0, equalsOffset); | ||
const after = itemWithoutLeadingDashes.slice(equalsOffset + 1); | ||
propertyName = (0, convert_case_1.convertToCamelCase)(before); | ||
rightHandValue = after; | ||
valueComesFromNextArg = false; | ||
} | ||
else { | ||
if (!item.startsWith("--")) { | ||
throw new Error(`Invalid command-line flag: '${item}'. Single-character command-line flags should only have one dash before them, and multi-character command-line flags should have two dashes before them. If you want to pass '${item}' as a positional argument, place it after a '--'.`); | ||
} | ||
propertyName = (0, convert_case_1.convertToCamelCase)(item.replace(/^--/, "")); | ||
propertyName = (0, convert_case_1.convertToCamelCase)(itemWithoutLeadingDashes); | ||
rightHandValue = argv[0]; | ||
valueComesFromNextArg = true; | ||
} | ||
let propertyValue; | ||
let propertyHint = hints[propertyName]; | ||
const nextValue = argv[0]; | ||
if (propertyHint == null) { | ||
propertyHint = bestGuess(nextValue); | ||
propertyHint = bestGuess(rightHandValue); | ||
} | ||
switch (propertyHint) { | ||
case Boolean: { | ||
if (nextValue === "false") { | ||
argv.shift(); | ||
if (rightHandValue === "false") { | ||
if (valueComesFromNextArg) { | ||
argv.shift(); | ||
} | ||
propertyValue = false; | ||
} | ||
else { | ||
if (nextValue === "true") { | ||
argv.shift(); | ||
if (rightHandValue === "true") { | ||
if (valueComesFromNextArg) { | ||
argv.shift(); | ||
} | ||
} | ||
@@ -73,16 +83,22 @@ propertyValue = true; | ||
case Number: { | ||
argv.shift(); | ||
propertyValue = Number(nextValue); | ||
if (valueComesFromNextArg) { | ||
argv.shift(); | ||
} | ||
propertyValue = Number(rightHandValue); | ||
break; | ||
} | ||
case String: { | ||
argv.shift(); | ||
propertyValue = nextValue; | ||
if (valueComesFromNextArg) { | ||
argv.shift(); | ||
} | ||
propertyValue = rightHandValue; | ||
break; | ||
} | ||
case exports.Path: { | ||
argv.shift(); | ||
propertyValue = isAbsolute(nextValue) | ||
? nextValue | ||
: resolvePath(getCwd(), nextValue); | ||
if (valueComesFromNextArg) { | ||
argv.shift(); | ||
} | ||
propertyValue = isAbsolute(rightHandValue) | ||
? rightHandValue | ||
: resolvePath(getCwd(), rightHandValue); | ||
break; | ||
@@ -89,0 +105,0 @@ } |
{ | ||
"name": "clef-parse", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Simple, lightweight argv parser. Powers the cleffa and clefairy packages", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"bin": "dist/cli.js", | ||
"scripts": { | ||
"build": "rm -rf dist/* && tsc --skipLibCheck", | ||
"build": "rm -rf dist/* && tsc --skipLibCheck && chmod +x dist/cli.js", | ||
"test": "vitest" | ||
@@ -10,0 +11,0 @@ }, |
@@ -5,4 +5,92 @@ # clef-parse | ||
## Usage | ||
### As a node.js library | ||
```ts | ||
import { parseArgv } from "clef-parse"; | ||
// Sample argv | ||
const result = parseArgv([ | ||
"--bundle", | ||
"--input", | ||
"index.js", | ||
"--output", | ||
"bundle.js", | ||
"-v", | ||
]); | ||
console.log(result); | ||
// Logs: | ||
// { | ||
// options: { bundle: true, input: 'index.js', output: 'bundle.js', v: true }, | ||
// positionalArgs: [] | ||
// } | ||
// Optionally, you can provide type hints that will help the parser coerce values: | ||
const result2 = parseArgv( | ||
["--bundle", "--input", "index.js", "--output", "bundle.js", "-v"], | ||
{ | ||
bundle: Boolean, | ||
input: String, | ||
output: String, | ||
v: Boolean, | ||
} | ||
); | ||
console.log(result2); | ||
// Logs: | ||
// { | ||
// options: { bundle: true, input: 'index.js', output: 'bundle.js', v: true }, | ||
// positionalArgs: [] | ||
// } | ||
// Valid hints are Number, Boolean, String, or Path. Number, Boolean, and String are the standard JS globals, but Path is a value exported by the library: | ||
import { Path } from "clef-parse"; | ||
// When you provide the Path hint, clef-parse will convert the input string into an absolute path: | ||
const result3 = parseArgv( | ||
["--bundle", "--input", "index.js", "--output", "bundle.js", "-v"], | ||
{ | ||
bundle: Boolean, | ||
input: Path, | ||
output: Path, | ||
v: Boolean, | ||
} | ||
); | ||
console.log(result3); | ||
// Logs (for example): | ||
// { | ||
// options: { | ||
// bundle: true, | ||
// input: '/home/suchipi/Code/clef-parse/index.js', | ||
// output: '/home/suchipi/Code/clef-parse/bundle.js', | ||
// v: true | ||
// }, | ||
// positionalArgs: [] | ||
// } | ||
// If you don't provide hints, clef-parse will do its best to guess. | ||
``` | ||
See the TypeScript types in the source code for more information. | ||
### As a CLI tool | ||
```sh | ||
$ npm install -g clef-parse | ||
$ clef-parse one two --three-four=five | ||
{ | ||
"options": { | ||
"threeFour": "five" | ||
}, | ||
"positionalArgs": [ | ||
"one", | ||
"two" | ||
] | ||
} | ||
``` | ||
You can't specify hints with the CLI tool. | ||
## License | ||
MIT |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
10480
10
180
96
1