List of usage examples for org.apache.commons.cli Option setValueSeparator
public void setValueSeparator(char sep)
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);//from ww w . j ava 2 s . c om 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; }
From source file:org.apache.hadoop.yarn.client.cli.ApplicationCLI.java
@Override public int run(String[] args) throws Exception { Options opts = new Options(); String title = null;/* w w w.j a v a 2 s.c o m*/ if (args.length > 0 && args[0].equalsIgnoreCase(APPLICATION)) { title = APPLICATION; opts.addOption(STATUS_CMD, true, "Prints the status of the application."); opts.addOption(LIST_CMD, false, "List applications. " + "Supports optional use of -appTypes to filter applications " + "based on application type, " + "and -appStates to filter applications based on application state."); opts.addOption(MOVE_TO_QUEUE_CMD, true, "Moves the application to a " + "different queue."); opts.addOption(QUEUE_CMD, true, "Works with the movetoqueue command to" + " specify which queue to move an application to."); opts.addOption(HELP_CMD, false, "Displays help for all commands."); Option appTypeOpt = new Option(APP_TYPE_CMD, true, "Works with -list to " + "filter applications based on " + "input comma-separated list of application types."); appTypeOpt.setValueSeparator(','); appTypeOpt.setArgs(Option.UNLIMITED_VALUES); appTypeOpt.setArgName("Types"); opts.addOption(appTypeOpt); Option appStateOpt = new Option(APP_STATE_CMD, true, "Works with -list " + "to filter applications based on input comma-separated list of " + "application states. " + getAllValidApplicationStates()); appStateOpt.setValueSeparator(','); appStateOpt.setArgs(Option.UNLIMITED_VALUES); appStateOpt.setArgName("States"); opts.addOption(appStateOpt); opts.addOption(APP_ID, true, "Specify Application Id to be operated"); opts.addOption(UPDATE_PRIORITY, true, "update priority of an application. ApplicationId can be" + " passed using 'appId' option."); Option killOpt = new Option(KILL_CMD, true, "Kills the application. " + "Set of applications can be provided separated with space"); killOpt.setValueSeparator(' '); killOpt.setArgs(Option.UNLIMITED_VALUES); killOpt.setArgName("Application ID"); opts.addOption(killOpt); opts.getOption(MOVE_TO_QUEUE_CMD).setArgName("Application ID"); opts.getOption(QUEUE_CMD).setArgName("Queue Name"); opts.getOption(STATUS_CMD).setArgName("Application ID"); opts.getOption(APP_ID).setArgName("Application ID"); opts.getOption(UPDATE_PRIORITY).setArgName("Priority"); } else if (args.length > 0 && args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) { title = APPLICATION_ATTEMPT; opts.addOption(STATUS_CMD, true, "Prints the status of the application attempt."); opts.addOption(LIST_CMD, true, "List application attempts for application."); opts.addOption(FAIL_CMD, true, "Fails application attempt."); opts.addOption(HELP_CMD, false, "Displays help for all commands."); opts.getOption(STATUS_CMD).setArgName("Application Attempt ID"); opts.getOption(LIST_CMD).setArgName("Application ID"); opts.getOption(FAIL_CMD).setArgName("Application Attempt ID"); } else if (args.length > 0 && args[0].equalsIgnoreCase(CONTAINER)) { title = CONTAINER; opts.addOption(STATUS_CMD, true, "Prints the status of the container."); opts.addOption(LIST_CMD, true, "List containers for application attempt."); opts.addOption(HELP_CMD, false, "Displays help for all commands."); opts.getOption(STATUS_CMD).setArgName("Container ID"); opts.getOption(LIST_CMD).setArgName("Application Attempt ID"); opts.addOption(SIGNAL_CMD, true, "Signal the container. The available signal commands are " + java.util.Arrays.asList(SignalContainerCommand.values()) + " Default command is OUTPUT_THREAD_DUMP."); opts.getOption(SIGNAL_CMD).setArgName("container ID [signal command]"); opts.getOption(SIGNAL_CMD).setArgs(3); } int exitCode = -1; CommandLine cliParser = null; try { cliParser = new GnuParser().parse(opts, args); } catch (MissingArgumentException ex) { sysout.println("Missing argument for options"); printUsage(title, opts); return exitCode; } if (cliParser.hasOption(STATUS_CMD)) { if (args.length != 3) { printUsage(title, opts); return exitCode; } if (args[0].equalsIgnoreCase(APPLICATION)) { exitCode = printApplicationReport(cliParser.getOptionValue(STATUS_CMD)); } else if (args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) { exitCode = printApplicationAttemptReport(cliParser.getOptionValue(STATUS_CMD)); } else if (args[0].equalsIgnoreCase(CONTAINER)) { exitCode = printContainerReport(cliParser.getOptionValue(STATUS_CMD)); } return exitCode; } else if (cliParser.hasOption(LIST_CMD)) { if (args[0].equalsIgnoreCase(APPLICATION)) { allAppStates = false; Set<String> appTypes = new HashSet<String>(); if (cliParser.hasOption(APP_TYPE_CMD)) { String[] types = cliParser.getOptionValues(APP_TYPE_CMD); if (types != null) { for (String type : types) { if (!type.trim().isEmpty()) { appTypes.add(StringUtils.toUpperCase(type).trim()); } } } } EnumSet<YarnApplicationState> appStates = EnumSet.noneOf(YarnApplicationState.class); if (cliParser.hasOption(APP_STATE_CMD)) { String[] states = cliParser.getOptionValues(APP_STATE_CMD); if (states != null) { for (String state : states) { if (!state.trim().isEmpty()) { if (state.trim().equalsIgnoreCase(ALLSTATES_OPTION)) { allAppStates = true; break; } try { appStates.add( YarnApplicationState.valueOf(StringUtils.toUpperCase(state).trim())); } catch (IllegalArgumentException ex) { sysout.println("The application state " + state + " is invalid."); sysout.println(getAllValidApplicationStates()); return exitCode; } } } } } listApplications(appTypes, appStates); } else if (args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) { if (args.length != 3) { printUsage(title, opts); return exitCode; } listApplicationAttempts(cliParser.getOptionValue(LIST_CMD)); } else if (args[0].equalsIgnoreCase(CONTAINER)) { if (args.length != 3) { printUsage(title, opts); return exitCode; } listContainers(cliParser.getOptionValue(LIST_CMD)); } } else if (cliParser.hasOption(KILL_CMD)) { if (args.length < 3 || hasAnyOtherCLIOptions(cliParser, opts, KILL_CMD)) { printUsage(title, opts); return exitCode; } return killApplication(cliParser.getOptionValues(KILL_CMD)); } else if (cliParser.hasOption(MOVE_TO_QUEUE_CMD)) { if (!cliParser.hasOption(QUEUE_CMD)) { printUsage(title, opts); return exitCode; } moveApplicationAcrossQueues(cliParser.getOptionValue(MOVE_TO_QUEUE_CMD), cliParser.getOptionValue(QUEUE_CMD)); } else if (cliParser.hasOption(FAIL_CMD)) { if (!args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) { printUsage(title, opts); return exitCode; } failApplicationAttempt(cliParser.getOptionValue(FAIL_CMD)); } else if (cliParser.hasOption(HELP_CMD)) { printUsage(title, opts); return 0; } else if (cliParser.hasOption(UPDATE_PRIORITY)) { if (!cliParser.hasOption(APP_ID)) { printUsage(title, opts); return exitCode; } updateApplicationPriority(cliParser.getOptionValue(APP_ID), cliParser.getOptionValue(UPDATE_PRIORITY)); } else if (cliParser.hasOption(SIGNAL_CMD)) { if (args.length < 3 || args.length > 4) { printUsage(title, opts); return exitCode; } final String[] signalArgs = cliParser.getOptionValues(SIGNAL_CMD); final String containerId = signalArgs[0]; SignalContainerCommand command = SignalContainerCommand.OUTPUT_THREAD_DUMP; if (signalArgs.length == 2) { command = SignalContainerCommand.valueOf(signalArgs[1]); } signalToContainer(containerId, command); } else { syserr.println("Invalid Command Usage : "); printUsage(title, opts); } return 0; }
From source file:org.apache.hadoop.yarn.client.cli.LogsCLI.java
@Override public int run(String[] args) throws Exception { Options opts = new Options(); opts.addOption(HELP_CMD, false, "Displays help for all commands."); Option appIdOpt = new Option(APPLICATION_ID_OPTION, true, "ApplicationId (required)"); appIdOpt.setRequired(true);/*from ww w. j ava 2 s . c o m*/ opts.addOption(appIdOpt); opts.addOption(CONTAINER_ID_OPTION, true, "ContainerId. " + "By default, it will only print syslog if the application is runing." + " Work with -logFiles to get other logs."); opts.addOption(NODE_ADDRESS_OPTION, true, "NodeAddress in the format " + "nodename:port"); opts.addOption(APP_OWNER_OPTION, true, "AppOwner (assumed to be current user if not specified)"); Option amOption = new Option(AM_CONTAINER_OPTION, true, "Prints the AM Container logs for this application. " + "Specify comma-separated value to get logs for related AM Container. " + "For example, If we specify -am 1,2, we will get the logs for " + "the first AM Container as well as the second AM Container. " + "To get logs for all AM Containers, use -am ALL. " + "To get logs for the latest AM Container, use -am -1. " + "By default, it will only print out syslog. Work with -logFiles " + "to get other logs"); amOption.setValueSeparator(','); amOption.setArgs(Option.UNLIMITED_VALUES); amOption.setArgName("AM Containers"); opts.addOption(amOption); Option logFileOpt = new Option(CONTAINER_LOG_FILES, true, "Work with -am/-containerId and specify comma-separated value " + "to get specified container log files. Use \"ALL\" to fetch all the " + "log files for the container."); logFileOpt.setValueSeparator(','); logFileOpt.setArgs(Option.UNLIMITED_VALUES); logFileOpt.setArgName("Log File Name"); opts.addOption(logFileOpt); opts.getOption(APPLICATION_ID_OPTION).setArgName("Application ID"); opts.getOption(CONTAINER_ID_OPTION).setArgName("Container ID"); opts.getOption(NODE_ADDRESS_OPTION).setArgName("Node Address"); opts.getOption(APP_OWNER_OPTION).setArgName("Application Owner"); opts.getOption(AM_CONTAINER_OPTION).setArgName("AM Containers"); Options printOpts = new Options(); printOpts.addOption(opts.getOption(HELP_CMD)); printOpts.addOption(opts.getOption(CONTAINER_ID_OPTION)); printOpts.addOption(opts.getOption(NODE_ADDRESS_OPTION)); printOpts.addOption(opts.getOption(APP_OWNER_OPTION)); printOpts.addOption(opts.getOption(AM_CONTAINER_OPTION)); printOpts.addOption(opts.getOption(CONTAINER_LOG_FILES)); if (args.length < 1) { printHelpMessage(printOpts); return -1; } if (args[0].equals("-help")) { printHelpMessage(printOpts); return 0; } CommandLineParser parser = new GnuParser(); String appIdStr = null; String containerIdStr = null; String nodeAddress = null; String appOwner = null; boolean getAMContainerLogs = false; String[] logFiles = null; List<String> amContainersList = new ArrayList<String>(); try { CommandLine commandLine = parser.parse(opts, args, true); appIdStr = commandLine.getOptionValue(APPLICATION_ID_OPTION); containerIdStr = commandLine.getOptionValue(CONTAINER_ID_OPTION); nodeAddress = commandLine.getOptionValue(NODE_ADDRESS_OPTION); appOwner = commandLine.getOptionValue(APP_OWNER_OPTION); getAMContainerLogs = commandLine.hasOption(AM_CONTAINER_OPTION); if (getAMContainerLogs) { String[] amContainers = commandLine.getOptionValues(AM_CONTAINER_OPTION); for (String am : amContainers) { boolean errorInput = false; if (!am.trim().equalsIgnoreCase("ALL")) { try { int id = Integer.parseInt(am.trim()); if (id != -1 && id <= 0) { errorInput = true; } } catch (NumberFormatException ex) { errorInput = true; } if (errorInput) { System.err.println("Invalid input for option -am. Valid inputs are 'ALL', -1 " + "and any other integer which is larger than 0."); printHelpMessage(printOpts); return -1; } amContainersList.add(am.trim()); } else { amContainersList.add("ALL"); break; } } } if (commandLine.hasOption(CONTAINER_LOG_FILES)) { logFiles = commandLine.getOptionValues(CONTAINER_LOG_FILES); } } catch (ParseException e) { System.err.println("options parsing failed: " + e.getMessage()); printHelpMessage(printOpts); return -1; } if (appIdStr == null) { System.err.println("ApplicationId cannot be null!"); printHelpMessage(printOpts); return -1; } ApplicationId appId = null; try { appId = ApplicationId.fromString(appIdStr); } catch (Exception e) { System.err.println("Invalid ApplicationId specified"); return -1; } LogCLIHelpers logCliHelper = new LogCLIHelpers(); logCliHelper.setConf(getConf()); if (appOwner == null || appOwner.isEmpty()) { appOwner = UserGroupInformation.getCurrentUser().getShortUserName(); } YarnApplicationState appState = YarnApplicationState.NEW; try { appState = getApplicationState(appId); if (appState == YarnApplicationState.NEW || appState == YarnApplicationState.NEW_SAVING || appState == YarnApplicationState.SUBMITTED) { System.out.println("Logs are not avaiable right now."); return -1; } } catch (IOException | YarnException e) { System.err.println( "Unable to get ApplicationState." + " Attempting to fetch logs directly from the filesystem."); } // To get am logs if (getAMContainerLogs) { // if we do not specify the value for CONTAINER_LOG_FILES option, // we will only output syslog if (logFiles == null || logFiles.length == 0) { logFiles = new String[] { "syslog" }; } // If the application is running, we will call the RM WebService // to get the AppAttempts which includes the nodeHttpAddress // and containerId for all the AM Containers. // After that, we will call NodeManager webService to get the // related logs if (appState == YarnApplicationState.ACCEPTED || appState == YarnApplicationState.RUNNING) { return printAMContainerLogs(getConf(), appIdStr, amContainersList, logFiles, logCliHelper, appOwner, false); } else { // If the application is in the final state, we will call RM webservice // to get all AppAttempts information first. If we get nothing, // we will try to call AHS webservice to get related AppAttempts // which includes nodeAddress for the AM Containers. // After that, we will use nodeAddress and containerId // to get logs from HDFS directly. if (getConf().getBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED, YarnConfiguration.DEFAULT_APPLICATION_HISTORY_ENABLED)) { return printAMContainerLogs(getConf(), appIdStr, amContainersList, logFiles, logCliHelper, appOwner, true); } else { System.out.println("Can not get AMContainers logs for the application:" + appId); System.out.println("This application:" + appId + " is finished." + " Please enable the application history service. Or Using " + "yarn logs -applicationId <appId> -containerId <containerId> " + "--nodeAddress <nodeHttpAddress> to get the container logs"); return -1; } } } int resultCode = 0; if (containerIdStr != null) { // if we provide the node address and the application is in the final // state, we could directly get logs from HDFS. if (nodeAddress != null && isApplicationFinished(appState)) { // if user specified "ALL" as the logFiles param, pass null // to logCliHelper so that it fetches all the logs List<String> logs; if (logFiles == null) { logs = null; } else if (fetchAllLogFiles(logFiles)) { logs = null; } else { logs = Arrays.asList(logFiles); } return logCliHelper.dumpAContainersLogsForALogType(appIdStr, containerIdStr, nodeAddress, appOwner, logs); } try { // If the nodeAddress is not provided, we will try to get // the ContainerReport. In the containerReport, we could get // nodeAddress and nodeHttpAddress ContainerReport report = getContainerReport(containerIdStr); String nodeHttpAddress = report.getNodeHttpAddress() .replaceFirst(WebAppUtils.getHttpSchemePrefix(getConf()), ""); String nodeId = report.getAssignedNode().toString(); // If the application is not in the final state, // we will provide the NodeHttpAddress and get the container logs // by calling NodeManager webservice. if (!isApplicationFinished(appState)) { if (logFiles == null || logFiles.length == 0) { logFiles = new String[] { "syslog" }; } printContainerLogsFromRunningApplication(getConf(), appIdStr, containerIdStr, nodeHttpAddress, nodeId, logFiles, logCliHelper, appOwner); } else { String[] requestedLogFiles = logFiles; if (fetchAllLogFiles(logFiles)) { requestedLogFiles = null; } // If the application is in the final state, we will directly // get the container logs from HDFS. printContainerLogsForFinishedApplication(appIdStr, containerIdStr, nodeId, requestedLogFiles, logCliHelper, appOwner); } return resultCode; } catch (IOException | YarnException ex) { System.err.println( "Unable to get logs for this container:" + containerIdStr + "for the application:" + appId); if (!getConf().getBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED, YarnConfiguration.DEFAULT_APPLICATION_HISTORY_ENABLED)) { System.out.println("Please enable the application history service. Or "); } System.out.println("Using " + "yarn logs -applicationId <appId> -containerId <containerId> " + "--nodeAddress <nodeHttpAddress> to get the container logs"); return -1; } } else { if (nodeAddress == null) { resultCode = logCliHelper.dumpAllContainersLogs(appId, appOwner, System.out); } else { System.out.println("Should at least provide ContainerId!"); printHelpMessage(printOpts); resultCode = -1; } } return resultCode; }
From source file:org.apache.hadoop.yarn.client.cli.NodeCLI.java
@Override public int run(String[] args) throws Exception { Options opts = new Options(); opts.addOption(HELP_CMD, false, "Displays help for all commands."); opts.addOption(STATUS_CMD, true, "Prints the status report of the node."); opts.addOption(LIST_CMD, false,/* w w w .j a va2 s . co m*/ "List all running nodes. " + "Supports optional use of -states to filter nodes " + "based on node state, all -all to list all nodes, " + "-showDetails to display more details about each node."); Option nodeStateOpt = new Option(NODE_STATE_CMD, true, "Works with -list to filter nodes based on input comma-separated " + "list of node states. " + getAllValidNodeStates()); nodeStateOpt.setValueSeparator(','); nodeStateOpt.setArgs(Option.UNLIMITED_VALUES); nodeStateOpt.setArgName("States"); opts.addOption(nodeStateOpt); Option allOpt = new Option(NODE_ALL, false, "Works with -list to list all nodes."); opts.addOption(allOpt); Option showDetailsOpt = new Option(NODE_SHOW_DETAILS, false, "Works with -list to show more details about each node."); opts.addOption(showDetailsOpt); opts.getOption(STATUS_CMD).setArgName("NodeId"); if (args != null && args.length > 0) { for (int i = args.length - 1; i >= 0; i--) { if (args[i].equalsIgnoreCase("-" + NODE_ALL)) { args[i] = "-" + NODE_ALL; } } } int exitCode = -1; CommandLine cliParser = null; try { cliParser = new GnuParser().parse(opts, args); } catch (MissingArgumentException ex) { sysout.println("Missing argument for options"); printUsage(opts); return exitCode; } if (cliParser.hasOption("status")) { if (args.length != 2) { printUsage(opts); return exitCode; } printNodeStatus(cliParser.getOptionValue("status")); } else if (cliParser.hasOption("list")) { Set<NodeState> nodeStates = new HashSet<NodeState>(); if (cliParser.hasOption(NODE_ALL)) { for (NodeState state : NodeState.values()) { nodeStates.add(state); } } else if (cliParser.hasOption(NODE_STATE_CMD)) { String[] types = cliParser.getOptionValues(NODE_STATE_CMD); if (types != null) { for (String type : types) { if (!type.trim().isEmpty()) { try { nodeStates.add(NodeState .valueOf(org.apache.hadoop.util.StringUtils.toUpperCase(type.trim()))); } catch (IllegalArgumentException ex) { sysout.println("The node state " + type + " is invalid."); sysout.println(getAllValidNodeStates()); return exitCode; } } } } } else { nodeStates.add(NodeState.RUNNING); } // List all node details with more information. if (cliParser.hasOption(NODE_SHOW_DETAILS)) { listDetailedClusterNodes(nodeStates); } else { listClusterNodes(nodeStates); } } else if (cliParser.hasOption(HELP_CMD)) { printUsage(opts); return 0; } else { syserr.println("Invalid Command Usage : "); printUsage(opts); } return 0; }
From source file:org.apache.ignite.startup.GridRandomCommandLineLoader.java
/** * Creates cli options.//from w ww. java2s . c om * * @return Command line options */ private static Options createOptions() { Options options = new Options(); Option help = new Option(OPTION_HELP, "print this message"); Option cfg = new Option(null, OPTION_CFG, true, "path to Spring XML configuration file."); cfg.setValueSeparator('='); cfg.setType(String.class); Option minTtl = new Option(null, OPTION_MIN_TTL, true, "node minimum time to live."); minTtl.setValueSeparator('='); minTtl.setType(Long.class); Option maxTtl = new Option(null, OPTION_MAX_TTL, true, "node maximum time to live."); maxTtl.setValueSeparator('='); maxTtl.setType(Long.class); Option duration = new Option(null, OPTION_DURATION, true, "run timeout."); duration.setValueSeparator('='); duration.setType(Long.class); Option log = new Option(null, OPTION_LOG_CFG, true, "path to log4j configuration file."); log.setValueSeparator('='); log.setType(String.class); options.addOption(help); OptionGroup grp = new OptionGroup(); grp.setRequired(true); grp.addOption(cfg); grp.addOption(minTtl); grp.addOption(maxTtl); grp.addOption(duration); grp.addOption(log); options.addOptionGroup(grp); return options; }
From source file:org.apache.ignite.startup.GridVmNodesStarter.java
/** * Creates cli options./*from ww w . j a v a2 s . c o m*/ * * @return Command line options */ private static Options createOptions() { Options options = new Options(); OptionGroup grp = new OptionGroup(); grp.setRequired(true); Option cfg = new Option(OPTION_CFG, null, true, "path to Spring XML configuration file."); cfg.setArgName("file"); Option n = new Option(null, OPTION_N, true, "nodes count."); n.setValueSeparator('='); n.setType(Integer.class); grp.addOption(cfg); grp.addOption(n); options.addOptionGroup(grp); return options; }
From source file:org.azyva.dragom.cliutil.CliUtil.java
/** * Utility method to add standard Option's. * <p>// ww w . ja v a2 s .c o m * The user properties and tools properties Option's are added depending on * whether user properties and tool properties are supported. * <p> * The following Option's are also added: * <ul> * <li>Workspace * <li>Specifying whether confirmation is required * <li>Specifying whether confirmation is required for a particular context * <li>Help * </ul> * Used by tools when initializing Options. * * @param options Options. */ public static void addStandardOptions(Options options) { Option option; Util.applyDragomSystemProperties(); if (Util.isNotNullAndTrue(System.getProperty(CliUtil.SYS_PROPERTY_IND_USER_PROPERTIES))) { option = new Option(null, null); option.setLongOpt(CliUtil.getUserPropertiesFileCommandLineOption()); option.setArgs(1); options.addOption(option); } if (Util.isNotNullAndTrue(System.getProperty(CliUtil.SYS_PROPERTY_IND_SINGLE_TOOL_PROPERTIES))) { option = new Option("D", null); option.setValueSeparator('='); option.setArgs(2); options.addOption(option); } if (Util.isNotNullAndTrue(System.getProperty(CliUtil.SYS_PROPERTY_IND_TOOL_PROPERTIES))) { option = new Option(null, null); option.setLongOpt(CliUtil.getToolPropertiesFileCommandLineOption()); option.setArgs(1); options.addOption(option); } option = new Option(null, null); option.setLongOpt(CliUtil.getWorkspacePathCommandLineOption()); option.setArgs(1); options.addOption(option); option = new Option(null, null); option.setLongOpt(CliUtil.getNoConfirmCommandLineOption()); options.addOption(option); option = new Option(null, null); option.setLongOpt(CliUtil.getNoConfirmContextCommandLineOption()); option.setArgs(1); options.addOption(option); option = new Option(null, null); option.setLongOpt(CliUtil.getHelpCommandLineOption()); options.addOption(option); }
From source file:org.bonitasoft.platform.setup.PlatformSetupApplication.java
private Options createOptions() { Options options = new Options(); Option systemPropertyOption = new Option("D", "specify system property to override configuration from database.properties"); systemPropertyOption.setArgName("property=value"); systemPropertyOption.setValueSeparator('='); systemPropertyOption.setArgs(2);/* w w w. j av a 2 s .c o m*/ options.addOption(systemPropertyOption); options.addOption("f", "force", false, "Force push even if critical folders will be deleted"); return options; }
From source file:org.codeseed.common.config.ext.CommandLineOptionsBuilder.java
/** * Potentially adds an option to the supplied collection. Used by the * {@link #build()} method to populate an options collection. * * @param options//from w ww . ja v a2s .co m * the current collection of options * @param groups * mappings of argument group identifiers to groups * @param method * the configuration method to look up */ protected void addOption(Options options, Map<String, OptionGroup> groups, Method method) { final CommandLine commandLine = method.getAnnotation(CommandLine.class); // Iterate over the triggers; take the first values String opt = null; String longOpt = null; for (String trigger : commandLine.value()) { if (!options.hasOption(trigger)) { if (opt == null && trigger.length() == 1) { opt = trigger; } else if (longOpt == null) { longOpt = trigger; } } } // Either we can use the method name or there is no option being added if (opt == null && longOpt == null) { String methodOpt = LOWER_CAMEL.to(LOWER_HYPHEN, method.getName()); if (!options.hasOption(methodOpt)) { longOpt = methodOpt; } else { // TODO Warn? return; } } // Create a new option Option option = new Option(opt, null); option.setLongOpt(longOpt); // Set the number of arguments based on the return type final Class<?> returnType = Primitives.wrap(method.getReturnType()); if (returnType.equals(Boolean.class)) { option.setArgs(0); } else if (Iterable.class.isAssignableFrom(returnType)) { option.setArgs(commandLine.maximum()); } else if (Map.class.isAssignableFrom(returnType)) { option.setArgs(2); option.setValueSeparator('='); } else { option.setArgs(1); } // Add some descriptive text if (bundle != null) { try { // TODO Does this make sense? String key = option.hasLongOpt() ? option.getLongOpt() : method.getName(); option.setDescription(bundle.getString(key + ".description")); } catch (MissingResourceException e) { option.setDescription(null); } } // Set argument names if (bundle != null && option.getArgs() > 0) { try { option.setArgName(bundle.getString(method.getName() + ".argName")); } catch (MissingResourceException e) { option.setArgName(null); } } // Add to either the collection or to the option groups String groupKey = commandLine.groupId(); if (groupKey.isEmpty()) { options.addOption(option); } else { OptionGroup group = groups.get(groupKey); if (group == null) { group = new OptionGroup(); groups.put(groupKey, group); } group.addOption(option); } }
From source file:org.dacapo.harness.CommandLineArgs.java
private static Option makeOption(String shortName, String longName, String description, String argName) { assert longName != null; Option option = new Option(shortName, longName, argName != null, description); if (argName != null) { option.setValueSeparator('='); option.setArgName(argName);//from w ww.jav a 2 s.c o m } return option; }