Skip to content

Commit

Permalink
feat: support flag aliases (#521)
Browse files Browse the repository at this point in the history
* feat: support flag aliases

* chore: fix e2e tests
  • Loading branch information
mdonnalley committed Oct 14, 2022
1 parent f95918e commit 63f3e0e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/config/config.ts
Expand Up @@ -752,6 +752,7 @@ export async function toCached(c: Command.Class, plugin?: IPlugin): Promise<Comm
allowNo: flag.allowNo,
dependsOn: flag.dependsOn,
exclusive: flag.exclusive,
aliases: flag.aliases,
}
} else {
flags[name] = {
Expand All @@ -771,6 +772,7 @@ export async function toCached(c: Command.Class, plugin?: IPlugin): Promise<Comm
relationships: flag.relationships,
exclusive: flag.exclusive,
default: await defaultToCached(flag),
aliases: flag.aliases,
}
// a command-level placeholder in the manifest so that oclif knows it should regenerate the command during help-time
if (typeof flag.defaultHelp === 'function') {
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/parser.ts
Expand Up @@ -143,6 +143,10 @@ export type FlagProps = {
* Define complex relationships between flags.
*/
relationships?: Relationship[];
/**
* Alternate names that can be used for this flag.
*/
aliases?: string[];
}

export type BooleanFlagProps = FlagProps & {
Expand Down
9 changes: 9 additions & 0 deletions src/parser/parse.ts
Expand Up @@ -39,6 +39,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
private readonly raw: ParsingToken[] = []

private readonly booleanFlags: { [k: string]: BooleanFlag<any> }
private readonly flagAliases: { [k: string]: BooleanFlag<any> | OptionFlag<any> }

private readonly context: any

Expand All @@ -52,6 +53,10 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
this.argv = [...input.argv]
this._setNames()
this.booleanFlags = pickBy(input.flags, f => f.type === 'boolean') as any
this.flagAliases = Object.fromEntries(Object.values(input.flags).flatMap(flag => {
return (flag.aliases ?? []).map(a => [a, flag])
}))

this.metaData = {}
}

Expand All @@ -64,6 +69,10 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
return name
}

if (this.flagAliases[name]) {
return this.flagAliases[name].name
}

if (arg.startsWith('--no-')) {
const flag = this.booleanFlags[arg.slice(5)]
if (flag && flag.allowNo) return flag.name
Expand Down
8 changes: 4 additions & 4 deletions test/integration/plugins.e2e.ts
Expand Up @@ -193,7 +193,7 @@ describe('oclif plugins', () => {

describe('installing a plugin by name', () => {
it('should install the plugin', async () => {
const result = await executor.executeCommand('plugins:install @oclif/plugin-warn-if-update-available')
const result = await executor.executeCommand('plugins:install @oclif/plugin-warn-if-update-available 2>&1')
expect(result.code).to.equal(0)
expect(result.output).to.include('@oclif/plugin-warn-if-update-available... installed v')

Expand All @@ -205,7 +205,7 @@ describe('oclif plugins', () => {

describe('installing a plugin by github url', () => {
after(async () => {
await executor.executeCommand('plugins:uninstall @oclif/plugin-warn-if-update-available')
await executor.executeCommand('plugins:uninstall @oclif/plugin-warn-if-update-available 2>&1')
})

it('should install the plugin', async () => {
Expand All @@ -220,7 +220,7 @@ describe('oclif plugins', () => {

describe('forcefully installing a plugin', () => {
it('should install the plugin', async () => {
const result = await executor.executeCommand('plugins:install @oclif/plugin-warn-if-update-available --force')
const result = await executor.executeCommand('plugins:install @oclif/plugin-warn-if-update-available --force 2>&1')
expect(result.code).to.equal(0)
expect(result.output).to.include('@oclif/plugin-warn-if-update-available... installed v')

Expand All @@ -236,7 +236,7 @@ describe('oclif plugins', () => {
})

it('should uninstall the plugin', async () => {
const result = await executor.executeCommand('plugins:uninstall @oclif/plugin-warn-if-update-available')
const result = await executor.executeCommand('plugins:uninstall @oclif/plugin-warn-if-update-available 2>&1')
expect(result.code).to.equal(0)
expect(result.output).to.include('success Uninstalled packages.Uninstalling @oclif/plugin-warn-if-update-available... done\n')

Expand Down
2 changes: 1 addition & 1 deletion test/integration/util.ts
Expand Up @@ -39,7 +39,7 @@ export class Executor {

public executeCommand(cmd: string): Promise<Result> {
const executable = process.platform === 'win32' ? path.join('bin', 'run.cmd') : path.join('bin', 'run')
return this.executeInTestDir(`${executable} ${cmd} 2>&1`)
return this.executeInTestDir(`${executable} ${cmd}`)
}

public exec(cmd: string, cwd = process.cwd(), silent = true): Promise<Result> {
Expand Down
24 changes: 24 additions & 0 deletions test/parser/parse.test.ts
Expand Up @@ -1338,4 +1338,28 @@ See more help with --help`)
})
})
})

describe('flag aliases', () => {
it('works with defined name', async () => {
const out = await parse(['--foo'], {
flags: {
foo: flags.boolean({
aliases: ['bar'],
}),
},
})
expect(out.flags.foo).to.equal(true)
})

it('works with aliased name', async () => {
const out = await parse(['--bar'], {
flags: {
foo: flags.boolean({
aliases: ['bar'],
}),
},
})
expect(out.flags.foo).to.equal(true)
})
})
})

0 comments on commit 63f3e0e

Please sign in to comment.