Skip to content

Support negative number arguments #2338

@mmkal

Description

@mmkal

It would be good to allow passing in negative numbers via CLI - ideally without workarounds for options, but as far as I can tell, it doesn't work at all for arguments.

Example:

import {program} from 'commander'

program.argument('<number>', 'The number to square').action(num => {
  console.log(Math.pow(Number(num), 2))
})

program.parse()

You can run node square.js 2 but not node square.js -2. Since it's not an option, there's no equivalent to the --foo=-2 suggested in #61. That issue has been closed for a few years but this comment suggests it's worth opening a new one #61 (comment).

My suggested fix:

If an argv value matching ^-\d(\.\d+)?$ is encountered, and doesn't match any option, don't throw error: unknown option '-2'. Just treat it like an non-option argv. I haven't looked into the code but hopefully that could be done by a single check somewhere. If feasible, this would also probably solve for the original complaint in #61 without needing end-users to know the = workaround.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions