List of usage examples for org.apache.commons.cli Option setOptionalArg
public void setOptionalArg(boolean optionalArg)
From source file:in.hatimi.nosh.support.CmdLineManager.java
private Option optionFromField(Field field) { CmdLineOption clo = field.getAnnotation(CmdLineOption.class); if (clo == null) { return null; }//from w w w .ja v a 2 s .co m Option option = new Option(clo.name(), clo.description()); //Option option = new Option(clo.name(), clo.longName(), clo.argCount() > 0, clo.description()); if (StringUtils.isNotBlank(clo.longName())) { option.setLongOpt(clo.longName()); } //option.set` option.setArgs(clo.argCount()); option.setRequired(clo.required()); option.setOptionalArg(clo.optionalArg()); option.setValueSeparator(clo.valueSeparator()); return option; }
From source file:de.hsos.ecs.richwps.wpsmonitor.boundary.cli.command.annotation.CommandAnnotationProcessor.java
private void addOptions(final Command cmd) throws CommandAnnotationProcessorException { final List<Field> fields = getFields(cmd.getClass()); for (final Field field : fields) { final CommandOption annotation = field.getAnnotation(CommandOption.class); if (annotation != null) { final String longOpt = annotation.longOptionName().equals("") ? null : annotation.longOptionName(); Option option = new Option(annotation.shortOptionName(), longOpt, annotation.hasArgument(), annotation.description()); if (annotation.hasArgument()) { option.setArgName(annotation.argumentName()); } else { if (!field.getType().equals(Boolean.class)) { throw new CommandAnnotationProcessorException( "If the Option has no argument, the field must be of type boolean."); }/*from w ww . j a va 2 s . c o m*/ } option.setOptionalArg(annotation.optionalArgument()); cmd.addOption(option); } } }
From source file:de.bmw.yamaica.common.console.CommandExecuter.java
private Option createOption(IConfigurationElement optionConfiguration) { String shortName = optionConfiguration.getAttribute(OPTION_SHORT_NAME_ATTRIBUTE_NAME); String longName = optionConfiguration.getAttribute(OPTION_LONG_NAME_ATTRIBUTE_NAME); String description = optionConfiguration.getAttribute(OPTION_DESCRIPTION_ATTRIBUTE_NAME); String required = optionConfiguration.getAttribute(OPTION_REQUIRED_ATTRIBUTE_NAME); String argCount = optionConfiguration.getAttribute(OPTION_ARG_COUNT_ATTRIBUTE_NAME); String argName = optionConfiguration.getAttribute(OPTION_ARG_NAME_ATTRIBUTE_NAME); String hasOptionalArg = optionConfiguration.getAttribute(OPTION_HAS_OPTIONAL_ARG_ATTRIBUTE_NAME); String valueSeparator = optionConfiguration.getAttribute(OPTION_VALUE_SEPARATOR_ATTRIBUTE_NAME); Option option = new Option(shortName, description); option.setRequired(Boolean.parseBoolean(required)); option.setArgs(Integer.parseInt(argCount)); option.setOptionalArg(Boolean.parseBoolean(hasOptionalArg)); if (null != longName) { option.setLongOpt(longName);//from w ww . jav a 2 s . c o m } if (null != argName) { option.setArgName(argName); } if (null != valueSeparator) { option.setValueSeparator(valueSeparator.charAt(0)); } return option; }
From source file:com.trackplus.tools.DBConnectionTester.java
private void parseCommandLineArguments(String[] args) { Option option = new Option("h", "Print this help message."); option.setLongOpt("help"); options.addOption(option);//from ww w .ja v a 2s .c o m option = new Option("v", "Turn on verbose output."); option.setLongOpt("verbose"); options.addOption(option); options.addOption(newOption("u", "Database user name.", "user", "database user")); options.addOption(newOption("c", "JDBC connector class.", "jdbc", "class name")); options.addOption(newOption("url", "Use this user name for login.", "url", "JDBC URL")); options.addOption(newOption("p", "Database user password.", "password", "password")); option.setOptionalArg(true); options.addOption(option); CommandLineParser parser = new GnuParser(); CommandLine line = null; try { line = parser.parse(options, args); } catch (ParseException e) { System.err.println("Error: Parsing failed. Reason: " + e.getMessage()); System.exit(1); } if (line != null) { if (line.hasOption("h")) { printUsage(); System.exit(0); } if (line.hasOption("v")) { verbose = true; } if (line.hasOption("u")) { user = line.getOptionValue("u", ""); } if (line.hasOption("c")) { jdbc = line.getOptionValue("c", ""); } if (line.hasOption("url")) { url = line.getOptionValue("url", ""); } if (line.hasOption("p")) { password = line.getOptionValue("p"); if (password == null) { try { System.out.print("Password: "); System.out.flush(); password = new BufferedReader(new InputStreamReader(System.in)).readLine(); } catch (IOException e) { System.out.println(); System.exit(1); } } } } return; }
From source file:com.oneops.boo.BooCli.java
/** * Instantiates a new boo cli./*from ww w . j a va 2 s .c o m*/ */ public BooCli() { Option help = new Option("h", "help", false, "show help."); Option create = Option.builder("c").longOpt("create").desc( "Create a new Assembly specified by -f. If Assembly automatic naming is enabled, each invocation will create a new Assembly.") .build(); Option update = Option.builder("u").longOpt("update").desc("Update configurations specified by -f.") .build(); Option status = Option.builder("s").longOpt("status").desc("Get status of deployments specified by -f") .build(); Option config = Option.builder("f").longOpt("config-file").argName("FILE").hasArg() .desc("Use specified Boo YAML file").build(); Option cleanup = Option.builder("r").longOpt("remove") .desc("Remove all deployed configurations specified by -f").build(); Option list = Option.builder("l").longOpt("list").numberOfArgs(1).optionalArg(Boolean.TRUE) .desc("Return a list of instances applicable to the identifier provided..").build(); Option force = Option.builder().longOpt("force").desc("Do not prompt for --remove").build(); Option nodeploy = Option.builder().longOpt("no-deploy").desc("Create assembly without deployments").build(); Option getIps = Option.builder().longOpt("get-ips").argName("environment> <compute-class") .desc("Get IPs of deployed nodes specified by -f; Args are optional.").build(); getIps.setOptionalArg(true); getIps.setArgs(Option.UNLIMITED_VALUES); Option retry = Option.builder().longOpt("retry").desc("Retry deployments of configurations specified by -f") .build(); Option quiet = Option.builder().longOpt("quiet").desc("Silence the textual output.").build(); Option assembly = Option.builder("a").longOpt("assembly").hasArg().desc("Override the assembly name.") .build(); Option action = Option.builder().longOpt("procedure").numberOfArgs(3).optionalArg(Boolean.TRUE) .argName("platform> <component> <action") .desc("Execute actions. Use 'list' as an action to show available actions.").build(); Option procedureArguments = Option.builder().longOpt("procedure-arguments").argName("arglist").hasArg() .desc("Arguments to pass to the procedure call. Example: '{\"backup_type\":\"incremental\"}'") .build(); Option instanceList = Option.builder().longOpt("procedure-instances").argName("instanceList").hasArg().desc( "Comma-separated list of component instance names. 'list' to show all available component instances.") .build(); Option stepSize = Option.builder().longOpt("procedure-step-size").argName("size").hasArg() .desc("Percent of nodes to perform procedure on, default is 100.").build(); Option comment = Option.builder("m").longOpt("message").argName("description").hasArg() .desc("Customize the comment for deployments").build(); Option view = Option.builder("v").longOpt("view").desc("View interpolated Boo YAML template").build(); Option profile = Option.builder("p").longOpt("profile").argName("PROFILE").hasArg() .desc("Choose specific profile from ~/.boo/config").build(); options.addOption(help); options.addOption(config); options.addOption(create); options.addOption(update); options.addOption(status); options.addOption(list); options.addOption(cleanup); options.addOption(getIps); options.addOption(retry); options.addOption(quiet); options.addOption(force); options.addOption(nodeploy); options.addOption(assembly); options.addOption(action); options.addOption(procedureArguments); options.addOption(instanceList); options.addOption(stepSize); options.addOption(comment); options.addOption(view); options.addOption(profile); }
From source file:com.hablutzel.cmdline.CommandLineApplication.java
/** * This method scans the subclass for annotations * that denote the command line options and arguments, * and configures the systems so that the members that * have been annotated in that way are set up for calling * at command line processing time//from w w w.jav a2 s . c o m * */ private final void configure() throws CommandLineException { // Find all the fields in our subclass for (Method method : this.getClass().getDeclaredMethods()) { // If this method is marked with a command line option, then configure // a corresponding commons-cli command line option here if (method.isAnnotationPresent(CommandLineOption.class)) { CommandLineOption commandLineOption = method.getDeclaredAnnotation(CommandLineOption.class); if (commandLineOption != null) { // Get the basic information about the option - the name and description String shortName = commandLineOption.shortForm().equals("") ? null : commandLineOption.shortForm(); String longName = commandLineOption.longForm().equals("") ? null : commandLineOption.longForm(); String description = commandLineOption.usage(); // If both the short and long name are null, then use the field name as the long name if (shortName == null && longName == null) { longName = method.getName(); } // The signature of the method determines what kind of command line // option is allowed. Basically, if the method does not take an argument, // then the option does not take arguments either. In this case, the // method is just called when the option is present. // // If the method does take argument, there are restrictions on the arguments // that are allowed. If there is a single argument, then the method will be // called for each argument supplied to the option. Generally in this case you // want the maximum number of option arguments to be 1, and you are just getting // the value of the argument. On the other hand, if the single argument is either // and array or a List<>, then the arguments will be passed in as an argument // or list respectively. // // Methods with more than 1 argument are not allowed. Methods with return types // other than boolean are not allowed. Methods that throw an exception other than // org.apache.commons.cli.CommandLineException are not allowed, // // If the method returns a boolean, and calling that method returns FALSE, then the // command line main function will not be called. // // The class of the argument has to be convertable using common-beanutils // conversion facilities CommandLineMethodHelper helper = getHelperForCommandOption(method, commandLineOption); // Now create and configure an option based on what the method is capable of handling // and the command line option parameters boolean allowsArguments = helper.methodType != MethodType.Boolean; Option option = new Option(shortName, longName, allowsArguments, description); // Configure it option.setRequired(commandLineOption.required()); if (option.hasArg()) { option.setType(helper.elementType); option.setArgs(commandLineOption.maximumArgumentCount()); option.setValueSeparator(commandLineOption.argumentSeparator()); option.setOptionalArg(commandLineOption.optionalArgument()); } // Remember it, both in the commons-cli options set and // in our list of elements for later post-processing options.addOption(option); optionHelperMap.put(option, helper); } // This was not a command line option method - is it the main command line method? } else if (method.isAnnotationPresent(CommandLineMain.class)) { // Make sure we only have one if (mainHelper != null) { throw new CommandLineException("Cannot have two main methods specified"); } else { mainHelper = getHelperForCommandLineMain(method); } } } }
From source file:com.netcrest.pado.tools.pado.PadoShell.java
private void parseArgs(String args[]) { Options options = new Options(); Option opt = OptionBuilder.create("dir"); opt.setArgs(1);//from w w w.j a va 2 s.co m opt.setOptionalArg(true); options.addOption(opt); opt = OptionBuilder.create("i"); opt.setArgs(1); opt.setOptionalArg(true); options.addOption(opt); opt = OptionBuilder.create("e"); opt.setArgs(Option.UNLIMITED_VALUES); opt.setOptionalArg(true); options.addOption(opt); opt = OptionBuilder.create("f"); opt.setArgs(1); opt.setOptionalArg(true); options.addOption(opt); opt = OptionBuilder.create("jar"); opt.setArgs(1); opt.setOptionalArg(true); options.addOption(opt); opt = OptionBuilder.create("l"); opt.setArgs(1); opt.setOptionalArg(true); options.addOption(opt); opt = OptionBuilder.create("a"); opt.setArgs(1); opt.setOptionalArg(true); options.addOption(opt); opt = OptionBuilder.create("u"); opt.setArgs(1); opt.setOptionalArg(true); options.addOption(opt); opt = OptionBuilder.create("p"); opt.setArgs(1); opt.setOptionalArg(true); options.addOption(opt); // domain opt = OptionBuilder.create("d"); opt.setArgs(1); opt.setOptionalArg(true); options.addOption(opt); // history opt = OptionBuilder.create("h"); opt.setArgs(1); opt.setOptionalArg(true); options.addOption(opt); // editor (vi or emacs) - default vi opt = OptionBuilder.create("o"); opt.setArgs(1); opt.setOptionalArg(true); options.addOption(opt); options.addOption("n", false, ""); options.addOption("v", false, ""); options.addOption("?", false, ""); CommandLine commandLine = null; try { commandLine = cliParseCommandLine(options, args); } catch (Exception e) { Logger.error(e); } if (commandLine == null || commandLine.hasOption('?')) { usage(); exit(0); } if (commandLine.hasOption('v')) { PadoVersion padoVersion = new PadoVersion(); println("v" + padoVersion.getVersion()); exit(0); } if (commandLine.hasOption("dir") && commandLine.getOptionValue("dir") != null) { // jarDirectoryPath = commandLine.getOptionValue("dir"); // ignore dir. dir is handled by the shell script. } if (commandLine.hasOption("i") && commandLine.getOptionValue("i") != null) { inputFilePath = commandLine.getOptionValue("i"); } if (commandLine.hasOption("e") && commandLine.getOptionValue("e") != null) { inputCommands = commandLine.getOptionValues("e"); } if (commandLine.hasOption("f") && commandLine.getOptionValue("f") != null) { scriptFilePath = commandLine.getOptionValue("f"); } if (commandLine.hasOption("jar") && commandLine.getOptionValue("jar") != null) { jarPaths = commandLine.getOptionValue("jar"); } if (commandLine.hasOption("h") && commandLine.getOptionValue("h") != null) { historyFileName = commandLine.getOptionValue("h"); } if (commandLine.hasOption("o") && commandLine.getOptionValue("o") != null) { editorName = commandLine.getOptionValue("o"); // Only vi and emacs supported. Default to vi if a bad name. if (editorName.equalsIgnoreCase("vi") == false && editorName.equalsIgnoreCase("emacs")) { editorName = "vi"; } } locators = commandLine.getOptionValue("l"); appId = commandLine.getOptionValue("a"); user = commandLine.getOptionValue("u"); String pw = commandLine.getOptionValue("p"); if (pw != null) { password = pw.toCharArray(); } ignorePadoRcFile = commandLine.hasOption("n"); if (commandLine.hasOption("h")) { setHistoryPerSession(Boolean.TRUE); } interactiveMode = scriptFilePath == null && inputCommands == null; if (interactiveMode) { println(); println(PadoShellLogo.getPadoLogo()); println(PadoShellLogo.getCopyrights()); println(); } envProperties.putAll(System.getenv()); }
From source file:kieker.tools.bridge.cli.CLIServerMain.java
/** * Compile the options for the CLI server. * * @return The composed options for the CLI server *//*from w ww. j a v a 2 s.c o m*/ private static Options declareOptions() { options = new Options(); Option option; // Type selection option = new Option(CMD_TYPE, CMD_TYPE_LONG, true, "select the service type: tcp-client, tcp-server, tcp-single-server, jms-client, jms-embedded, http-rest"); option.setArgName("type"); option.setRequired(true); options.addOption(option); // TCP client option = new Option(CMD_HOST, CMD_HOST_LONG, true, "connect to server named <hostname>"); option.setArgName("hostname"); options.addOption(option); // TCP server option = new Option(CMD_PORT, CMD_PORT_LONG, true, "listen at port (tcp-server, jms-embedded, or http-rest) or connect to port (tcp-client)"); option.setArgName("number"); option.setType(Number.class); options.addOption(option); // JMS client option = new Option(CMD_USER, CMD_USER_LONG, true, "user name for a JMS service"); option.setArgName("username"); options.addOption(option); option = new Option(CMD_PASSWORD, CMD_PASSWORD_LONG, true, "password for a JMS service"); option.setArgName("password"); options.addOption(option); option = new Option(CMD_URL, CMD_URL_LONG, true, "URL for JMS server or HTTP servlet"); option.setArgName("jms-url"); option.setType(URL.class); options.addOption(option); // HTTP client option = new Option(CMD_CONTEXT, CMD_CONTEXT_LONG, true, "context for the HTTP servlet"); option.setArgName("context"); options.addOption(option); // kieker configuration file option = new Option(CMD_KIEKER_CONFIGURATION, CMD_KIEKER_CONFIGURATION_LONG, true, "kieker configuration file"); option.setArgName("configuration"); options.addOption(option); // mapping file for TCP and JMS option = new Option(CMD_MAP_FILE, CMD_MAP_FILE_LONG, true, "Class name to id (integer or string) mapping"); option.setArgName("map-file"); option.setType(File.class); option.setRequired(true); options.addOption(option); // libraries option = new Option(CMD_LIBRARIES, CMD_LIBRARIES_LONG, true, "List of library paths separated by " + File.pathSeparatorChar); option.setArgName("paths"); option.setType(File.class); option.setRequired(true); option.setValueSeparator(File.pathSeparatorChar); options.addOption(option); // verbose option = new Option(CMD_VERBOSE, CMD_VERBOSE_LONG, true, "output processing information"); option.setRequired(false); option.setOptionalArg(true); options.addOption(option); // statistics option = new Option(CMD_STATS, CMD_STATS_LONG, false, "output performance statistics"); option.setRequired(false); options.addOption(option); // daemon mode option = new Option(CMD_DAEMON, CMD_DAEMON_LONG, false, "detach from console; TCP server allows multiple connections"); option.setRequired(false); options.addOption(option); return options; }
From source file:com.trsst.Command.java
@SuppressWarnings("static-access") private void buildOptions(String[] argv, PrintStream out, InputStream in) { // NOTE: OptionsBuilder is NOT thread-safe // which was causing us random failures. Option o; portOptions = new Options(); o = new Option(null, "Specify port"); o.setRequired(false);//from w ww .ja v a 2s.c om o.setArgs(1); o.setLongOpt("port"); portOptions.addOption(o); o = new Option(null, "Expose client API"); o.setRequired(false); o.setArgs(0); o.setLongOpt("api"); portOptions.addOption(o); o = new Option(null, "Launch embedded GUI"); o.setRequired(false); o.setArgs(0); o.setLongOpt("gui"); portOptions.addOption(o); o = new Option(null, "Turn off SSL"); o.setRequired(false); o.setArgs(0); o.setLongOpt("clear"); portOptions.addOption(o); o = new Option(null, "Use TOR (experimental)"); o.setRequired(false); o.setArgs(0); o.setLongOpt("tor"); portOptions.addOption(o); pullOptions = new Options(); o = new Option("h", "Set host server for this operation"); o.setRequired(false); o.setArgs(1); o.setArgName("url"); o.setLongOpt("host"); pullOptions.addOption(o); o = new Option("d", "Decrypt entries as specified recipient id"); o.setRequired(false); o.setArgs(1); o.setArgName("id"); o.setLongOpt("decrypt"); pullOptions.addOption(o); postOptions = new Options(); o = new Option("a", "Attach the specified file, or - for std input"); o.setRequired(false); o.setOptionalArg(true); o.setArgName("file"); o.setLongOpt("attach"); postOptions.addOption(o); o = new Option("b", "Set base URL for this feed"); o.setRequired(false); o.setArgs(1); o.setArgName("url"); o.setLongOpt("base"); postOptions.addOption(o); o = new Option("p", "Specify passphrase on the command line"); o.setRequired(false); o.setArgs(1); o.setArgName("text"); o.setLongOpt("pass"); postOptions.addOption(o); o = new Option("s", "Specify status update on command line"); o.setRequired(false); o.setArgs(1); o.setArgName("text"); o.setLongOpt("status"); postOptions.addOption(o); o = new Option("u", "Attach the specified url to the new entry"); o.setRequired(false); o.setArgs(1); o.setArgName("url"); o.setLongOpt("url"); postOptions.addOption(o); o = new Option("v", "Specify an activitystreams verb for this entry"); o.setRequired(false); o.setArgs(1); o.setArgName("verb"); o.setLongOpt("verb"); postOptions.addOption(o); o = new Option("r", "Add a mention"); o.setRequired(false); o.setArgs(1); o.setArgName("id"); o.setLongOpt("mention"); postOptions.addOption(o); o = new Option("g", "Add a tag"); o.setRequired(false); o.setArgs(1); o.setArgName("text"); o.setLongOpt("tag"); postOptions.addOption(o); o = new Option("c", "Specify entry content on command line"); o.setRequired(false); o.setArgs(1); o.setArgName("text"); o.setLongOpt("content"); postOptions.addOption(o); o = new Option("t", "Set this feed's title"); o.setRequired(false); o.setArgs(1); o.setArgName("text"); o.setLongOpt("title"); postOptions.addOption(o); o = new Option(null, "Set this feed's subtitle"); o.setRequired(false); o.setArgs(1); o.setArgName("text"); o.setLongOpt("subtitle"); postOptions.addOption(o); o = new Option("n", "Set this feed's author name"); o.setRequired(false); o.setArgs(1); o.setArgName("text"); o.setLongOpt("name"); postOptions.addOption(o); o = new Option(null, "Set this feed's author uri"); o.setRequired(false); o.setArgs(1); o.setArgName("uri"); o.setLongOpt("uri"); postOptions.addOption(o); o = new Option("e", "Encrypt entry for specified public key"); o.setRequired(false); o.setArgs(1); o.setArgName("pubkey"); o.setLongOpt("encrypt"); postOptions.addOption(o); o = new Option("m", "Set this feed's author email"); o.setRequired(false); o.setArgs(1); o.setArgName("email"); o.setLongOpt("email"); postOptions.addOption(o); o = new Option("i", "Set as this feed's icon or specify url"); o.setRequired(false); o.setArgs(1); o.setArgName("url"); o.setLongOpt("icon"); postOptions.addOption(o); o = new Option("l", "Set as this feed's logo or specify url"); o.setRequired(false); o.setArgs(1); o.setArgName("url"); o.setLongOpt("logo"); postOptions.addOption(o); o = new Option(null, "Generate feed id with specified prefix"); o.setRequired(false); o.setArgs(1); o.setArgName("prefix"); o.setLongOpt("vanity"); postOptions.addOption(o); o = new Option(null, "Require SSL certs"); o.setRequired(false); o.setArgs(0); o.setLongOpt("strict"); postOptions.addOption(o); // merge options parameters mergedOptions = new Options(); for (Object obj : pullOptions.getOptions()) { mergedOptions.addOption((Option) obj); } for (Object obj : postOptions.getOptions()) { mergedOptions.addOption((Option) obj); } for (Object obj : portOptions.getOptions()) { mergedOptions.addOption((Option) obj); } helpOption = OptionBuilder.isRequired(false).withLongOpt("help").withDescription("Display these options") .create('?'); mergedOptions.addOption(helpOption); }
From source file:org.apache.activemq.apollo.util.cli.OptionBuilder.java
public Option op() { Option option = new Option(id != null ? id : " ", description); option.setLongOpt(name);// w w w .j a v a 2s. co m option.setRequired(required); option.setOptionalArg(optional); option.setType(type); option.setValueSeparator(sperator); if (arg != null && args == -1) { args = 1; } option.setArgs(args); option.setArgName(arg); return option; }