-
Notifications
You must be signed in to change notification settings - Fork 11
refactor!: rework results to remove redundant flags property and store value true for boolean options
#83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
d414a7b
f53e230
877b0ce
e355fd5
d12e027
e9bfed4
79bfb5c
167f9e7
9defcc3
2b56411
92ae729
a34fca9
7488e61
554ed54
0660d1d
52b2913
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,9 @@ | |
|
|
||
| [![Coverage][coverage-image]][coverage-url] | ||
|
|
||
| > | ||
| > | ||
| > 🚨 THIS REPO IS AN EARLY WIP -- DO NOT USE ... yet 🚨 | ||
| > | ||
| > | ||
|
|
||
| Polyfill of future proposal to the [nodejs/tooling](https://github.com/nodejs/tooling) repo for `util.parseArgs()` | ||
|
|
||
|
|
@@ -86,8 +86,7 @@ process.mainArgs = process.argv.slice(process._exec ? 1 : 2) | |
| * `short` {string} (Optional) A single character alias for an option; When appearing one or more times in `args`; Respects the `multiple` configuration | ||
| * `strict` {Boolean} (Optional) A `Boolean` on wheather or not to throw an error when unknown args are encountered | ||
| * Returns: {Object} An object having properties: | ||
| * `flags` {Object}, having properties and `Boolean` values corresponding to parsed options passed | ||
| * `values` {Object}, have properties and `String` values corresponding to parsed options passed | ||
| * `values` {Object}, key:value for each option found. Value is a string for string options, or `true` for boolean options, or an array for options configured as `multiple:true`. | ||
| * `positionals` {string[]}, containing [Positionals][] | ||
|
|
||
| ---- | ||
|
|
@@ -103,40 +102,37 @@ const { parseArgs } = require('@pkgjs/parseargs'); | |
| const { parseArgs } = require('@pkgjs/parseargs'); | ||
| const args = ['-f', '--foo=a', '--bar', 'b']; | ||
| const options = {}; | ||
| const { flags, values, positionals } = parseArgs({ args, options }); | ||
| // flags = { f: true, bar: true } | ||
| // values = { foo: 'a' } | ||
| const { values, positionals } = parseArgs({ args, options }); | ||
| // values = { f: true, foo: 'a', bar: true } | ||
| // positionals = ['b'] | ||
| ``` | ||
|
|
||
| ```js | ||
| const { parseArgs } = require('@pkgjs/parseargs'); | ||
| // withValue | ||
| // type:string | ||
| const args = ['-f', '--foo=a', '--bar', 'b']; | ||
| const options = { | ||
| foo: { | ||
| bar: { | ||
| type: 'string', | ||
| }, | ||
| }; | ||
| const { flags, values, positionals } = parseArgs({ args, options }); | ||
| // flags = { f: true } | ||
| // values = { foo: 'a', bar: 'b' } | ||
| const { values, positionals } = parseArgs({ args, options }); | ||
| // values = { f: true, foo: 'a', bar: 'b' } | ||
| // positionals = [] | ||
| ``` | ||
|
|
||
| ```js | ||
| const { parseArgs } = require('@pkgjs/parseargs'); | ||
| // withValue & multiple | ||
| // type:string & multiple | ||
| const args = ['-f', '--foo=a', '--foo', 'b']; | ||
| const options = { | ||
| foo: { | ||
| type: 'string', | ||
| multiple: true, | ||
| }, | ||
| }; | ||
| const { flags, values, positionals } = parseArgs({ args, options }); | ||
| // flags = { f: true } | ||
| // values = { foo: ['a', 'b'] } | ||
| const { values, positionals } = parseArgs({ args, options }); | ||
| // values = { f: true, foo: [ 'a', 'b' ] } | ||
| // positionals = [] | ||
| ``` | ||
|
|
||
|
|
@@ -149,9 +145,8 @@ const options = { | |
| short: 'f', | ||
| }, | ||
| }; | ||
| const { flags, values, positionals } = parseArgs({ args, options }); | ||
| // flags = { foo: true } | ||
| // values = {} | ||
| const { values, positionals } = parseArgs({ args, options }); | ||
| // values = { foo: true } | ||
| // positionals = ['b'] | ||
| ``` | ||
|
|
||
|
|
@@ -189,26 +184,29 @@ const { flags, values, positionals } = parseArgs({ args, options }); | |
| - `"0o22"` | ||
| - Does it coerce types? | ||
| - no | ||
| - Does `--no-foo` coerce to `--foo=false`? For all flags? Only boolean flags? | ||
| - no, it sets `{args:{'no-foo': true}}` | ||
| - Does `--no-foo` coerce to `--foo=false`? For all options? Only boolean options? | ||
| - no, it sets `{values:{'no-foo': true}}` | ||
|
Comment on lines
+188
to
+189
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unrelated to this PR, but it would be nice to have parseArgs handle "what if i do
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I note that's something you could trivially build on top of |
||
| - Is `--foo` the same as `--foo=true`? Only for known booleans? Only at the end? | ||
| - no, `--foo` is the same as `--foo=` | ||
| - no, `--foo` is a boolean option and `--foo=true` is a string option | ||
|
shadowspawn marked this conversation as resolved.
Outdated
|
||
| - Does it read environment variables? Ie, is `FOO=1 cmd` the same as `cmd --foo=1`? | ||
| - no | ||
| - Do unknown arguments raise an error? Are they parsed? Are they treated as positional arguments? | ||
| - no, they are parsed, not treated as positionals | ||
| - Does `--` signal the end of flags/options? | ||
| - **open question** | ||
| - 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']}`? | ||
| - Does `--` signal the end of options? | ||
| - yes | ||
| - Is `--` included as a positional? | ||
| - no | ||
| - Is `program -- foo` the same as `program foo`? | ||
| - yes, both store `{positionals:['foo']}` | ||
| - Does the API specify whether a `--` was present/relevant? | ||
| - no | ||
| - Is `-bar` the same as `--bar`? | ||
| - no, `-bar` is a short option or options, with expansion logic that follows the | ||
| [Utility Syntax Guidelines in POSIX.1-2017](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html). `-bar` expands to `-b`, `-a`, `-r`. | ||
| - Is `---foo` the same as `--foo`? | ||
| - no | ||
| - the first flag would be parsed as `'-foo'` | ||
| - the second flag would be parsed as `'foo'` | ||
| - the first is a long option named `'-foo'` | ||
| - the second is a long option named `'foo'` | ||
| - Is `-` a positional? ie, `bash some-test.sh | tap -` | ||
| - yes | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.