Comparing version 1.0.1 to 1.0.2
export interface ParseArgsOptions { | ||
/** | ||
* The list of option names (can be shorthand or longhand) for which values aren't collected. | ||
* The list of option keys (can be shorthand or longhand) for which values aren't collected. | ||
*/ | ||
flags?: string[]; | ||
/** | ||
* The map from a single character shorthand to a multi-character longhand option name. | ||
* The map from a single character shorthand to a multi-character longhand option key. | ||
*/ | ||
@@ -22,11 +22,11 @@ shorthands?: { | ||
/** | ||
* Keys parsed from the args. | ||
* Map from an option key to the list of associated values. | ||
*/ | ||
[key: string]: string[] | true | undefined; | ||
/** | ||
* List of args that weren't associated with any key. | ||
* The list of args that weren't associated with any option. | ||
*/ | ||
'': string[]; | ||
/** | ||
* List of args after `'--'` arg, or `undefined` if there were no `'--'` arg. | ||
* The list of args after `'--'` arg, or `undefined` if there were no `'--'` arg. | ||
*/ | ||
@@ -36,3 +36,3 @@ '--'?: string[]; | ||
/** | ||
* Parses process arguments and returns a map from an option name to its value. | ||
* Parses process arguments and returns a map from an option key to its value. | ||
* | ||
@@ -39,0 +39,0 @@ * @param args Arguments retrieved by `process.argv.slice(2)`. |
18
index.js
@@ -6,3 +6,3 @@ "use strict"; | ||
/** | ||
* Parses process arguments and returns a map from an option name to its value. | ||
* Parses process arguments and returns a map from an option key to its value. | ||
* | ||
@@ -29,6 +29,6 @@ * @param args Arguments retrieved by `process.argv.slice(2)`. | ||
} | ||
// '--key' | ||
// '--foo' | ||
if (arg.charCodeAt(2) !== CHAR_CODE_MINUS) { | ||
key = arg.substring(2); | ||
if (flags === null || flags === void 0 ? void 0 : flags.includes(key)) { | ||
if (flags && flags.includes(key)) { | ||
result[key] = true; | ||
@@ -46,11 +46,11 @@ key = ''; | ||
for (var j = 1; j < argLength; j++) { | ||
key = arg.charAt(j); | ||
var longhand = shorthands === null || shorthands === void 0 ? void 0 : shorthands[key]; | ||
var shorthand = arg.charAt(j); | ||
var longhand = shorthands ? shorthands[shorthand] : undefined; | ||
if (!longhand && !keepShorthands) { | ||
// Unknown shorthand | ||
key = undefined; | ||
key = null; | ||
continue; | ||
} | ||
key = longhand || key; | ||
if (flags === null || flags === void 0 ? void 0 : flags.includes(key)) { | ||
key = longhand || shorthand; | ||
if (flags && (flags.includes(shorthand) || (longhand && flags.includes(longhand)))) { | ||
result[key] = true; | ||
@@ -66,3 +66,3 @@ key = ''; | ||
} | ||
if (key === undefined) { | ||
if (key === null) { | ||
continue; | ||
@@ -69,0 +69,0 @@ } |
{ | ||
"name": "argcat", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "The simplest CLI arguments parser.", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
<p align="center"> | ||
<a href="#readme"> | ||
<img src="./logo.png" alt="argcat" width="450"/> | ||
<img src="./argcat.png" alt="argcat" width="450"/> | ||
</a> | ||
@@ -63,6 +63,6 @@ </p> | ||
parseArgs(['--foo', 'bar']); | ||
// ⮕ { '--foo': 'bar' } | ||
// ⮕ { foo: ['bar'] } | ||
parseArgs(['--foo', 'bar'], { flags: ['foo'] }); | ||
// ⮕ { '--foo': true, '': ['bar'] } | ||
// ⮕ { foo: true, '': ['bar'] } | ||
``` | ||
@@ -103,2 +103,12 @@ | ||
Shorthand options can have values: | ||
```ts | ||
parseArgs(['-x', 'bar'], { keepShorthands: true }); | ||
// ⮕ { x: ['bar'] } | ||
parseArgs(['-abc', 'bar'], { keepShorthands: true }); | ||
// ⮕ { a: [], b: [], c: ['bar'] } | ||
``` | ||
# Commands | ||
@@ -118,3 +128,3 @@ | ||
```ts | ||
const command = result[''].pop(); | ||
const command = result[''].shift(); | ||
@@ -142,9 +152,14 @@ if (command === 'push') { | ||
const argsShape = d.object({ | ||
foo: d.number() | ||
}); | ||
// 1️⃣ Define the shape of a CLI options object | ||
const optionsShape = d | ||
.object({ | ||
age: d.number() | ||
}) | ||
.strip(); | ||
const options = argsShape.parse( | ||
// 🟡 This comes from process.argv.slice(2) | ||
const options = optionsShape.parse( | ||
// 2️⃣ Convert process.argv.slice(2) to options | ||
parseArgs(['--age', '42']), | ||
// 3️⃣ Enable type coercion | ||
{ coerce: true } | ||
@@ -151,0 +166,0 @@ ); |
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
11258
171