List of usage examples for org.apache.commons.cli Options hasOption
public boolean hasOption(String opt)
From source file:org.apache.flink.table.client.config.entries.DeploymentEntry.java
/** * Parses the given command line options from the deployment properties. Ignores properties * that are not defined by options.//w w w.j av a 2s. c o m */ public CommandLine getCommandLine(Options commandLineOptions) throws Exception { final List<String> args = new ArrayList<>(); properties.asMap().forEach((k, v) -> { // only add supported options if (commandLineOptions.hasOption(k)) { final Option o = commandLineOptions.getOption(k); final String argument = "--" + o.getLongOpt(); // options without args if (!o.hasArg()) { final Boolean flag = Boolean.parseBoolean(v); // add key only if (flag) { args.add(argument); } } // add key and value else if (!o.hasArgs()) { args.add(argument); args.add(v); } // options with multiple args are not supported yet else { throw new IllegalArgumentException("Option '" + o + "' is not supported yet."); } } }); return CliFrontendParser.parse(commandLineOptions, args.toArray(new String[args.size()]), true); }
From source file:org.apache.openmeetings.cli.OmHelpFormatter.java
private LinkedHashMap<String, List<OmOption>> getOptions(Options opts, int leftPad) { final String longOptSeparator = " "; final String lpad = createPadding(leftPad); final String lpadParam = createPadding(leftPad + 2); List<OmOption> reqOptions = getReqOptions(opts); LinkedHashMap<String, List<OmOption>> map = new LinkedHashMap<String, List<OmOption>>(reqOptions.size()); map.put(GENERAL_OPTION_GROUP, new ArrayList<OmOption>()); for (OmOption o : reqOptions) { map.put(o.getOpt(), new ArrayList<OmOption>()); }//from w w w .j a va 2 s . co m for (Option _o : opts.getOptions()) { OmOption o = (OmOption) _o; //TODO need better check (required option should go first and should not be duplicated boolean skipOption = map.containsKey(o.getOpt()); boolean mainOption = skipOption || o.getGroup() == null; // first create list containing only <lpad>-a,--aaa where // -a is opt and --aaa is long opt; in parallel look for // the longest opt string this list will be then used to // sort options ascending StringBuilder optBuf = new StringBuilder(); if (o.getOpt() == null) { optBuf.append(mainOption ? lpad : lpadParam).append(" ").append(getLongOptPrefix()) .append(o.getLongOpt()); } else { optBuf.append(mainOption ? lpad : lpadParam).append(getOptPrefix()).append(o.getOpt()); if (o.hasLongOpt()) { optBuf.append(',').append(getLongOptPrefix()).append(o.getLongOpt()); } } if (o.hasArg()) { String argName = o.getArgName(); if (argName != null && argName.length() == 0) { // if the option has a blank argname optBuf.append(' '); } else { optBuf.append(o.hasLongOpt() ? longOptSeparator : " "); optBuf.append("<").append(argName != null ? o.getArgName() : getArgName()).append(">"); } } o.setHelpPrefix(optBuf); maxPrefixLength = Math.max(optBuf.length(), maxPrefixLength); if (skipOption) { //TODO need better check (required option should go first and should not be duplicated continue; } String grp = o.getGroup(); grp = grp == null ? GENERAL_OPTION_GROUP : grp; String[] grps = grp.split(","); for (String g : grps) { map.get(g).add(o); } } for (Map.Entry<String, List<OmOption>> me : map.entrySet()) { final String key = me.getKey(); List<OmOption> options = me.getValue(); Collections.sort(options, new Comparator<OmOption>() { @Override public int compare(OmOption o1, OmOption o2) { boolean o1opt = !o1.isOptional(key); boolean o2opt = !o2.isOptional(key); return (o1opt && o2opt || !o1opt && !o2opt) ? (o1.getOpt() == null ? 1 : -1) : (o1opt ? -1 : 1); } }); if (opts.hasOption(key)) { options.add(0, (OmOption) opts.getOption(key)); } } return map; }
From source file:org.apache.stratos.cli.commands.AddApplicationSignupCommand.java
@Override public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts) throws CommandException { if (log.isDebugEnabled()) { log.debug("Executing command: ", getName()); }/* w w w . j ava2s . c o m*/ if ((args == null) || (args.length <= 0)) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } String applicationId = args[0]; try { CommandLineParser parser = new GnuParser(); CommandLine commandLine = parser.parse(options, args); //merge newly discovered options with previously discovered ones. Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions()); if (opts.hasOption(CliConstants.RESOURCE_PATH)) { String resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue(); if (resourcePath == null) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } String resourceFileContent = CliUtils.readResource(resourcePath); RestCommandLineService.getInstance().addApplicationSignup(resourceFileContent, applicationId); return CliConstants.COMMAND_SUCCESSFULL; } else { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } } catch (ParseException e) { log.error("Error parsing arguments", e); System.out.println(e.getMessage()); return CliConstants.COMMAND_FAILED; } catch (IOException e) { System.out.println("Invalid resource path"); return CliConstants.COMMAND_FAILED; } catch (Exception e) { String message = "Unknown error occurred: " + e.getMessage(); System.out.println(message); log.error(message, e); return CliConstants.COMMAND_FAILED; } }
From source file:org.apache.stratos.cli.commands.AddAutoscalingPolicyCommand.java
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts) throws CommandException { if (log.isDebugEnabled()) { log.debug("Executing {} command...", getName()); }/*from w ww.j a v a2 s. c om*/ if (args != null && args.length > 0) { String resourcePath = null; String resourceFileContent = null; final CommandLineParser parser = new GnuParser(); CommandLine commandLine; try { commandLine = parser.parse(options, args); //merge newly discovered options with previously discovered ones. Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions()); if (log.isDebugEnabled()) { log.debug("Autoscaling policy deployment"); } if (opts.hasOption(CliConstants.RESOURCE_PATH)) { if (log.isTraceEnabled()) { log.trace("Resource path option is passed"); } resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue(); resourceFileContent = CliUtils.readResource(resourcePath); } if (resourcePath == null) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } RestCommandLineService.getInstance().addAutoscalingPolicy(resourceFileContent); return CliConstants.COMMAND_SUCCESSFULL; } catch (ParseException e) { log.error("Error parsing arguments", e); System.out.println(e.getMessage()); return CliConstants.COMMAND_FAILED; } catch (IOException e) { System.out.println("Invalid resource path"); return CliConstants.COMMAND_FAILED; } } else { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } }
From source file:org.apache.stratos.cli.commands.AddCartridgeCommand.java
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts) throws CommandException { if (log.isDebugEnabled()) { log.debug("Executing {} command...", getName()); }// w w w. j av a 2s.co m if (args != null && args.length > 0) { String resourcePath = null; String resourceFileContent = null; final CommandLineParser parser = new GnuParser(); CommandLine commandLine; try { commandLine = parser.parse(options, args); //merge newly discovered options with previously discovered ones. Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions()); if (log.isDebugEnabled()) { log.debug("Cartridge deployment"); } if (opts.hasOption(CliConstants.RESOURCE_PATH)) { if (log.isTraceEnabled()) { log.trace("Resource path option is passed"); } resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue(); resourceFileContent = CliUtils.readResource(resourcePath); } if (resourcePath == null) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } RestCommandLineService.getInstance().addCartridge(resourceFileContent); return CliConstants.COMMAND_SUCCESSFULL; } catch (ParseException e) { log.error("Error parsing arguments", e); System.out.println(e.getMessage()); return CliConstants.COMMAND_FAILED; } catch (IOException e) { System.out.println("Invalid resource path"); return CliConstants.COMMAND_FAILED; } } else { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } }
From source file:org.apache.stratos.cli.commands.AddCartridgeGroupCommand.java
@Override public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts) throws CommandException { if (log.isDebugEnabled()) { log.debug("Executing command: ", getName()); }//from w ww. ja va2 s .c o m if ((args == null) || (args.length <= 0)) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } try { CommandLineParser parser = new GnuParser(); CommandLine commandLine = parser.parse(options, args); //merge newly discovered options with previously discovered ones. Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions()); if (opts.hasOption(CliConstants.RESOURCE_PATH)) { String resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue(); if (resourcePath == null) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } String resourceFileContent = CliUtils.readResource(resourcePath); RestCommandLineService.getInstance().addCartridgeGroup(resourceFileContent); return CliConstants.COMMAND_SUCCESSFULL; } else { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } } catch (ParseException e) { log.error("Error parsing arguments", e); System.out.println(e.getMessage()); return CliConstants.COMMAND_FAILED; } catch (IOException e) { System.out.println("Invalid resource path"); return CliConstants.COMMAND_FAILED; } catch (Exception e) { String message = "Unknown error occurred: " + e.getMessage(); System.out.println(message); log.error(message, e); return CliConstants.COMMAND_FAILED; } }
From source file:org.apache.stratos.cli.commands.AddDeploymentPolicyCommand.java
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts) throws CommandException { if (log.isDebugEnabled()) { log.debug("Executing {} command...", getName()); }/*from w w w . j a va 2s . c o m*/ if (args != null && args.length > 0) { String resourcePath = null; String resourceFileContent = null; final CommandLineParser parser = new GnuParser(); CommandLine commandLine; try { commandLine = parser.parse(options, args); //merge newly discovered options with previously discovered ones. Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions()); if (log.isDebugEnabled()) { log.debug("Deployment policy deployment"); } if (opts.hasOption(CliConstants.RESOURCE_PATH)) { if (log.isTraceEnabled()) { log.trace("Resource path option is passed"); } resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue(); resourceFileContent = CliUtils.readResource(resourcePath); } if (resourcePath == null) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } RestCommandLineService.getInstance().addDeploymentPolicy(resourceFileContent); return CliConstants.COMMAND_SUCCESSFULL; } catch (ParseException e) { log.error("Error parsing arguments", e); System.out.println(e.getMessage()); return CliConstants.COMMAND_FAILED; } catch (IOException e) { System.out.println("Invalid resource path"); return CliConstants.COMMAND_FAILED; } } else { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } }
From source file:org.apache.stratos.cli.commands.AddDomainMappingsCommand.java
@Override public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts) throws CommandException { if (log.isDebugEnabled()) { log.debug("Executing command: ", getName()); }/* w w w.j a va 2s. co m*/ if ((args == null) || (args.length <= 0)) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } try { CommandLineParser parser = new GnuParser(); CommandLine commandLine = parser.parse(options, args); //merge newly discovered options with previously discovered ones. Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions()); if (opts.hasOption(CliConstants.RESOURCE_PATH)) { String resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue(); if (resourcePath == null) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } String applicationId = args[0]; if (applicationId != null) { String resourceFileContent = CliUtils.readResource(resourcePath); RestCommandLineService.getInstance().addDomainMappings(applicationId, resourceFileContent); return CliConstants.COMMAND_SUCCESSFULL; } else { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } } else { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } } catch (ParseException e) { log.error("Error parsing arguments", e); System.out.println(e.getMessage()); return CliConstants.COMMAND_FAILED; } catch (IOException e) { System.out.println("Invalid resource path"); return CliConstants.COMMAND_FAILED; } catch (Exception e) { String message = "Unknown error occurred: " + e.getMessage(); System.out.println(message); log.error(message, e); return CliConstants.COMMAND_FAILED; } }
From source file:org.apache.stratos.cli.commands.AddKubernetesClusterCommand.java
@Override public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts) throws CommandException { if (log.isDebugEnabled()) { log.debug("Executing command: ", getName()); }//from ww w . jav a 2 s . c om if ((args == null) || (args.length <= 0)) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } try { CommandLineParser parser = new GnuParser(); CommandLine commandLine = parser.parse(options, args); //merge newly discovered options with previously discovered ones. Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions()); if (opts.hasOption(CliConstants.RESOURCE_PATH)) { String resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue(); if (resourcePath == null) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } String resourceFileContent = CliUtils.readResource(resourcePath); RestCommandLineService.getInstance().addKubernetesCluster(resourceFileContent); return CliConstants.COMMAND_SUCCESSFULL; } else { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } } catch (ParseException e) { log.error("Error parsing arguments", e); System.out.println(e.getMessage()); return CliConstants.COMMAND_FAILED; } catch (IOException e) { System.out.println("Invalid resource path"); return CliConstants.COMMAND_FAILED; } catch (Exception e) { String message = "Unknown error occurred: " + e.getMessage(); System.out.println(message); log.error(message, e); return CliConstants.COMMAND_FAILED; } }
From source file:org.apache.stratos.cli.commands.AddKubernetesHostCommand.java
@Override public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts) throws CommandException { if (log.isDebugEnabled()) { log.debug("Executing command: ", getName()); }// w w w . j a va2 s.c om if ((args == null) || (args.length <= 0)) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } try { CommandLineParser parser = new GnuParser(); CommandLine commandLine = parser.parse(options, args); //merge newly discovered options with previously discovered ones. Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions()); if ((opts.hasOption(CliConstants.RESOURCE_PATH)) && (opts.hasOption(CliConstants.CLUSTER_ID_OPTION))) { // get cluster id arg value String clusterId = opts.getOption(CliConstants.CLUSTER_ID_OPTION).getValue(); if (clusterId == null) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } // get resource path arg value String resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue(); if (resourcePath == null) { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } String resourceFileContent = CliUtils.readResource(resourcePath); RestCommandLineService.getInstance().addKubernetesHost(resourceFileContent, clusterId); return CliConstants.COMMAND_SUCCESSFULL; } else { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; } } catch (ParseException e) { log.error("Error parsing arguments", e); System.out.println(e.getMessage()); return CliConstants.COMMAND_FAILED; } catch (IOException e) { System.out.println("Invalid resource path"); return CliConstants.COMMAND_FAILED; } catch (Exception e) { String message = "Unknown error occurred: " + e.getMessage(); System.out.println(message); log.error(message, e); return CliConstants.COMMAND_FAILED; } }