Skip to content

Commit 61c9252

Browse files
committed
Handle unknown option with no command
1 parent 51a721e commit 61c9252

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

src/main.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ function handleUnknownOption(option, defaultOptions) {
251251
console.error(`Unknown option '${option}'.\n\nSee 'mlck --help'.\n`)
252252

253253
if (option.slice(0, 2) === '--') {
254+
// Find and display close matches using Levenshtein distance.
254255
printClosestMatches(option.slice(2), Object.keys(defaultOptions))
255256
}
256257

@@ -640,19 +641,38 @@ function handleLicenseCommand() {
640641

641642
function handleUnknownCommand(command) {
642643
if (command) {
643-
console.error(`Unknown command '${command}'.\n\nSee 'mlck --help'.\n`)
644+
if (command[0] === '-') {
645+
// Treat command as an option.
646+
const defaultOptions = {
647+
'help': false,
648+
'version': false,
649+
'license': false,
650+
}
644651

645-
if (command[0] !== '-') {
646-
// Find and display close matches using Levenshtein distance.
647-
printClosestMatches(command, [
648-
'id',
649-
'encrypt',
650-
'decrypt',
651-
'help',
652-
'version',
653-
'license',
654-
])
652+
const shortcuts = {
653+
'-h': '--help',
654+
'-?': '--help',
655+
'-V': '--version',
656+
}
657+
658+
const options = parseArgs([ command ], defaultOptions, shortcuts)
659+
660+
if (options['!?'].length > 0) {
661+
handleUnknownOption(options['!?'][0], defaultOptions)
662+
}
655663
}
664+
665+
console.error(`Unknown command '${command}'.\n\nSee 'mlck --help'.\n`)
666+
667+
// Find and display close matches using Levenshtein distance.
668+
printClosestMatches(command, [
669+
'id',
670+
'encrypt',
671+
'decrypt',
672+
'help',
673+
'version',
674+
'license',
675+
])
656676
} else {
657677
printUsage()
658678
}

0 commit comments

Comments
 (0)