Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
feat(args): Support checking options after parse function
Browse files Browse the repository at this point in the history
fixes #162
  • Loading branch information
justinedelson committed Apr 21, 2021
1 parent f4e6f85 commit c8a511e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
working_directory: ~/cli
environment:
NYC: "yarn exec nyc -- --nycrc-path node_modules/@oclif/nyc-config/.nycrc"
FORCE_COLOR: "0"
steps:
- checkout
- restore_cache: &restore_cache
Expand Down
2 changes: 2 additions & 0 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface IArg<T = string> {
parse?: ParseFn<T>;
default?: T | (() => T);
options?: string[];
postParseOptions?: string[];
}

export interface ArgBase<T> {
Expand All @@ -19,6 +20,7 @@ export interface ArgBase<T> {
default?: T | (() => T);
input?: string;
options?: string[];
postParseOptions?: string[];
}

export type RequiredArg<T> = ArgBase<T> & {
Expand Down
7 changes: 7 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ export class ArgInvalidOptionError extends CLIParseError {
super({parse: {}, message})
}
}

export class ArgInvalidPostParseOptionError extends CLIParseError {
constructor(arg: Arg<any>, input: string) {
const message = `Expected ${input} to be one of: ${arg.postParseOptions!.join(', ')} after parsing`
super({parse: {}, message})
}
}
6 changes: 5 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
if (arg.options && !arg.options.includes(token.input)) {
throw new m.errors.ArgInvalidOptionError(arg, token.input)
}
args[i] = arg.parse(token.input)
const parsedToken = arg.parse(token.input)
if (arg.postParseOptions && !arg.postParseOptions.includes(parsedToken)) {
throw new m.errors.ArgInvalidPostParseOptionError(arg, parsedToken)
}
args[i] = parsedToken
} else {
args[i] = token.input
}
Expand Down
17 changes: 17 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,23 @@ See more help with --help`)
})
})

describe('arg postParseOptions', () => {
it('accepts valid postParseOptions', () => {
const out = parse(['myotheropt'], {
args: [{name: 'foo', parse: s => s.toUpperCase(), postParseOptions: ['MYOPT', 'MYOTHEROPT']}],
})
expect(out.args.foo).to.equal('MYOTHEROPT')
})

it('fails when invalid', () => {
expect(() => {
parse(['invalidopt'], {
args: [{name: 'foo', parse: s => s.toUpperCase(), postParseOptions: ['MYOPT', 'MYOTHEROPT']}],
})
}).to.throw('Expected INVALIDOPT to be one of: MYOPT, MYOTHEROPT after parsing')
})
})

describe('env', () => {
it('accepts as environment variable', () => {
process.env.TEST_FOO = '101'
Expand Down

0 comments on commit c8a511e

Please sign in to comment.