Skip to content

Commit f05b0c8

Browse files
mdonnalleyk-capehart
andauthoredApr 8, 2024
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 <kyleacapehart@gmail.com>
1 parent d3e7b6b commit f05b0c8

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
 

‎src/parser/parse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class Parser<
173173
this.currentFlag = flag
174174
let input = isLong || arg.length < 3 ? this.argv.shift() : arg.slice(arg[2] === '=' ? 3 : 2)
175175

176-
if (flag.allowStdin === 'only' && input !== '-' && input !== undefined) {
176+
if (flag.allowStdin === 'only' && input !== '-' && input !== undefined && !this.findFlag(input).name) {
177177
throw new CLIError(
178178
`Flag --${name} can only be read from stdin. The value must be "-" or not provided at all.`,
179179
)

‎test/parser/parse.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1956,4 +1956,17 @@ describe('allowStdin', () => {
19561956
}
19571957
}
19581958
})
1959+
1960+
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 () => {
1961+
sandbox.stub(parser, 'readStdin').returns(stdinPromise)
1962+
const out = await parse(['--myflag', '--myflag2'], {
1963+
flags: {
1964+
myflag: Flags.string({allowStdin: 'only'}),
1965+
myflag2: Flags.boolean(),
1966+
},
1967+
})
1968+
1969+
expect(out.flags.myflag).to.equals(stdinValue)
1970+
expect(out.raw[0].input).to.equal('x')
1971+
})
19591972
})

0 commit comments

Comments
 (0)