Skip to content

Commit d795bf8

Browse files
authored
fix: support single dash as positional (#49)
1 parent a2f36d7 commit d795bf8

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ const { flags, values, positionals } = parseArgs(argv, options);
201201
- the first flag would be parsed as `'-foo'`
202202
- the second flag would be parsed as `'foo'`
203203
- Is `-` a positional? ie, `bash some-test.sh | tap -`
204-
- no
204+
- yes
205205

206206
[coverage-image]: https://img.shields.io/nycrc/pkgjs/parseargs
207207
[coverage-url]: https://github.com/pkgjs/parseargs/blob/main/.nycrc

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,14 @@ const parseArgs = (
105105
let arg = argv[pos];
106106

107107
if (StringPrototypeStartsWith(arg, '-')) {
108-
// Everything after a bare '--' is considered a positional argument
109-
// and is returned verbatim
110-
if (arg === '--') {
108+
if (arg === '-') {
109+
// '-' commonly used to represent stdin/stdout, treat as positional
110+
result.positionals = ArrayPrototypeConcat(result.positionals, '-');
111+
++pos;
112+
continue;
113+
} else if (arg === '--') {
114+
// Everything after a bare '--' is considered a positional argument
115+
// and is returned verbatim
111116
result.positionals = ArrayPrototypeConcat(
112117
result.positionals,
113118
ArrayPrototypeSlice(argv, ++pos)

test/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ test('args equals are passed "withValue"', function(t) {
111111
t.end();
112112
});
113113

114+
test('when args include single dash then result stores dash as positional', function(t) {
115+
const passedArgs = ['-'];
116+
const expected = { flags: { }, values: { }, positionals: ['-'] };
117+
const args = parseArgs(passedArgs);
118+
119+
t.deepEqual(args, expected);
120+
121+
t.end();
122+
});
123+
114124
test('zero config args equals are parsed as if "withValue"', function(t) {
115125
const passedArgs = ['--so=wat'];
116126
const passedOptions = { };

0 commit comments

Comments
 (0)