vercel / arg

Simple argument parsing
https://npmjs.com/arg
MIT License
1.23k stars 54 forks source link

Print help? #39

Closed borekb closed 5 years ago

borekb commented 5 years ago

I'm looking for a package to help me write CLIs and very much like arg's simplicity for parsing args. I was thinking that if the spec format was a bit enhanced, it could perhaps help me print help as well, like this:

const spec = {
  "--help": { type: Boolean, description: "Show help" },
  "--port": { type: Number, description: "Port number" }
};

const args = arg(spec);

if (args['--help']) {
  console.log(getHelp(spec));
}

What do you think? I know it might be out of the scope of this package but arg parsing and printing help are closely related things and I'd like to stay DRY.

Qix- commented 5 years ago

Nope, because help messages are arbitrary and it would inflate the source code. It's entirely out of scope IMO - @rauchg and I have discussed this before.

The way we do it is this:

const arg = require('arg');
const chalk = require('chalk');

const helpMessage = chalk`
  {bold USAGE}

      {dim $} {bold my-app} [--help] --string {underline some-arg}

  {bold OPTIONS}
      --help                      Shows this help message
      --string {underline some-arg}           A string flag
`;

const args = arg({
    '--help': Boolean,
    '--string': String
});

if (args['--help']) {
    console.error(helpMessage);
    process.exit(2);
}

console.log('you said:', args['--string']);

image

Took me about 2 minutes to write, tops.

rauschma commented 5 years ago

@Qix- I was also wondering if args supported this feature. Maybe document as an FAQ in the readme that it’s out of scope (along with the code example, which is really nice)?