fix: execution of shell commands that have -h/--help option

This commit is contained in:
Zoltan Kochan
2021-05-28 02:49:00 +03:00
parent 9a23fbc712
commit 22f8410393
3 changed files with 24 additions and 10 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/parse-cli-args": patch
---
The `--help` option should not convert the command to `help` if the command is unknown. So `pnpm eslint -h` is not parsed as `pnpm help eslint`.

View File

@@ -48,16 +48,6 @@ export default async function parseCliArgs (
inputArgv,
0
)
if (noptExploratoryResults['help']) {
return {
argv: noptExploratoryResults.argv,
cmd: 'help',
options: {},
params: noptExploratoryResults.argv.remain,
unknownOptions: new Map(),
fallbackCommandUsed: false,
}
}
const recursiveCommandUsed = RECURSIVE_CMDS.has(noptExploratoryResults.argv.remain[0])
let commandName = getCommandName(noptExploratoryResults.argv.remain)
@@ -67,6 +57,15 @@ export default async function parseCliArgs (
cmd = opts.fallbackCommand!
commandName = opts.fallbackCommand!
inputArgv.unshift(opts.fallbackCommand!)
} else if (noptExploratoryResults['help']) {
return {
argv: noptExploratoryResults.argv,
cmd: 'help',
options: {},
params: noptExploratoryResults.argv.remain,
unknownOptions: new Map(),
fallbackCommandUsed: false,
}
}
const types = {
...opts.universalOptionsTypes,

View File

@@ -158,6 +158,16 @@ test('if a help option is used, set cmd to "help"', async () => {
expect(cmd).toBe('help')
})
test('if a help option is used with an unknown command, do not set cmd to "help"', async () => {
const { cmd, fallbackCommandUsed } = await parseCliArgs({
...DEFAULT_OPTS,
getCommandLongName: () => null,
fallbackCommand: 'run',
}, ['eslint', '--help'])
expect(cmd).toBe('run')
expect(fallbackCommandUsed).toBeTruthy()
})
test('no command', async () => {
const { cmd } = await parseCliArgs({
...DEFAULT_OPTS,