Skip to content

Invalid "negative strings" are treated as numbers #97

Description

@morinim

Inside:

inline bool parser::is_number(std::string const& arg) const
{
   std::istringstream istr(arg);
   double number;
   istr >> number;
   return !(istr.fail() || istr.bad());
}

stream extraction succeeds when it can parse a numeric prefix. It doesn't require the entire string to be numeric.

For example:

  • -1abc -> parses numeric prefix -1;
  • -1.2.3 -> parses numeric prefix -1.2.

and is_number() returns true. Then

inline bool parser::is_option(std::string const& arg) const
{
   assert(0 != arg.size());
   if (is_number(arg))
      return false;
   ...

returns false, causing these arguments to be stored as positional values:

const char* argv[] = { "-1abc" };
argh::parser cmdl(1, argv);

Actual:

cmdl.pos_args().size() == 1
cmdl.flags().empty()

Expected:

cmdl["1abc"] == true

This can also alter subsequent parsing. Given a registered parameter --output -1abc, -1abc may be consumed as output value instead of being recognised as another option.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions