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

Commit

Permalink
feat: exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed May 13, 2018
1 parent 2545f34 commit a24c135
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type IFlagBase<T, I> = {
hidden?: boolean
required?: boolean
dependsOn?: string[],
exclusive?: string[],
/**
* also accept an environment variable as input
*/
Expand Down
5 changes: 5 additions & 0 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export function validate(parse: { input: ParserInput; output: ParserOutput<any,
throw new CLIError(`--${also}= must also be provided when using --${name}=`)
}
}
for (let also of flag.exclusive || []) {
if (parse.output.flags[also]) {
throw new CLIError(`--${also}= cannot also be provided when using --${name}=`)
}
}
} else {
if (flag.required) throw new RequiredFlagError({parse, flag})
}
Expand Down
32 changes: 32 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,36 @@ See more help with --help`)
}).to.throw('--bar= must also be provided when using --foo=')
})
})

describe('exclusive', () => {
it('ignores', () => {
parse([], {
flags: {
foo: flags.string({exclusive: ['bar']}),
bar: flags.string({char: 'b'}),
},
})
})

it('succeeds', () => {
const out = parse(['--foo', 'a'], {
flags: {
foo: flags.string({exclusive: ['bar']}),
bar: flags.string({char: 'b'}),
},
})
expect(out.flags.foo).to.equal('a')
})

it('fails', () => {
expect(() => {
parse(['--foo', 'a', '-bb'], {
flags: {
foo: flags.string({exclusive: ['bar']}),
bar: flags.string({char: 'b'}),
},
})
}).to.throw('--bar= cannot also be provided when using --foo=')
})
})
})

0 comments on commit a24c135

Please sign in to comment.