Skip to content

Commit

Permalink
fix: do not throw an error if a flag with allowStdin='only' is immedi…
Browse files Browse the repository at this point in the history
…ately followed by another flag (#1046) (#1047)

Co-authored-by: Kyle Capehart <kyleacapehart@gmail.com>
  • Loading branch information
mdonnalley and k-capehart authored Apr 8, 2024
1 parent d3e7b6b commit f05b0c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
)
Expand Down
13 changes: 13 additions & 0 deletions test/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

0 comments on commit f05b0c8

Please sign in to comment.