Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose the errorutility (in src/utils.js) #54

Open
paulovieira opened this issue Jul 4, 2022 · 1 comment
Open

expose the errorutility (in src/utils.js) #54

paulovieira opened this issue Jul 4, 2022 · 1 comment

Comments

@paulovieira
Copy link

paulovieira commented Jul 4, 2022

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?

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 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' };
}
paulovieira added a commit to paulovieira/sade that referenced this issue Jul 11, 2022
@paulovieira
Copy link
Author

For anyone interested in using this feature: a simple alternative is to extract the code from the error utility (https://github.com/lukeed/sade/blob/master/src/utils.js#L79-L84) to your own utilities module:

function cliError(bin, str, num=1) {

  const __ = '  ';
  const NL = '\n';

  let section = function section(str, arr) {
    if (!arr || !arr.length) return '';
    let i=0, out='';
    out += (NL + __ + str);
    for (; i < arr.length; i++) {
      out += (NL + __ + __ + arr[i]);
    }
    return out + NL;
  }

  let out = section('ERROR', [str]);
  out += (NL + __ + `Run \`$ ${bin} --help\` for more info.` + NL);
  
  console.error(out);
  process.exit(num);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant