lukeed / lukeed/sade

expose the `error`utility (in src/utils.js)

Open
#54 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.1k
Forks
24
PR merge metrics
No merged PRs in 30d

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' };
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading src/utils.js around lines 79-84 to understand the existing error utility and then inspect how the program object exposes its methods. Done means making the utility available through prog.error for application-level validation while retaining the documented user-facing error output.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
cli
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.