Skip to content

Commit be153db

Browse files
authored
refactor!: rework results to remove redundant flags property and store value true for boolean options (#83)
1 parent 084a23f commit be153db

File tree

7 files changed

+71
-95
lines changed

7 files changed

+71
-95
lines changed

README.md

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
[![Coverage][coverage-image]][coverage-url]
55

6-
>
6+
>
77
> 🚨 THIS REPO IS AN EARLY WIP -- DO NOT USE ... yet 🚨
8-
>
8+
>
99
1010
Polyfill of future proposal to the [nodejs/tooling](https://github.com/nodejs/tooling) repo for `util.parseArgs()`
1111

@@ -86,8 +86,7 @@ process.mainArgs = process.argv.slice(process._exec ? 1 : 2)
8686
* `short` {string} (Optional) A single character alias for an option; When appearing one or more times in `args`; Respects the `multiple` configuration
8787
* `strict` {Boolean} (Optional) A `Boolean` on wheather or not to throw an error when unknown args are encountered
8888
* Returns: {Object} An object having properties:
89-
* `flags` {Object}, having properties and `Boolean` values corresponding to parsed options passed
90-
* `values` {Object}, have properties and `String` values corresponding to parsed options passed
89+
* `values` {Object}, key:value for each option found. Value is a string for string options, or `true` for boolean options, or an array (of strings or booleans) for options configured as `multiple:true`.
9190
* `positionals` {string[]}, containing [Positionals][]
9291

9392
----
@@ -103,40 +102,37 @@ const { parseArgs } = require('@pkgjs/parseargs');
103102
const { parseArgs } = require('@pkgjs/parseargs');
104103
const args = ['-f', '--foo=a', '--bar', 'b'];
105104
const options = {};
106-
const { flags, values, positionals } = parseArgs({ args, options });
107-
// flags = { f: true, bar: true }
108-
// values = { foo: 'a' }
105+
const { values, positionals } = parseArgs({ args, options });
106+
// values = { f: true, foo: 'a', bar: true }
109107
// positionals = ['b']
110108
```
111109

112110
```js
113111
const { parseArgs } = require('@pkgjs/parseargs');
114-
// withValue
112+
// type:string
115113
const args = ['-f', '--foo=a', '--bar', 'b'];
116114
const options = {
117-
foo: {
115+
bar: {
118116
type: 'string',
119117
},
120118
};
121-
const { flags, values, positionals } = parseArgs({ args, options });
122-
// flags = { f: true }
123-
// values = { foo: 'a', bar: 'b' }
119+
const { values, positionals } = parseArgs({ args, options });
120+
// values = { f: true, foo: 'a', bar: 'b' }
124121
// positionals = []
125122
```
126123

127124
```js
128125
const { parseArgs } = require('@pkgjs/parseargs');
129-
// withValue & multiple
126+
// type:string & multiple
130127
const args = ['-f', '--foo=a', '--foo', 'b'];
131128
const options = {
132129
foo: {
133130
type: 'string',
134131
multiple: true,
135132
},
136133
};
137-
const { flags, values, positionals } = parseArgs({ args, options });
138-
// flags = { f: true }
139-
// values = { foo: ['a', 'b'] }
134+
const { values, positionals } = parseArgs({ args, options });
135+
// values = { f: true, foo: [ 'a', 'b' ] }
140136
// positionals = []
141137
```
142138

@@ -150,9 +146,8 @@ const options = {
150146
type: 'boolean'
151147
},
152148
};
153-
const { flags, values, positionals } = parseArgs({ args, options });
154-
// flags = { foo: true }
155-
// values = {}
149+
const { values, positionals } = parseArgs({ args, options });
150+
// values = { foo: true }
156151
// positionals = ['b']
157152
```
158153

@@ -190,26 +185,29 @@ const { flags, values, positionals } = parseArgs({ args, options });
190185
- `"0o22"`
191186
- Does it coerce types?
192187
- no
193-
- Does `--no-foo` coerce to `--foo=false`? For all flags? Only boolean flags?
194-
- no, it sets `{args:{'no-foo': true}}`
188+
- Does `--no-foo` coerce to `--foo=false`? For all options? Only boolean options?
189+
- no, it sets `{values:{'no-foo': true}}`
195190
- Is `--foo` the same as `--foo=true`? Only for known booleans? Only at the end?
196-
- no, `--foo` is the same as `--foo=`
191+
- no, they are not the same. There is no special handling of `true` as a value so it is just another string.
197192
- Does it read environment variables? Ie, is `FOO=1 cmd` the same as `cmd --foo=1`?
198193
- no
199194
- Do unknown arguments raise an error? Are they parsed? Are they treated as positional arguments?
200195
- no, they are parsed, not treated as positionals
201-
- Does `--` signal the end of flags/options?
202-
- **open question**
203-
- If `--` signals the end, is `--` included as a positional? is `program -- foo` the same as `program foo`? Are both `{positionals:['foo']}`, or is the first one `{positionals:['--', 'foo']}`?
196+
- Does `--` signal the end of options?
197+
- yes
198+
- Is `--` included as a positional?
199+
- no
200+
- Is `program -- foo` the same as `program foo`?
201+
- yes, both store `{positionals:['foo']}`
204202
- Does the API specify whether a `--` was present/relevant?
205203
- no
206204
- Is `-bar` the same as `--bar`?
207205
- no, `-bar` is a short option or options, with expansion logic that follows the
208206
[Utility Syntax Guidelines in POSIX.1-2017](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html). `-bar` expands to `-b`, `-a`, `-r`.
209207
- Is `---foo` the same as `--foo`?
210208
- no
211-
- the first flag would be parsed as `'-foo'`
212-
- the second flag would be parsed as `'foo'`
209+
- the first is a long option named `'-foo'`
210+
- the second is a long option named `'foo'`
213211
- Is `-` a positional? ie, `bash some-test.sh | tap -`
214212
- yes
215213

index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,24 @@ const protoKey = '__proto__';
7676
function storeOptionValue(options, longOption, value, result) {
7777
const optionConfig = options[longOption] || {};
7878

79-
// Flags
80-
result.flags[longOption] = true;
81-
8279
if (longOption === protoKey) {
8380
return;
8481
}
8582

8683
// Values
84+
const usedAsFlag = value === undefined;
85+
const newValue = usedAsFlag ? true : value;
8786
if (optionConfig.multiple) {
8887
// Always store value in array, including for flags.
8988
// result.values[longOption] starts out not present,
9089
// first value is added as new array [newValue],
9190
// subsequent values are pushed to existing array.
92-
const usedAsFlag = value === undefined;
93-
const newValue = usedAsFlag ? true : value;
9491
if (result.values[longOption] !== undefined)
9592
ArrayPrototypePush(result.values[longOption], newValue);
9693
else
9794
result.values[longOption] = [newValue];
9895
} else {
99-
result.values[longOption] = value;
96+
result.values[longOption] = newValue;
10097
}
10198
}
10299

@@ -132,7 +129,6 @@ const parseArgs = ({
132129
);
133130

134131
const result = {
135-
flags: {},
136132
values: {},
137133
positionals: []
138134
};

test/dash.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { parseArgs } = require('../index.js');
1313

1414
test("dash: when args include '-' used as positional then result has '-' in positionals", (t) => {
1515
const passedArgs = ['-'];
16-
const expected = { flags: {}, values: {}, positionals: ['-'] };
16+
const expected = { values: {}, positionals: ['-'] };
1717

1818
const result = parseArgs({ args: passedArgs });
1919

@@ -25,7 +25,7 @@ test("dash: when args include '-' used as positional then result has '-' in posi
2525
test("dash: when args include '-' used as space-separated option value then result has '-' in option value", (t) => {
2626
const passedArgs = ['-v', '-'];
2727
const passedOptions = { v: { type: 'string' } };
28-
const expected = { flags: { v: true }, values: { v: '-' }, positionals: [] };
28+
const expected = { values: { v: '-' }, positionals: [] };
2929

3030
const result = parseArgs({ args: passedArgs, options: passedOptions });
3131

0 commit comments

Comments
 (0)