node-convict icon indicating copy to clipboard operation
node-convict copied to clipboard

Replacing Command Line Args Module

Open mlucool opened this issue 9 years ago • 8 comments

In order to replace usage of something like https://www.npmjs.com/package/command-line-args it would be helpful to allow arg to have an alias. For example, I want -h and --help to both allow for help to be called.

Example usage:

        help: {
            arg: ['help', 'h'],
            type: Boolean,
            doc: 'Display this usage message',
            default: false
        },

On a related note, getSchemaString seems to print something like:

{
  "properties": {
    "help": {},
    "env": {},
  }
}

Any idea why it is mostly blank. Ideally, I'd like to see the output mirror https://www.npmjs.com/package/command-line-args#module_command-line-args--CommandLineArgs+getUsage

mlucool avatar Feb 16 '16 23:02 mlucool

Some way of auto-generating CLI help from the schema would be nice. If the user runs node index.js --help I want to see help output similar to the way commander does it.

None of the available methods .toString, getSchema or getProperties even lists the arg keys. Hard to generate help from that? Ideas?

fgeorgsson avatar Feb 23 '16 14:02 fgeorgsson

@mlucool I'm not against adding support for command-line aliases, though I don't think it's that valuable because command-line args are already supported as long options. I fear it will make the code a bit more complex (and some parts are already too complex for easy maintenance I think). So I would rather avoid it, but it's not a definitive "No". What do other interested parties say?

On the other hand I find @pcguru's idea of auto-generating-CLI-help far more easy to accept. This would not complicate the code, this would only add a new simple method. There is no code at the moment that returns the arg keys, but that should be very easy to develop. This is a nice idea for a PR for a wannabee contributor :-)

Thanks to both of you for your ideas!

madarche avatar Mar 16 '16 09:03 madarche

I would really appreciate both command-line aliases and an auto-generated help message. As far as I can tell, convict currently uses optmist to parse the command line arguments. However optimist has been deprecated since February 2014. So maybe replacing optimist with something like commander would be a good idea anyways.

sykaeh avatar Apr 06 '16 15:04 sykaeh

The lack of short arguments is a pronounced deficiency. It's much more common to use short arguments (single hyphen) than to use the long form. convict is going way against the grain (esp in the Unix world) by only having support for long arguments.

mojavelinux avatar Sep 28 '17 00:09 mojavelinux

I'm not opposed to the idea of supporting short arguments. And now, with all the wonderful work of @elyscape and especially the 100% code coverage, we can serenely consider adding new features.

As for the syntax, I would be more in favor of something like:

help: {
            short: 'h',
            type: Boolean,
            doc: 'Display this usage message',
            default: false
        },

Ideas, discussions welcome!

madarche avatar Sep 28 '17 09:09 madarche

How is the status on this issue?

Would a PR with the new key short or shortArg like @madarche suggested have a chance of being merged? :)

bodo22 avatar Apr 08 '19 09:04 bodo22

With customGetter #313 you will be able to replace 'env' getter by your own getter/function.

A-312 avatar Dec 07 '19 08:12 A-312

You will be able to do that with :

const useArgOnlyOnce = false;

convict.addGetter('arg', function(value, schema, stopPropagation) {
  const argv = parseArgs(this.getArgs(), {
    configuration: {
      'dot-notation': false
    }
  });
  return schema._cvtCoerce(argv[value]);
}, useArgOnlyOnce, true);

;)

A-312 avatar Jan 06 '20 22:01 A-312