Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'only' option to allowStdin #900

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/interfaces/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,11 @@ export type OptionFlagProps = FlagProps & {
*/
delimiter?: ','
/**
* Allow input value to be read from stdin.
* Allow input value to be read from stdin if the provided value is `-`.
* If set to `only`, the flag will only accept input from stdin.
* Should only be used on one flag at a time.
*/
allowStdin?: boolean
allowStdin?: boolean | 'only'
}

export type FlagParserContext = Command & {token: FlagToken}
Expand Down
4 changes: 4 additions & 0 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ export class Parser<
throw new CLIError(`Flag --${name} expects a value`)
}

if (flag.allowStdin === 'only' && input !== '-') {
throw new CLIError(`Flag --${name} can only be read from stdin. The value must be "-".`)
}

if (flag.allowStdin && input === '-') {
const stdin = await readStdin()
if (stdin) {
Expand Down
44 changes: 43 additions & 1 deletion test/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,7 @@ describe('allowStdin', () => {
sandbox.restore()
})

it('should read stdin as input for flag', async () => {
it('should read stdin as input for flag when value is "-"', async () => {
sandbox.stub(parser, 'readStdin').returns(stdinPromise)
const out = await parse(['--myflag', '-'], {
flags: {
Expand All @@ -1883,4 +1883,46 @@ describe('allowStdin', () => {
expect(out.flags.myflag).to.equals(stdinValue)
expect(out.raw[0].input).to.equal('x')
})

it('should not read stdin when value is not "-"', async () => {
sandbox.stub(parser, 'readStdin').returns(stdinPromise)
const out = await parse(['--myflag', 'foo'], {
flags: {
myflag: Flags.string({allowStdin: true}),
},
})

expect(out.flags.myflag).to.equals('foo')
expect(out.raw[0].input).to.equal('foo')
})

it('should read stdin as input for flag when allowStdin is "only" and when value is "-"', async () => {
sandbox.stub(parser, 'readStdin').returns(stdinPromise)
const out = await parse(['--myflag', '-'], {
flags: {
myflag: Flags.string({allowStdin: 'only'}),
},
})

expect(out.flags.myflag).to.equals(stdinValue)
expect(out.raw[0].input).to.equal('x')
})

it('should throw if allowStdin is "only" but value is not "-"', async () => {
sandbox.stub(parser, 'readStdin').returns(stdinPromise)
try {
await parse(['--myflag', 'INVALID'], {
flags: {
myflag: Flags.string({allowStdin: 'only'}),
},
})
expect.fail('Should have thrown an error')
} catch (error) {
if (error instanceof CLIError) {
expect(error.message).to.equal('Flag --myflag can only be read from stdin. The value must be "-".')
} else {
expect.fail('Should have thrown a CLIError')
}
}
})
})
Loading