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

Commit a48f523

Browse files
committed
feat: added alsoRequire to flags
1 parent 79b0ed0 commit a48f523

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

src/errors.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ export class RequiredArgsError extends CLIParseError {
4646
}
4747

4848
export class RequiredFlagError extends CLIParseError {
49-
public flags: flags.IFlag<any>[]
49+
public flag: flags.IFlag<any>
5050

51-
constructor({flags, parse}: ICLIParseErrorOptions & { flags: flags.IFlag<any>[] }) {
52-
const usage = m.list.renderList(m.help.flagUsages(flags, {displayRequired: false}))
53-
const message = `Missing required flag${flags.length === 1 ? '' : 's'}:\n${usage}`
51+
constructor({flag, parse}: ICLIParseErrorOptions & { flag: flags.IFlag<any> }) {
52+
const usage = m.list.renderList(m.help.flagUsages([flag], {displayRequired: false}))
53+
const message = `Missing required flag:\n${usage}`
5454
super({parse, message})
55-
this.flags = flags
55+
this.flag = flag
5656
}
5757
}
5858

src/flags.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type IFlagBase<T, I> = {
1010
description?: string
1111
hidden?: boolean
1212
required?: boolean
13+
alsoRequire?: string[],
1314
/**
1415
* also accept an environment variable as input
1516
*/

src/validate.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {CLIError} from '@oclif/errors'
2+
13
import {RequiredArgsError, RequiredFlagError, UnexpectedArgsError} from './errors'
24
import {ParserInput, ParserOutput} from './parse'
35

@@ -16,10 +18,16 @@ export function validate(parse: { input: ParserInput; output: ParserOutput<any,
1618
}
1719

1820
function validateFlags() {
19-
const flags = Object.keys(parse.input.flags)
20-
.map(f => parse.input.flags[f])
21-
.filter(f => f.required && !parse.output.flags[f.name])
22-
if (flags.length) throw new RequiredFlagError({parse, flags})
21+
for (let [name, flag] of Object.entries(parse.input.flags)) {
22+
if (flag.required && !parse.output.flags[name]) {
23+
throw new RequiredFlagError({parse, flag})
24+
}
25+
for (let also of flag.alsoRequire || []) {
26+
if (!parse.output.flags[also]) {
27+
throw new CLIError(`--${also}= must also be provided when using --${name}=`)
28+
}
29+
}
30+
}
2331
}
2432

2533
validateArgs()

test/parse.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,28 @@ See more help with --help`)
413413
})
414414
expect(out.flags.foo).to.equal('b')
415415
})
416+
417+
describe('alsoRequired', () => {
418+
it('succeeds', () => {
419+
const out = parse(['--foo', 'a', '-bb'], {
420+
flags: {
421+
foo: flags.string({alsoRequire: ['bar']}),
422+
bar: flags.string({char: 'b'}),
423+
},
424+
})
425+
expect(out.flags.foo).to.equal('a')
426+
expect(out.flags.bar).to.equal('b')
427+
})
428+
429+
it('fails', () => {
430+
expect(() => {
431+
parse(['--foo', 'a'], {
432+
flags: {
433+
foo: flags.string({alsoRequire: ['bar']}),
434+
bar: flags.string({char: 'b'}),
435+
},
436+
})
437+
}).to.throw('--bar= must also be provided when using --foo=')
438+
})
439+
})
416440
})

0 commit comments

Comments
 (0)