-
-
Notifications
You must be signed in to change notification settings - Fork 24
Description
The error utility is used internally for cases like this:
$ node index.js --abc
ERROR
Invalid option: --abc
Run `$ my-cli-app --help` for more info.
https://github.com/lukeed/sade/blob/master/src/utils.js#L79-L84
But even if all the options are accepted by sade, they might not be valid (example: 2 options might conflict with each other). The user is responsible for these app-specific validations.
It would then be useful if sade could provide a prog.error(message).
Thanks for considering this.
UPDATE: I've made a fork for personal use where this feature is implemented. Is there any interest in a PR?
https://github.com/paulovieira/sade/commit/cd26309da9df6e1cadb8bcecbb84aaa5244cdd2d
UPDATE 2: below is the sade template that I'm using for my cli applications. Notice the call to the new prog.error utility (after the call to validateArgs), which would output this:
$ node cli.js --param1=aaa
ERROR
something is wrong
Run `$ my-cli --help` for more info.
#!/usr/bin/env node
let Sade = require('sade');
// 1 - setup sade
let isSingleCommand = true;
let prog = Sade('my-cli', isSingleCommand);
prog
.example('--param1=123')
.example('--param2=abc')
.option('--param1', 'Help for param1')
.option('--param2', 'Help for param2')
.action(({ param1, param2, _ }) => {
// do stuff...
});
// 2 - lazy parse + validation
let parseOptions = {
lazy: true,
unknown: arg => `Unknown option: ${arg}`
}
let { name, args, handler } = prog.parse(process.argv, parseOptions);
let { isValid, message } = validateArgs(args);
// 3 - proceed to the handler added in .action() or abort with a user-friendly message
if (isValid) {
handler.apply(null, args);
}
else {
prog.error(prog.bin, message);
}
function validateArgs(options) {
// return { isValid: true };
return { isValid: false, message: 'something is wrong' };
}