Example usage for org.apache.commons.cli OptionBuilder create

List of usage examples for org.apache.commons.cli OptionBuilder create

Introduction

In this page you can find the example usage for org.apache.commons.cli OptionBuilder create.

Prototype

public static Option create() throws IllegalArgumentException 

Source Link

Document

Create an Option using the current settings

Usage

From source file:org.midonet.midolman.tools.MmCtl.java

private static OptionGroup getOptionalOptionGroup() {
    OptionGroup optionalGroup = new OptionGroup();

    OptionBuilder.hasArg();//from w  w w. j  ava  2  s. c  o m
    OptionBuilder.withLongOpt("config");
    OptionBuilder.withDescription("MM configuration file");
    optionalGroup.addOption(OptionBuilder.create());

    optionalGroup.setRequired(false);
    return optionalGroup;
}

From source file:org.midonet.midolman.tools.MmCtl.java

private static OptionGroup getMutuallyExclusiveOptionGroup() {

    // The command line tool can only accept one of these options:
    OptionGroup mutuallyExclusiveOptions = new OptionGroup();

    OptionBuilder.hasArgs(2);//from  ww w. jav a2s . c  o m
    OptionBuilder.isRequired();
    OptionBuilder.withLongOpt("bind-port");
    OptionBuilder.withDescription("Bind a port to an interface");
    mutuallyExclusiveOptions.addOption(OptionBuilder.create());

    OptionBuilder.hasArg();
    OptionBuilder.isRequired();
    OptionBuilder.withLongOpt("unbind-port");
    OptionBuilder.withDescription("Unbind a port from an interface");
    mutuallyExclusiveOptions.addOption(OptionBuilder.create());

    OptionBuilder.withLongOpt("list-hosts");
    OptionBuilder.withDescription("List MidolMan agents in the system");
    mutuallyExclusiveOptions.addOption(OptionBuilder.create());

    // make sure that there is at least one.
    mutuallyExclusiveOptions.setRequired(true);

    return mutuallyExclusiveOptions;
}

From source file:org.midonet.mmdpctl.Mmdpctl.java

public static void main(String... args) {
    Options options = new Options();

    // The command line tool can only accept one of these options:
    OptionGroup mutuallyExclusiveOptions = new OptionGroup();

    OptionBuilder.withDescription("List all the installed datapaths");
    OptionBuilder.isRequired();/*w ww.  jav  a  2s  .co  m*/
    OptionBuilder.withLongOpt("list-dps");
    mutuallyExclusiveOptions.addOption(OptionBuilder.create());

    OptionBuilder.withDescription("Show all the information related to a given datapath.");
    OptionBuilder.hasArg();
    OptionBuilder.isRequired();
    OptionBuilder.withLongOpt("show-dp");
    mutuallyExclusiveOptions.addOption(OptionBuilder.create());

    OptionBuilder.withDescription("Show all the flows installed for a given datapath.");
    OptionBuilder.hasArg();
    OptionBuilder.isRequired();
    OptionBuilder.withLongOpt("dump-dp");
    mutuallyExclusiveOptions.addOption(OptionBuilder.create());

    OptionBuilder.withDescription("Add a new datapath.");
    OptionBuilder.hasArg();
    OptionBuilder.withLongOpt("add-dp");
    mutuallyExclusiveOptions.addOption(OptionBuilder.create());

    OptionBuilder.withDescription("Delete a datapath.");
    OptionBuilder.hasArg();
    OptionBuilder.withLongOpt("delete-dp");
    mutuallyExclusiveOptions.addOption(OptionBuilder.create());

    // make sure that there is at least one.
    mutuallyExclusiveOptions.setRequired(true);
    options.addOptionGroup(mutuallyExclusiveOptions);

    // add an optional timeout to the command.
    OptionBuilder.withDescription(
            "Specifies a timeout in seconds. " + "If the program is not able to get the results in less than "
                    + "this amount of time it will stop and return with an error code");
    OptionBuilder.hasArg();
    OptionBuilder.withLongOpt("timeout");
    options.addOption(OptionBuilder.create());

    CommandLineParser parser = new PosixParser();
    try {
        CommandLine cl = parser.parse(options, args);

        Mmdpctl mmdpctl = new Mmdpctl();

        // check if the user sets a (correct) timeout.
        if (cl.hasOption("timeout")) {
            String timeoutString = cl.getOptionValue("timeout");
            Integer timeout = Integer.parseInt(timeoutString);
            if (timeout > 0) {
                log.info("Installing a timeout of {} seconds", timeout);
                mmdpctl.setTimeout(timeout);
            } else {
                System.out.println("The timeout needs to be a positive number, bigger than 0.");
                System.exit(1);
            }
        }

        if (cl.hasOption("list-dps")) {
            System.exit(mmdpctl.execute(new ListDatapathsCommand()));
        } else if (cl.hasOption("show-dp")) {
            System.exit(mmdpctl.execute(new GetDatapathCommand(cl.getOptionValue("show-dp"))));
        } else if (cl.hasOption("dump-dp")) {
            System.exit(mmdpctl.execute(new DumpDatapathCommand(cl.getOptionValue("dump-dp"))));
        } else if (cl.hasOption("add-dp")) {
            System.exit(mmdpctl.execute(new AddDatapathCommand(cl.getOptionValue("add-dp"))));
        } else if (cl.hasOption("delete-dp")) {
            System.exit(mmdpctl.execute(new DeleteDatapathCommand(cl.getOptionValue("delete-dp"))));
        }

    } catch (ParseException e) {
        showHelpAndExit(options, e.getMessage());
    }

    System.exit(0);
}

