Skip to content

Commit

Permalink
fix: restore short flags for --help and --version (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
peternhale authored Jul 29, 2021
1 parent f3ee1f8 commit 67dadd4
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 16 deletions.
2 changes: 0 additions & 2 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export {boolean, integer, url} from './parser'

export const version = (opts: Partial<BooleanFlag<boolean>> = {}) => {
return Parser.flags.boolean({
// char: 'v',
description: 'show CLI version',
...opts,
parse: async (_: any, cmd: Command) => {
Expand All @@ -42,7 +41,6 @@ export const version = (opts: Partial<BooleanFlag<boolean>> = {}) => {

export const help = (opts: Partial<BooleanFlag<boolean>> = {}) => {
return Parser.flags.boolean({
// char: 'h',
description: 'show CLI help',
...opts,
parse: async (_: any, cmd: Command) => {
Expand Down
15 changes: 12 additions & 3 deletions src/help/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ import {HelpFormatter} from './formatter'
export {CommandHelp} from './command'
export {standardizeIDFromArgv, loadHelpClass} from './util'

function getHelpSubject(args: string[]): string | undefined {
const helpFlags = ['--help']

export function getHelpFlagAdditions(config: Interfaces.Config): string[] {
const additionalHelpFlags = config.pjson.oclif.additionalHelpFlags ?? []
return [...new Set([...helpFlags, ...additionalHelpFlags]).values()]
}

function getHelpSubject(args: string[], config: Interfaces.Config): string | undefined {
// for each help flag that starts with '--' create a new flag with same name sans '--'
const mergedHelpFlags = getHelpFlagAdditions(config)
for (const arg of args) {
if (arg === '--') return
if (arg === 'help' || arg === '--help') continue
if (mergedHelpFlags.includes(arg) || arg === 'help') continue
if (arg.startsWith('-')) return
return arg
}
Expand Down Expand Up @@ -82,7 +91,7 @@ export class Help extends HelpBase {

public async showHelp(argv: string[]) {
if (this.config.topicSeparator !== ':') argv = standardizeIDFromArgv(argv, this.config)
const subject = getHelpSubject(argv)
const subject = getHelpSubject(argv, this.config)
if (!subject) {
if (this.config.pjson.oclif.default) {
const rootCmd = this.config.findCommand(this.config.pjson.oclif.default)
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/pjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export namespace PJSON {
hidden?: boolean;
};
};
additionalHelpFlags?: string[];
additionalVersionFlags?: string[];
};
}

Expand Down
22 changes: 11 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ import {format, inspect} from 'util'

import * as Interfaces from './interfaces'
import {Config} from './config'
import {loadHelpClass, standardizeIDFromArgv} from './help'
import {getHelpFlagAdditions, loadHelpClass, standardizeIDFromArgv} from './help'

const log = (message = '', ...args: any[]) => {
// tslint:disable-next-line strict-type-predicates
message = typeof message === 'string' ? message : inspect(message)
process.stdout.write(format(message, ...args) + '\n')
}

const helpOverride = (argv: string[], config: Interfaces.Config): boolean => {
export const helpAddition = (argv: string[], config: Interfaces.Config): boolean => {
if (argv.length === 0 && !config.pjson.oclif.default) return true
const mergedHelpFlags = getHelpFlagAdditions(config)
for (const arg of argv) {
if (arg === '--help') return true
if (mergedHelpFlags.includes(arg)) return true
if (arg === '--') return false
}
return false
}

const versionOverride = (argv: string[]): boolean => {
if (['--version'].includes(argv[0])) return true
export const versionAddition = (argv: string[], config?: Interfaces.Config): boolean => {
const additionalVersionFlags = config?.pjson.oclif.additionalVersionFlags ?? []
const mergedVersionFlags = [...new Set([...['--version'], ...additionalVersionFlags]).values()]
if (mergedVersionFlags.includes(argv[0])) return true
return false
}

Expand All @@ -41,17 +44,14 @@ export async function run(argv = process.argv.slice(2), options?: Interfaces.Loa
await config.runHook('init', {id, argv: argvSlice})

// display version if applicable
if (versionOverride(argv)) {
if (versionAddition(argv, config)) {
log(config.userAgent)
return
}

// display help version if applicable
if (helpOverride(argv, config)) {
argv = argv.filter(arg => {
if (arg === '--help') return false
return true
})
if (helpAddition(argv, config)) {
argv = argv.filter(arg => !getHelpFlagAdditions(config).includes(arg))
const Help = await loadHelpClass(config)
const help = new Help(config, config.pjson.helpOptions)
await help.showHelp(argv)
Expand Down
12 changes: 12 additions & 0 deletions test/config/fixtures/help/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "help-plugin",
"description": "Module type ESM; Configure help and version flag overrides",
"private": true,
"type": "module",
"files": [],
"oclif": {
"commands": "./src/commands",
"additionalHelpFlags": ["-h", "--mycommandhelp"],
"additionalVersionFlags": ["-v", "myversion", "version"]
}
}
5 changes: 5 additions & 0 deletions test/config/fixtures/help/src/commands/foo/bar/baz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class Command {
static run() {
console.log('it works!')
}
}
51 changes: 51 additions & 0 deletions test/config/help.config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as url from 'url'
import * as path from 'path'

import {Config} from '../../src/config'

import {expect, fancy} from './test'
import {getHelpFlagAdditions} from '../../src/help'
import {helpAddition, versionAddition} from '../../src/main'

const root = path.resolve(__dirname, 'fixtures/help')
// const p = (p: string) => path.join(root, p)

// This tests file URL / import.meta.url simulation.
const rootAsFileURL = url.pathToFileURL(root).toString()

const withConfig = fancy
.add('config', () => Config.load(rootAsFileURL))

describe('help and version flag additions', () => {
withConfig
.it('has help and version additions', ({config}) => {
expect(config.pjson.oclif.additionalHelpFlags).to.have.lengthOf(2)
expect(config.pjson.oclif.additionalVersionFlags).to.have.lengthOf(3)
const mergedHelpFlags = getHelpFlagAdditions(config)
expect(mergedHelpFlags).to.deep.equal(['--help', ...config.pjson.oclif.additionalHelpFlags as string[]])
expect(helpAddition(['-h'], config)).to.be.true
expect(helpAddition(['help'], config)).to.be.false
expect(helpAddition(['--mycommandhelp'], config)).to.be.true
expect(helpAddition(['foobar'], config)).to.be.false
expect(versionAddition(['-v'], config)).to.be.true
expect(versionAddition(['version'], config)).to.be.true
expect(versionAddition(['myversion'], config)).to.be.true
expect(versionAddition(['notmyversion'], config)).to.be.false
})

withConfig
.do(({config}) => delete config.pjson.oclif.additionalHelpFlags)
.it('has version additions', ({config}) => {
expect(config.pjson.oclif.additionalHelpFlags).to.not.be.ok
expect(config.pjson.oclif.additionalVersionFlags).to.have.lengthOf(3)
const mergedHelpFlags = getHelpFlagAdditions(config)
expect(mergedHelpFlags).to.deep.equal(['--help'])
expect(helpAddition(['-h'], config)).to.be.false
expect(helpAddition(['help'], config)).to.be.false
expect(helpAddition(['mycommandhelp'], config)).to.be.false
expect(versionAddition(['-v'], config)).to.be.true
expect(versionAddition(['version'], config)).to.be.true
expect(versionAddition(['myversion'], config)).to.be.true
expect(versionAddition(['notmyversion'], config)).to.be.false
})
})

0 comments on commit 67dadd4

Please sign in to comment.