Skip to content

Commit c27a2d5

Browse files
committed
feat(breadc): parse boolean options value
1 parent 0495f11 commit c27a2d5

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

packages/breadc/src/parser/parser.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function parseOption(
3636
): TreeNode | false {
3737
const o = token.option();
3838
const [key, rawV] = o.split('=');
39+
3940
if (context.options.has(key)) {
4041
const option = context.options.get(key)!;
4142
const name = camelCase(option.name);
@@ -45,7 +46,17 @@ export function parseOption(
4546
return option.parse(cursor, token, context);
4647
} else if (option.type === 'boolean') {
4748
// Parse boolean option
48-
context.result.options[name] = !key.startsWith('no-') ? true : false;
49+
const negative = key.startsWith('no-');
50+
if (
51+
rawV === undefined ||
52+
['true', 'yes', 't', 'y'].includes(rawV.toLowerCase())
53+
) {
54+
context.result.options[name] = !negative ? true : false;
55+
} else if (['false', 'no', 'f', 'n'].includes(rawV.toLowerCase())) {
56+
context.result.options[name] = !negative ? false : true;
57+
} else {
58+
throw new ParseError(`Unexpected value ${rawV} for ${option.format}`);
59+
}
4960
} else if (option.type === 'string') {
5061
// Parse string option
5162
if (rawV !== undefined) {

packages/breadc/test/parse.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,82 @@ describe('Option Parser', () => {
491491
`);
492492
});
493493

494+
it('should parse boolean option with value', async () => {
495+
const cli = breadc('cli');
496+
cli.option('--open');
497+
cli.command('').action((o) => o);
498+
499+
expect(await cli.run(['--open=true'])).toMatchInlineSnapshot(`
500+
{
501+
"--": [],
502+
"open": true,
503+
}
504+
`);
505+
506+
expect(await cli.run(['--open=YES'])).toMatchInlineSnapshot(`
507+
{
508+
"--": [],
509+
"open": true,
510+
}
511+
`);
512+
513+
expect(await cli.run(['--open=T'])).toMatchInlineSnapshot(`
514+
{
515+
"--": [],
516+
"open": true,
517+
}
518+
`);
519+
520+
expect(await cli.run(['--open=y'])).toMatchInlineSnapshot(`
521+
{
522+
"--": [],
523+
"open": true,
524+
}
525+
`);
526+
527+
expect(await cli.run(['--open=false'])).toMatchInlineSnapshot(`
528+
{
529+
"--": [],
530+
"open": false,
531+
}
532+
`);
533+
534+
expect(await cli.run(['--open=No'])).toMatchInlineSnapshot(`
535+
{
536+
"--": [],
537+
"open": false,
538+
}
539+
`);
540+
541+
expect(await cli.run(['--open=f'])).toMatchInlineSnapshot(`
542+
{
543+
"--": [],
544+
"open": false,
545+
}
546+
`);
547+
548+
expect(await cli.run(['--open=N'])).toMatchInlineSnapshot(`
549+
{
550+
"--": [],
551+
"open": false,
552+
}
553+
`);
554+
555+
expect(await cli.run(['--no-open=true'])).toMatchInlineSnapshot(`
556+
{
557+
"--": [],
558+
"open": false,
559+
}
560+
`);
561+
562+
expect(await cli.run(['--no-open=false'])).toMatchInlineSnapshot(`
563+
{
564+
"--": [],
565+
"open": true,
566+
}
567+
`);
568+
});
569+
494570
it('should parse string option', async () => {
495571
const cli = breadc('cli');
496572
cli.option('--flag');

0 commit comments

Comments
 (0)