From 4a41d16022f041d256eb8515e0c563f0fd5c8fbc Mon Sep 17 00:00:00 2001 From: John Gee Date: Tue, 25 Jan 2022 18:34:23 +1300 Subject: [PATCH] fix: support single dash as positional --- README.md | 2 +- index.js | 11 ++++++++--- test/index.js | 10 ++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e11fcfa..bf7884e 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,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 diff --git a/index.js b/index.js index ae746eb..41c1758 100644 --- a/index.js +++ b/index.js @@ -93,9 +93,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) diff --git a/test/index.js b/test/index.js index 1208a98..723f62f 100644 --- a/test/index.js +++ b/test/index.js @@ -47,6 +47,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('same arg is passed twice "withValue" and last value is recorded', function(t) { const passedArgs = ['--foo=a', '--foo', 'b']; const passedOptions = { withValue: ['foo'] };