Thanks to @elliot-nelson, you can now configure sywac to error when unknown flags or arguments are encountered during parsing, giving your CLI an extra layer of security against typos for sensitive operations.
To enable strict mode, call the .strict()
synchronous configuration method, just like you would do for any other configuration.
A good example
Consider this use-case from #17:
// example of sensitive operation WITHOUT strict mode
require('sywac')
.boolean('-D, --dry-run', { desc: 'Dry run' })
.command('delete-all', {
desc: 'Deletes everything',
run: argv => {
console.log(argv.D ? 'no harm done' : 'wreak havoc')
}
})
.help('-h, --help', { implicitCommand: false })
.parseAndExit()
$ program delete-all --dyr-run
wreak havoc
Oh no! Without .strict()
mode, the parser basically ignored the mistyped --dyr-run
option.
But if we add .strict()
mode:
// example of sensitive operation WITH strict mode
require('sywac')
.boolean('-D, --dry-run', { desc: 'Dry run' })
.command('delete-all', {
desc: 'Deletes everything',
run: argv => {
console.log(argv.D ? 'no harm done' : 'wreak havoc')
}
})
.help('-h, --help', { implicitCommand: false })
.strict() // <-- just add this one line here
.parseAndExit()
$ program delete-all --dyr-run
Usage: program delete-all [options]
Options:
-D, --dry-run Dry run [boolean]
-h, --help Show help [boolean]
Unknown options: --dyr-run
Whew! The option was mistyped and the CLI erred out before wreaking havoc.
Caveats
If you use .showHelpByDefault()
along with .strict()
mode for your command-driven CLI, sywac will still Do The Right Thing™ and print help text without an error until a runnable command is actually specified, at which point .strict()
mode kicks in and makes sure all arguments and options for that command are kosher.
However, using .strict()
mode along with your own custom default command (i.e. one with a *
alias) will not work because strict mode will dominate parsing control before your default command is given a chance to run.
Again, thanks to @elliot-nelson, we have an idea that may fix this in the future, but for now see further info about this here.
Now it's your turn
Give the new .strict()
mode a test spin and let us know what you think. Thanks!