From f05b0c8119853b8a9de62d9076a42d8d93a94046 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Mon, 8 Apr 2024 13:05:51 -0600 Subject: [PATCH] fix: do not throw an error if a flag with allowStdin='only' is immediately followed by another flag (#1046) (#1047) Co-authored-by: Kyle Capehart --- src/parser/parse.ts | 2 +- test/parser/parse.test.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/parser/parse.ts b/src/parser/parse.ts index 237447142..037b6521b 100644 --- a/src/parser/parse.ts +++ b/src/parser/parse.ts @@ -173,7 +173,7 @@ export class Parser< this.currentFlag = flag let input = isLong || arg.length < 3 ? this.argv.shift() : arg.slice(arg[2] === '=' ? 3 : 2) - if (flag.allowStdin === 'only' && input !== '-' && input !== undefined) { + if (flag.allowStdin === 'only' && input !== '-' && input !== undefined && !this.findFlag(input).name) { throw new CLIError( `Flag --${name} can only be read from stdin. The value must be "-" or not provided at all.`, ) diff --git a/test/parser/parse.test.ts b/test/parser/parse.test.ts index abcb3f178..c5022e4c5 100644 --- a/test/parser/parse.test.ts +++ b/test/parser/parse.test.ts @@ -1956,4 +1956,17 @@ describe('allowStdin', () => { } } }) + + it('should read stdin as input for flag when allowStdin is "only" and no value is given, and a second flag is used after', async () => { + sandbox.stub(parser, 'readStdin').returns(stdinPromise) + const out = await parse(['--myflag', '--myflag2'], { + flags: { + myflag: Flags.string({allowStdin: 'only'}), + myflag2: Flags.boolean(), + }, + }) + + expect(out.flags.myflag).to.equals(stdinValue) + expect(out.raw[0].input).to.equal('x') + }) })