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

Commit

Permalink
feat: added alsoRequire to flags
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Apr 9, 2018
1 parent 79b0ed0 commit a48f523
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export class RequiredArgsError extends CLIParseError {
}

export class RequiredFlagError extends CLIParseError {
public flags: flags.IFlag<any>[]
public flag: flags.IFlag<any>

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

Expand Down
1 change: 1 addition & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type IFlagBase<T, I> = {
description?: string
hidden?: boolean
required?: boolean
alsoRequire?: string[],
/**
* also accept an environment variable as input
*/
Expand Down
16 changes: 12 additions & 4 deletions src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {CLIError} from '@oclif/errors'

import {RequiredArgsError, RequiredFlagError, UnexpectedArgsError} from './errors'
import {ParserInput, ParserOutput} from './parse'

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

function validateFlags() {
const flags = Object.keys(parse.input.flags)
.map(f => parse.input.flags[f])
.filter(f => f.required && !parse.output.flags[f.name])
if (flags.length) throw new RequiredFlagError({parse, flags})
for (let [name, flag] of Object.entries(parse.input.flags)) {
if (flag.required && !parse.output.flags[name]) {
throw new RequiredFlagError({parse, flag})
}
for (let also of flag.alsoRequire || []) {
if (!parse.output.flags[also]) {
throw new CLIError(`--${also}= must also be provided when using --${name}=`)
}
}
}
}

validateArgs()
Expand Down
24 changes: 24 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,28 @@ See more help with --help`)
})
expect(out.flags.foo).to.equal('b')
})

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

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

0 comments on commit a48f523

Please sign in to comment.