From 364d6dd8fd5a54334a6e77255cd6b3a5e7321632 Mon Sep 17 00:00:00 2001 From: RasPhilCo Date: Wed, 27 Jan 2021 10:29:22 -0800 Subject: [PATCH] fix: only --version & --help are special global flags (#96) --- src/command.ts | 7 +------ src/help/index.ts | 2 +- src/main.ts | 5 +---- test/command/command.test.ts | 10 +++++----- test/command/main.test.ts | 4 ++-- 5 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/command.ts b/src/command.ts index 4377c1d01..6a70e22d6 100644 --- a/src/command.ts +++ b/src/command.ts @@ -164,12 +164,6 @@ export default abstract class Command { protected async catch(err: any): Promise { if (!err.message) throw err - if (err.message.match(/Unexpected arguments?: (-h|--help|help)(,|\n)/)) { - return this._help() - } - if (err.message.match(/Unexpected arguments?: (-v|--version|version)(,|\n)/)) { - return this._version() - } try { const {cli} = require('cli-ux') const chalk = require('chalk') // eslint-disable-line node/no-extraneous-require @@ -202,6 +196,7 @@ export default abstract class Command { } protected _helpOverride(): boolean { + if (this.argv[0] === '--version') return this._version() as any for (const arg of this.argv) { if (arg === '--help') return true if (arg === '--') return false diff --git a/src/help/index.ts b/src/help/index.ts index b667bc0a9..04735d832 100644 --- a/src/help/index.ts +++ b/src/help/index.ts @@ -23,7 +23,7 @@ const ROOT_INDEX_CMD_ID = '' function getHelpSubject(args: string[]): string | undefined { for (const arg of args) { if (arg === '--') return - if (arg === 'help' || arg === '--help' || arg === '-h') continue + if (arg === 'help' || arg === '--help') continue if (arg.startsWith('-')) return return arg } diff --git a/src/main.ts b/src/main.ts index 05f5a5736..399604f85 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,7 +13,6 @@ const log = (message = '', ...args: any[]) => { } const helpOverride = (argv: string[], config: Interfaces.Config): boolean => { - if (['-h', 'help'].includes(argv[0])) return true if (argv.length === 0 && !config.findCommand(ROOT_INDEX_CMD_ID)) return true for (const arg of argv) { if (arg === '--help') return true @@ -23,7 +22,7 @@ const helpOverride = (argv: string[], config: Interfaces.Config): boolean => { } const versionOverride = (argv: string[]): boolean => { - if (['-v', '--version', 'version'].includes(argv[0])) return true + if (['--version'].includes(argv[0])) return true return false } @@ -44,9 +43,7 @@ export async function run(argv = process.argv.slice(2), options?: Interfaces.Loa // display help version if applicable if (helpOverride(argv, config)) { argv = argv.filter(arg => { - if (arg === 'help') return false if (arg === '--help') return false - if (arg === '-h') return false return true }) const Help = getHelpClass(config) diff --git a/test/command/command.test.ts b/test/command/command.test.ts index f6f57f836..5b1b617c0 100644 --- a/test/command/command.test.ts +++ b/test/command/command.test.ts @@ -257,10 +257,10 @@ OPTIONS .stdout() .do(async () => { class CMD extends Command {} - await CMD.run(['-h'], root) + await CMD.run(['--help'], root) }) .catch(/EEXIT: 0/) - .it('-h', ctx => { + .it('--help', ctx => { // expect(process.exitCode).to.equal(0) expect(ctx.stdout).to.equal(`test command @@ -281,7 +281,7 @@ USAGE class CMD extends Command { config = config } - await CMD.run(['-h']) + await CMD.run(['--help']) }) .catch((error: Error) => expect(error.message).to.contain('Unable to load configured help class "help-class-does-not-exist", failed with message:\n')) .it('shows useful error message when configured help class cannot be loaded') @@ -300,7 +300,7 @@ USAGE config = config } - await CMD.run(['-h']) + await CMD.run(['--help']) }) .catch(/EEXIT: 0/) .it('-h via a plugin in lib dir (compiled to js)', ctx => { @@ -325,7 +325,7 @@ USAGE config = config } - await CMD.run(['-h']) + await CMD.run(['--help']) }) .catch(/EEXIT: 0/) .it('-h via a plugin in src dir (source in ts)', ctx => { diff --git a/test/command/main.test.ts b/test/command/main.test.ts index ac72f2a3a..eba0e1518 100644 --- a/test/command/main.test.ts +++ b/test/command/main.test.ts @@ -16,13 +16,13 @@ describe('main', () => { fancy .stdout() - .do(() => run(['-v'], root)) + .do(() => run(['--version'], root)) .do((output: any) => expect(output.stdout).to.equal(version + '\n')) .it('runs -v') fancy .stdout() - .do(() => run(['-h'], root)) + .do(() => run(['--help'], root)) .do((output: any) => expect(output.stdout).to.equal(`base library for oclif CLIs VERSION