question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add .types() for command arguments

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
postatumcommented, Feb 9, 2017

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()

1reaction
davebrysoncommented, Jan 22, 2017

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding arguments and options to your Bash scripts
Bash uses a tool called positional parameters to provide a means of entering data into a Bash program when it is invoked from...
Read more >
Command CLI Arguments - Typer - tiangolo
Typer, build great CLIs. Easy to code. Based on Python type hints.
Read more >
argparse — Parser for command-line options, arguments and ...
The ArgumentParser object will hold all the information necessary to parse the command line into Python data types. Adding arguments¶. Filling an ArgumentParser ......
Read more >
Understanding Command Line Arguments and How to Use ...
Command line arguments are extra commands you can use when launching a program so that the program's functionality will change.
Read more >
Command line arguments in C/C++
The most important function of C/C++ is main() function. It is mostly defined with a return type of int and without parameters :...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found