From source file:org.nuunframework.cli.NuunCliPlugin.java

private Option createOptionFromField(Field field) {
    Option option = null;//from  w w  w.  j a v a2 s  .  com
    // Cli Option Builder is completly static :-/
    // so we synchronized it ...
    synchronized (OptionBuilder.class) {
        // reset the builder creating a dummy option
        OptionBuilder.withLongOpt("dummy");
        OptionBuilder.create();

        // 
        NuunOption nuunOption = field.getAnnotation(NuunOption.class);

        if (nuunOption == null) {
            for (Annotation anno : field.getAnnotations()) {
                if (AssertUtils.hasAnnotationDeep(anno.annotationType(), NuunOption.class)) {
                    nuunOption = AssertUtils.annotationProxyOf(NuunOption.class, anno);
                    break;
                }
            }
        }

        // longopt
        if (!Strings.isNullOrEmpty(nuunOption.longOpt())) {
            OptionBuilder.withLongOpt(nuunOption.longOpt());
        }
        // description
        if (!Strings.isNullOrEmpty(nuunOption.description())) {
            OptionBuilder.withDescription(nuunOption.description());
        }
        // required
        OptionBuilder.isRequired((nuunOption.required()));

        // arg
        OptionBuilder.hasArg((nuunOption.arg()));

        // args
        if (nuunOption.args()) {
            if (nuunOption.numArgs() > 0) {
                OptionBuilder.hasArgs(nuunOption.numArgs());
            } else {
                OptionBuilder.hasArgs();
            }
        }
        // is optional 
        if (nuunOption.optionalArg()) {
            OptionBuilder.hasOptionalArg();
        }

        // nuun 
        OptionBuilder.withValueSeparator(nuunOption.valueSeparator());

        // opt 
        if (!Strings.isNullOrEmpty(nuunOption.opt())) {
            option = OptionBuilder.create(nuunOption.opt());
        } else {
            option = OptionBuilder.create();
        }
    }

    return option;

}

From source file:org.stem.tools.cli.Utils.java

@SuppressWarnings("all")
static Option newOpt(String longOpt, String name) {
    OptionBuilder builder = OptionBuilder.withLongOpt(longOpt);
    if (null != name)
        builder.withArgName(name).hasArg();

    return builder.create();
}

