Overview
Features
Download
Documentation
Community
Add-Ons & Services

How to deal with option argument value

Please post support and help requests here.

How to deal with option argument value

Postby dbarzo » 17 Jun 2014, 16:43

Hi,

maybe the question is trivial but I cannot figure out...

I'm using poco 1.5.2-all and I need to handle an option like '/d=n' (where n is an integer).
I defined my option like:

Code: Select all
options.addOption(
  Option("debug", "d", "Debug Level")
     .required(false)
     .repeatable(false)
     .argument("value")
     .callback(OptionCallBack<TBServer>(this, &TBServer::HandleDebugOpt)));


But when I run the code with argument /d=2 I get a Poco::Util::AmbiguousOptionException in Poco::Util::OptionSet::getOption(string&, bool)

So I have two questions:
1. Where I wrong;
2. What exactly the 'argument()' method does?

Thanks in advance,
Daniele.
dbarzo
 
Posts: 38
Joined: 14 Jan 2013, 17:15
Location: Italy

Re: How to deal with option argument value

Postby fbraem » 18 Jun 2014, 08:26

When you use the short option the value of the argument must follow directly after the argument:

-d2

When you use the long option you can use the equal sign or a colon. A space is only allowed when the argument is required (also for short option).

--debug=2

The argument() gives the argument a name, which is used in the help information ...
Zumuta!, that's the way to do IT!
fbraem
 
Posts: 103
Joined: 11 Aug 2008, 22:47
Location: Belgium

Re: How to deal with option argument value

Postby dbarzo » 18 Jun 2014, 08:53

Thanks a lot fbraem!
dbarzo
 
Posts: 38
Joined: 14 Jan 2013, 17:15
Location: Italy

Re: How to deal with option argument value

Postby dbarzo » 18 Jun 2014, 23:07

Since it did not work I digged a bit...and maybe I found a little strangeness.

I'm developing a Server Application and I see that, in this kind of app, there are some "predefined" options:

Code: Select all
+ [0]  {_shortName="" _fullName="registerService" _description="Register the application as a service."}
+ [1]  {_shortName="" _fullName="unregisterService" _description="Unregister the application as a service."}
+ [2]  {_shortName="" _fullName="displayName" _description="Specify a display name for the service (only with /registerService)."}
+ [3]  {_shortName="" _fullName="description" _description="Specify a description for the service (only with /registerService)."}
+ [4]  {_shortName="" _fullName="startup" _description="Specify the startup mode for the service (only with /registerService)."}


I added these:
Code: Select all
+ [5]  {_shortName="h" _fullName="help" _description="display help information" }
+ [6]  {_shortName="f" _fullName="config-file" _description="specify a custom configuration file" }
+ [7]  {_shortName="d" _fullName="debug" _description="run the server in debug mode" }


So, looking at the method which raises the exceptions:

Code: Select all
const Option& OptionSet::getOption(const std::string& name, bool matchShort) const
{
  const Option* pOption = 0;
  for (Iterator it = _options.begin(); it != _options.end(); ++it)
  {
    if ((matchShort && it->matchesShort(name)) || (!matchShort && it->matchesPartial(name)))
    {
      if (!pOption)
      {
        pOption = &*it;
        if (!matchShort && it->matchesFull(name))
          break;
      }
      else if (!matchShort && it->matchesFull(name))
      {
        pOption = &*it;
        break;
      }
      else throw AmbiguousOptionException(name);
    }
  }
  if (pOption)
    return *pOption;
  else
    throw UnknownOptionException(name);
}


The problem maybe is due to the fact that in my option 'd' (debug) the char 'd' is contained also in options displayName and description
So, when it made matchesShort() and matchesPartial() it 'matches'.
But, while comparing 'd' with 'displayName' does not have effect because, at first turn, pOption is NULL, in the second turn, when comparing 'd' with 'description', the flow goes to the second 'else if' and, since it->matchesFull(name) return false the exception is trown.

In fact, changing my option name from:
Code: Select all
options.addOption( Option("debug", "d", "run the server in debug mode") ..)

to
Code: Select all
options.addOption( Option("Xdebug", "X", "run the server in debug mode") ..)

and passing /X 2, all works as expected.

Is this a bug or a know thing?

PS. Just to note that a space is required between the letter and the value. If '/X2' (instead of '/X 2') is specified, an UnknownOptionException is thrown.

Regards,
Daniele.
dbarzo
 
Posts: 38
Joined: 14 Jan 2013, 17:15
Location: Italy


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron