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

feat(args): Support checking options after parse function #163

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really not related to this PR, but was necessary to get circle ci to pass. Happy to create a separate PR for this if desired.

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