dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

Add .types() for command arguments #178

Open Arthelon opened 8 years ago

Arthelon commented 8 years ago

Command arguments are automatically assigned a type but there doesn't seem to be any support for explicit typing like with options. When entering in a phone number with special characters like so:

vorpal.command("send <number> <message>")

app$ send +1212345678 "hello world"

the argument "number" is converted to a number, omitting the + symbol. Furthermore, I think the .types() function available for options should also support command arguments.

postatum commented 7 years ago

I would also love to use this feature. Faced this issue when trying to specify API version using number wrapped in brackets like "1.0". It got converted to number 1.

davebryson commented 7 years ago

Same for me. Is there any workaround? I'm having problems with hex strings such as: "0x4e47e95247a535d9c1ed9e5a73f1344a5d864383" Being converted to a number: 4.469049581943643e+47

The only alternative is to use a type with an option but this is a little awkward for the CLI I'm working on.

The conversion seems to be from the dep minimist here: https://github.com/dthree/vorpal/blob/master/lib/util.js#L33

And via minimist, anything not flagged with an option flag that may be a number according to the regex is converted:

https://github.com/substack/minimist/blob/master/index.js#L186

jstoiko commented 7 years ago

maybe arguments that are quoted should be passed as strings, at least.

postatum commented 7 years ago

Hello again dear contributors.

@dthree, I'm ready to implement this feature/bugfix and I was wondering if you would be willing to accept my contribution. If you would, please share your opinion on this issue and maybe give some guidelines if you think this issue makes sense.

Thank you! :)

postatum commented 7 years ago

Greetings to everyone having this issue.

I've discovered a way to make params be parsed as strings. Hopefully this will be useful to someone. Note: This enables such behavior per-command and parses ALL arguments(non-options) as strings.

So the solution is - add "_" to your string types by calling .types({string: ['_']}). This has to be done PER-COMMAND for those commands in which you want arguments to be parsed as strings.

Examples:

If you already have .types() call add "_" to its "string":

Before:
vorpal
  .command('send <number> <message>')
  .option('--anotherNumber <val>')
  .types({string: ['anotherNumber']})

After:
vorpal
  .command('send <number> <message>')
  .option('--anotherNumber <val>')
  .types({string: ['anotherNumber', '_']})

If you don't have .types() call, make new one with "_" in "string":

Before:
vorpal
  .command('send <number> <message>')

After:
vorpal
  .command('send <number> <message>')
  .types({string: ['_']})

Documentation on .types()

gagarin55 commented 7 years ago

@dthree Any plans to fix this issue ?