You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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
letSade=require('sade');// 1 - setup sade letisSingleCommand=true;letprog=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 + validationletparseOptions={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 messageif(isValid){handler.apply(null,args);}else{prog.error(prog.bin,message);}functionvalidateArgs(options){// return { isValid: true };return{isValid: false,message: 'something is wrong'};}
The text was updated successfully, but these errors were encountered:
functioncliError(bin,str,num=1){const__=' ';constNL='\n';letsection=functionsection(str,arr){if(!arr||!arr.length)return'';leti=0,out='';out+=(NL+__+str);for(;i<arr.length;i++){out+=(NL+__+__+arr[i]);}returnout+NL;}letout=section('ERROR',[str]);out+=(NL+__+`Run \`$ ${bin} --help\` for more info.`+NL);console.error(out);process.exit(num);}
The error utility is used internally for cases like this:
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?
paulovieira@cd26309
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 tovalidateArgs
), which would output this:The text was updated successfully, but these errors were encountered: