Skip to content

Commit

Permalink
feat: handle unknown options
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Mar 27, 2023
1 parent 2b51995 commit 9fbf083
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/breadc/src/breadc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ export function breadc(name: string, config: AppOption = {}) {
globalOptions.push(option);
return breadc;
},
command(text, _config = {}) {
command(text, _config = {}, _config2: any = {}) {
const config =
typeof _config === 'string' ? { description: _config } : _config;
typeof _config === 'string'
? { description: _config, ..._config2 }
: _config;

const command = makeCommand(text, config, root, container);

Expand Down
2 changes: 2 additions & 0 deletions packages/breadc/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export function makeCommand<F extends string = string>(
return makeTreeNode({
command,
init(context) {
context.config.allowUnknownOption =
config.allowUnknownOption ?? 'error';
initContextOptions(options, context);
},
finish(context) {
Expand Down
13 changes: 12 additions & 1 deletion packages/breadc/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ export function parseOption(
context.result.options[name] = option.cast(context.result.options[name]);
}
} else {
throw new ParseError(`Unknown option ${token.raw()}`);
switch (context.config.allowUnknownOption) {
case 'rest':
context.result['--'].push(token.raw());
case 'skip':
break;
case 'error':
default:
throw new ParseError(`Unknown option ${token.raw()}`);
}
}
return cursor;
/* c8 ignore next 1 */
Expand All @@ -87,6 +95,9 @@ export function parse(root: TreeNode, args: string[]): BreadcParseResult {
'--': []
},
meta: {},
config: {
allowUnknownOption: 'error'
},
parseOption
};

Expand Down
7 changes: 7 additions & 0 deletions packages/breadc/src/parser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export interface Context {
*/
meta: Record<string, any>;

/**
* Configuration
*/
config: {
allowUnknownOption: 'error' | 'skip' | 'rest';
};

parseOption(
cursor: TreeNode,
token: Token,
Expand Down
9 changes: 8 additions & 1 deletion packages/breadc/src/types/breadc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export interface Breadc<GlobalOption extends object = {}> {

command<F extends string = string>(
format: F,
description?: string
description?: string,
option?: CommandOption
): Command<F, ExtractCommand<F>, {}, GlobalOption>;
command<F extends string = string>(
format: F,
Expand Down Expand Up @@ -103,10 +104,16 @@ export interface Command<

export interface CommandOption {
description?: string;

/**
* Config how to handle unknown options
*/
allowUnknownOption?: 'error' | 'skip' | 'rest';
}

export interface Argument {
type: 'const' | 'require' | 'optional' | 'rest';

name: string;
}

Expand Down
30 changes: 30 additions & 0 deletions packages/breadc/test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ import { breadc } from '../src';

options.enabled = false;

describe('Skip unknown options', () => {
it('should skip', async () => {
const cli = breadc('cli');
cli
.command('echo', { allowUnknownOption: 'skip' })
.action((options) => options['--']);
expect(await cli.run(['echo', '--test', '--check'])).toMatchInlineSnapshot(
'[]'
);
});

it('should add to rest args', async () => {
const cli = breadc('cli');
cli
.command('echo', 'Echo command', { allowUnknownOption: 'rest' })
.action((options) => options['--']);
expect(
await cli.run(['echo', '--test', 'abc', '--check'])
).toMatchInlineSnapshot(
`
[
"--test",
"abc",
"--check",
]
`
);
});
});

describe('Alias command', () => {
it('should share alias', async () => {
const cli = breadc('cli');
Expand Down

0 comments on commit 9fbf083

Please sign in to comment.