Skip to content

Commit

Permalink
fix: support single dash as positional (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Feb 5, 2022
1 parent a2f36d7 commit d795bf8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ const { flags, values, positionals } = parseArgs(argv, options);
- the first flag would be parsed as `'-foo'`
- the second flag would be parsed as `'foo'`
- Is `-` a positional? ie, `bash some-test.sh | tap -`
- no
- yes

[coverage-image]: https://img.shields.io/nycrc/pkgjs/parseargs
[coverage-url]: https://github.com/pkgjs/parseargs/blob/main/.nycrc
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,14 @@ const parseArgs = (
let arg = argv[pos];

if (StringPrototypeStartsWith(arg, '-')) {
// Everything after a bare '--' is considered a positional argument
// and is returned verbatim
if (arg === '--') {
if (arg === '-') {
// '-' commonly used to represent stdin/stdout, treat as positional
result.positionals = ArrayPrototypeConcat(result.positionals, '-');
++pos;
continue;
} else if (arg === '--') {
// Everything after a bare '--' is considered a positional argument
// and is returned verbatim
result.positionals = ArrayPrototypeConcat(
result.positionals,
ArrayPrototypeSlice(argv, ++pos)
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ test('args equals are passed "withValue"', function(t) {
t.end();
});

test('when args include single dash then result stores dash as positional', function(t) {
const passedArgs = ['-'];
const expected = { flags: { }, values: { }, positionals: ['-'] };
const args = parseArgs(passedArgs);

t.deepEqual(args, expected);

t.end();
});

test('zero config args equals are parsed as if "withValue"', function(t) {
const passedArgs = ['--so=wat'];
const passedOptions = { };
Expand Down

0 comments on commit d795bf8

Please sign in to comment.