commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eric Sirianni" <eric.siria...@gmail.com>
Subject Re: [CLI]: Can have two group options with an option with the same shortname?
Date Thu, 29 Jun 2006 17:41:20 GMT
This doesn't seem to work.  Consider the following example:

    private static void foo() throws Exception {
        ArgumentBuilder argumentBuilder = new ArgumentBuilder();
        DefaultOptionBuilder defaultOptionBuilder = new DefaultOptionBuilder();
        CommandBuilder commandBuilder = new CommandBuilder();
        GroupBuilder groupBuilder = new GroupBuilder();

        Argument nameArgument =
argumentBuilder.withName("name").withMaximum(1).create();
        DefaultOption nameOption =
defaultOptionBuilder.withShortName("name").withArgument(nameArgument).withRequired(true).create();

        Argument dbNameArgument =
argumentBuilder.withName("db_name").withMaximum(1).create();
        DefaultOption dbNameOption =
defaultOptionBuilder.withShortName("name").withArgument(dbNameArgument).withRequired(true).create();

        Group dbGroup =
groupBuilder.withOption(dbNameOption).withMinimum(1).create();
        DefaultOption databaseOption =
defaultOptionBuilder.withShortName("db").withChildren(dbGroup).create();

        Group optionGroup =
groupBuilder.withOption(nameOption).withOption(databaseOption).create();
        Command command =
commandBuilder.withName("create").withChildren(optionGroup).create();
        Group commands = groupBuilder.withOption(command).create();

        Parser parser = new Parser();
        parser.setGroup(commands);

        System.out.println("Command: " + command);
        CommandLine commandLine = parser.parse("create -name foo -db
-name bar".split(" "));

        System.out.println(commandLine.getValue(nameOption));
        System.out.println(commandLine.getValue(dbNameOption));
    }

I get the following output:
Command: [create [-name <name>|-db -name <db_name>]]
Exception in thread "main" org.apache.commons.cli2.OptionException:
Unexpected bar while processing create
	at org.apache.commons.cli2.commandline.Parser.parse(Parser.java:100)
	at com.foo.Main.foo(Main.java:86)
	at com.foo.Main.main(Main.java:18)

Is there a way to express my desired syntax using common-cli?  Namely,
I want to be able to have two groups with the same shortnamed option.
The groups may or may not be mutually exclusive.

Thanks,
Eric


Andrew Shirley <aks@decisionsoft.co.uk> wrote:
  On Fri, Jun 02, 2006 at 06:34:03AM -0700, David wrote:
> Dear members,
>
> I have two group options appended to my main group option of my
> parser. Let's say -report group option and -process group option. On
> both I would like to define the option -date, but I don't know how
> to distinguish each case when invoking:
>
> line.hasOption("-date");
>
> is there any way to specifically say, I just want to know if has
> option -date from group option -report?
>

when you are creating the Option objects (for the date option), you
could store them away somewhere and then do the lookup using
CommandLine#hasOption(Option) and similarly CommandLine#getValue(Option)

hope this helps
Andrew

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message