Skip to content

Commit

Permalink
fix: only --version & --help are special global flags (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo committed Jan 27, 2021
1 parent 2005c5f commit 364d6dd
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 18 deletions.
7 changes: 1 addition & 6 deletions src/command.ts
Expand Up @@ -164,12 +164,6 @@ export default abstract class Command {

protected async catch(err: any): Promise<any> {
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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/help/index.ts
Expand Up @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions src/main.ts
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions test/command/command.test.ts
Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand Down
4 changes: 2 additions & 2 deletions test/command/main.test.ts
Expand Up @@ -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
Expand Down

0 comments on commit 364d6dd

Please sign in to comment.