From source file:org.waveprotocol.wave.examples.fedone.FlagBinder.java

/**
 * Parse command line arguments./*from   w  w  w .  ja v  a2s  .  c  om*/
 *
 * @param args argv from command line
 * @return a Guice module configured with flag support.
 * @throws ParseException on bad command line args
 */
public static Module parseFlags(String[] args, Class<?>... flagSettings) throws ParseException {
    Options options = new Options();

    List<Field> fields = new ArrayList<Field>();
    for (Class<?> settings : flagSettings) {
        fields.addAll(Arrays.asList(settings.getDeclaredFields()));
    }

    // Reflect on flagSettings class and absorb flags
    final Map<Flag, Field> flags = new LinkedHashMap<Flag, Field>();
    for (Field field : fields) {
        if (!field.isAnnotationPresent(Flag.class)) {
            continue;
        }

        // Validate target type
        if (!supportedFlagTypes.contains(field.getType())) {
            throw new IllegalArgumentException(
                    field.getType() + " is not one of the supported flag types " + supportedFlagTypes);
        }

        Flag flag = field.getAnnotation(Flag.class);
        OptionBuilder.withLongOpt(flag.name());
        OptionBuilder.hasArg();
        final OptionBuilder option = OptionBuilder.withArgName(flag.name().toUpperCase());
        if (flag.defaultValue().isEmpty()) {
            OptionBuilder.withDescription(flag.description());
        } else {
            OptionBuilder.withDescription(flag.description() + "(default: " + flag.defaultValue() + ")");
        }

        options.addOption(OptionBuilder.create());

        flags.put(flag, field);
    }

    // Parse up our cmd line
    CommandLineParser parser = new PosixParser();
    final CommandLine cmd = parser.parse(options, args);

    // Now validate them
    for (Flag flag : flags.keySet()) {
        if (flag.defaultValue().isEmpty()) {
            String help = !"".equals(flag.description()) ? flag.description() : flag.name();
            mandatoryOption(cmd, flag.name(), "must supply " + help, options);
        }
    }

    // bundle everything up in an injectable guice module
    return new AbstractModule() {

        @Override
        protected void configure() {
            // We must iterate the flags a third time when binding.
            // Note: do not collapse these loops as that will damage
            // early error detection. The runtime is still O(n) in flag count.
            for (Map.Entry<Flag, Field> entry : flags.entrySet()) {
                Class<?> type = entry.getValue().getType();
                Flag flag = entry.getKey();

                // Skip non-mandatory, missing flags.
                //          if (!flag.mandatory()) {
                //            continue;
                //          }

                String flagValue = cmd.getOptionValue(flag.name());
                // Coerce String flag or defaultValue into target type.
                // NOTE(dhanji): only supported types are int, String and boolean.
                if (flagValue == null ||
                // The empty string is a valid value for a string type.
                (flagValue.isEmpty() && (int.class.equals(type) || boolean.class.equals(type)))) {
                    // Use the default.
                    if (int.class.equals(type)) {
                        bindConstant().annotatedWith(Names.named(flag.name()))
                                .to(Integer.parseInt(flag.defaultValue()));
                    } else if (boolean.class.equals(type)) {
                        bindConstant().annotatedWith(Names.named(flag.name()))
                                .to(Boolean.parseBoolean(flag.defaultValue()));
                    } else {
                        bindConstant().annotatedWith(Names.named(flag.name())).to(flag.defaultValue());
                    }
                } else {
                    if (int.class.equals(type)) {
                        bindConstant().annotatedWith(Names.named(flag.name())).to(Integer.parseInt(flagValue));
                    } else if (boolean.class.equals(type)) {
                        bindConstant().annotatedWith(Names.named(flag.name()))
                                .to(Boolean.parseBoolean(flagValue));
                    } else {
                        bindConstant().annotatedWith(Names.named(flag.name())).to(flagValue);
                    }
                }
            }
        }
    };
}