From d795bf877d068fd67aec381f30b30b63f97109ad Mon Sep 17 00:00:00 2001 From: John Gee Date: Sun, 6 Feb 2022 03:55:21 +1300 Subject: [PATCH] fix: support single dash as positional (#49) --- 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 ba69c48..f050690 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 60e14ec..84882e7 100644 --- a/index.js +++ b/index.js @@ -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) diff --git a/test/index.js b/test/index.js index 7a0aafc..50957f2 100644 --- a/test/index.js +++ b/test/index.js @@ -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 = { };