Skip to content

Negative numbers are not parsed correctly #156

@kkarbowiak

Description

@kkarbowiak

In fact, anything that starts with a dash is treated as a potential optional argument, so negative numbers cannot be parsed. This results in parsing errors:

#include "argparse.hpp"
#include <iostream>


int main(int argc, char * argv[])
{
    auto parser = argparse::ArgumentParser();
    parser.add_argument("num").type<int>();
    auto parsed = parser.parse_args(argc, argv);
    auto num = parsed.get_value<int>("num");
    std::cout << "The number is " << num << '\n';
}
$ ./test1 -10
unrecognised arguments: -10
usage: test1 [-h] num

positional arguments:
  num

optional arguments:
  -h, --help            show this help message and exit

and

#include "argparse.h"
#include <iostream>


int main(int argc, char * argv[])
{
    auto parser = argparse::ArgumentParser();
    parser.add_argument("--num").type<int>();
    auto parsed = parser.parse_args(argc, argv);
    auto num = parsed.get_value<int>("num");
    std::cout << "The number is " << num << '\n';
}
$ ./test2 --num -10
argument --num: expected one argument
usage: test2 [-h] [--num NUM]

optional arguments:
  -h, --help            show this help message and exit
  --num NUM

This is a regression introduced in

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions