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.

Issues parsing negative numbers when passed without `=`

See original GitHub issue

in node shell:

> const arg = require('arg')
undefined
> arg({"--int": parseInt}, {argv:["--int=-100"]})
{ _: [], '--int': -100 }
> arg({"--int": parseInt}, {argv:["--int -100"]})
Thrown:
{ Error: Unknown or unexpected option: --int -100
    at arg (/run/media/karfau/hdd-data/dev/node-cli-arguments-options/arg/node_modules/arg/index.js:88:19) code: 'ARG_UNKNOWN_OPTION' }
> arg({"--num": parseFloat}, {argv:["--num=-1.0"]})
{ _: [], '--num': -1 }
> arg({"--num": parseFloat}, {argv:["--num -1.0"]})
Thrown:
{ Error: Unknown or unexpected option: --num -1.0
    at arg (node-cli-arguments-options/arg/node_modules/arg/index.js:88:19) code: 'ARG_UNKNOWN_OPTION' }

I also tried to wrap the numbers with different kinds of quotes, but it didn’t have any effect. I don’t know if this is a “feature” or a “bug”.

Update: The example above is bad in the sense that argv is different from what it would look like when using from the shell, which influences the error messages. A better example is:

> const arg = require('arg')
undefined
> arg({"--int": parseInt}, {argv:["--int", "-100"]})
Thrown:
Error: Option requires argument: --int
    at arg (node-cli-arguments-options/arg/node_modules/arg/index.js:105:13)
>  arg({"--float": parseFloat}, {argv:["--float", "-0.1"]})
Thrown:
Error: Option requires argument: --float
    at arg (node-cli-arguments-options/arg/node_modules/arg/index.js:105:13)

Since the repo doesn’t seem to be maintained (sorry for that, it’s not true, must have switched the name with one of the other repos), just filing this to make people aware since I discovered it while comparing features of different argument parsers (WIP)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Qix-commented, Feb 1, 2020

--int=-5 works. We don’t have special handling for numbers. That is indeed a bug.

However, the confusion comes from your usage. In the OP, you CANNOT pass a single string ("--int -5") to the argv option property. That will never work, even with a bugfix.

I’ll push a patch shortly. Thank you for bringing it up.

1reaction
Qix-commented, Feb 2, 2020

Got distracted yesterday, apologies. Released as 4.1.3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with parsing negative numbers in Java - Stack Overflow
The error message you display indicates that an attempt was made to pass a Null String ("") through the Integer.parseInt() method. IMHO it's...
Read more >
Net 5.0 Int.Parse fails parsing with negative values. #40258
Net 5.0 Preview 7, fails on Int.Parse("-1") with Message: System.FormatException : Input string was not in a correct format.
Read more >
Chapter 2. Parsing a Number - Andrew Shitov
The first two numbers pass the exam, but the third one does not. For a negative number, the parse method returns Nil ....
Read more >
[0.1.8] Negative numbers parsed from strings act strangely
Multiplying by m2, the exact same number but not parsed from string, doesn't have this problem and gives the correct answer: -200.
Read more >
Why is the check for the negative number not working in my ...
I'm at the last section 3/3 and the problem is. ... last project for the C# beginner course and I can't make it...
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