List of usage examples for org.apache.commons.cli OptionBuilder hasArgs
public static OptionBuilder hasArgs(int num)
num
argument values. From source file:org.dcm4che3.tool.stowrs.StowRS.java
@SuppressWarnings("static-access") private static CommandLine parseComandLine(String[] args) throws ParseException { opts = new Options(); opts.addOption(OptionBuilder.hasArgs(2).withArgName("[seq/]attr=value").withValueSeparator() .withDescription(rb.getString("metadata")).create("m")); opts.addOption("u", "url", true, rb.getString("url")); opts.addOption("t", "metadata-type", true, rb.getString("metadata-type")); opts.addOption("ts", "transfer-syntax", true, rb.getString("transfer-syntax")); CLIUtils.addCommonOptions(opts);/*from w w w .j av a 2 s. co m*/ return CLIUtils.parseComandLine(args, opts, rb, StowRS.class); }
From source file:org.geoint.geoserver.configuration.manager.GeoserverConfigCli.java
public static Options getOptions() { Option debug_option = OptionBuilder.withDescription("sets logger to all").create("debug"); Option userName_option = OptionBuilder.withArgName("username").hasArg().isRequired(true) .withDescription("(required) domain username").create(OPTION_U); Option password_option = OptionBuilder.withArgName("password").isRequired(true).hasArg() .withDescription("(required) domain Password").create(OPTION_P); Option sourceServer_option = OptionBuilder.withArgName("sourceServer").hasArg() .withDescription("The source server URL").create(OPTION_S); Option zipLocation_option = OptionBuilder.withArgName("ziplocation").hasArg().isRequired(true) .withDescription(/*from ww w.j a v a 2 s . com*/ "(required) the path to the ZIP file. This can be where you want it, or the source for distribution") .create(OPTION_Z); Option collect_option = OptionBuilder.withArgName(OPTION_COLLECT) .withDescription("Collect configuration from server").create(OPTION_COLLECT); Option distribute_option = OptionBuilder.withArgName(OPTION_DISTRIBUTE) .withDescription("distribute configuration to other geoserver instances").create(OPTION_DISTRIBUTE); Option collectanddistribute_option = OptionBuilder.withArgName("collectanddistribute_option") .withDescription("collect and distribute configuration to other geoserver instances") .create(OPTION_COLLECTANDDISTRIBUTE); Option destinationServers_option = OptionBuilder.hasArgs(12) .withDescription("a list of destination server URLs comma separated").withValueSeparator(',') .create(OPTION_D); Option help_option = OptionBuilder.withDescription("prints help").create(OPTION_HELP); Options options = new Options(); options.addOption(debug_option); options.addOption(userName_option); options.addOption(password_option); options.addOption(sourceServer_option); options.addOption(zipLocation_option); options.addOption(collect_option); options.addOption(distribute_option); options.addOption(collectanddistribute_option); options.addOption(destinationServers_option); options.addOption(help_option); return options; }
From source file:org.lib4j.cli.Options.java
public static Options parse(final Cli binding, final Class<?> mainClass, final String[] args) { final Set<String> requiredNames = new HashSet<>(); final Map<String, String> nameToAltName = new HashMap<>(); final org.apache.commons.cli.Options apacheOptions = new org.apache.commons.cli.Options(); apacheOptions.addOption(null, "help", false, "Print help and usage."); short argumentsMinOccurs = 0; short argumentsMaxOccurs = 0; final Cli.Arguments cliArguments; if (binding != null) { cliArguments = binding.getArguments(); if (cliArguments != null) { argumentsMinOccurs = cliArguments.getMinOccurs(); argumentsMaxOccurs = "unbounded".equals(cliArguments.getMaxOccurs()) ? Short.MAX_VALUE : Short.parseShort(cliArguments.getMaxOccurs()); if (argumentsMaxOccurs < argumentsMinOccurs) { logger.error("minOccurs > maxOccurs on <arguments> element"); System.exit(1);/* w ww . jav a 2s . c o m*/ } } if (binding.getOption() != null) { for (final Cli.Option option : binding.getOption()) { final Cli.Option.Name optionName = option.getName(); final String longName = optionName.getLong() == null ? null : optionName.getLong(); final String shortName = optionName.getShort() == null ? null : optionName.getShort(); final String name = longName != null ? longName : shortName; if (longName == null && shortName == null) { logger.error("both [long] and [short] option names are null in cli spec"); System.exit(1); } nameToAltName.put(name, shortName != null ? shortName : longName); OptionBuilder.withLongOpt(name == longName ? longName : null); // Record which options are required if (option.getArgument() != null) { final Cli.Option.Argument argument = option.getArgument(); final boolean isRequired = Use.REQUIRED == argument.getUse(); if (isRequired) { OptionBuilder.isRequired(); requiredNames.add(longName); } final int maxOccurs = argument.getMaxOccurs() == null ? 1 : "unbounded".equals(argument.getMaxOccurs()) ? Integer.MAX_VALUE : Integer.parseInt(argument.getMaxOccurs()); if (maxOccurs == 1) { if (isRequired) OptionBuilder.hasArgs(1); else OptionBuilder.hasOptionalArgs(1); } else if (maxOccurs == Integer.MAX_VALUE) { if (isRequired) OptionBuilder.hasArgs(); else OptionBuilder.hasOptionalArgs(); } else { if (isRequired) OptionBuilder.hasArgs(maxOccurs); else OptionBuilder.hasOptionalArgs(maxOccurs); } final char valueSeparator = argument.getValueSeparator() != null ? argument.getValueSeparator().charAt(0) : ' '; OptionBuilder .withArgName(formatArgumentName(argument.getLabel(), maxOccurs, valueSeparator)); OptionBuilder.withValueSeparator(valueSeparator); if (option.getDescription() == null) { logger.error("missing <description> for " + name + " option"); System.exit(1); } final StringBuilder description = new StringBuilder(option.getDescription()); if (option.getArgument().getDefault() != null) description.append("\nDefault: ").append(option.getArgument().getDefault()); OptionBuilder.withDescription(description.toString()); } apacheOptions.addOption(OptionBuilder.create(shortName)); } } } else { cliArguments = null; } final Map<String, Option> optionsMap = new HashMap<>(); final Set<String> specifiedLongNames; CommandLine commandLine = null; if (args != null && args.length != 0) { specifiedLongNames = new HashSet<>(); final CommandLineParser parser = new PosixParser(); do { try { commandLine = parser.parse(apacheOptions, args); } catch (final UnrecognizedOptionException e) { if (e.getMessage().startsWith("Unrecognized option: ")) { final String unrecognizedOption = e.getMessage().substring(21); logger.error("Unrecognized option: " + unrecognizedOption); for (int i = 0; i < args.length; i++) if (args[i].equals(unrecognizedOption)) args[i] = "--help"; } else { throw new IllegalArgumentException(e); } } catch (final org.apache.commons.cli.ParseException e) { Options.trapPrintHelp(apacheOptions, cliArguments, null, System.err); } } while (commandLine == null); } else { specifiedLongNames = null; } final Collection<String> arguments = commandLine != null ? commandLine.getArgList() : null; if (arguments != null && arguments.size() > 0) { if (argumentsMaxOccurs < arguments.size() || arguments.size() < argumentsMinOccurs) { Options.trapPrintHelp(apacheOptions, cliArguments, null, System.err); } } else if (argumentsMinOccurs > 0) { Options.trapPrintHelp(apacheOptions, cliArguments, null, System.err); } if (commandLine != null) { for (final org.apache.commons.cli.Option option : commandLine.getOptions()) { specifiedLongNames.add(option.getLongOpt()); if ("help".equals(option.getLongOpt())) Options.trapPrintHelp(apacheOptions, cliArguments, null, System.out); final String optionName = option.getLongOpt() != null ? option.getLongOpt() : option.getOpt(); optionsMap.put(optionName, option.getValue() != null ? new Option(optionName, option.getValueSeparator(), option.getValues()) : new Option(optionName, option.getValueSeparator(), "true")); } } // See if some arguments are missing if (requiredNames.size() != 0) { if (specifiedLongNames != null) requiredNames.removeAll(specifiedLongNames); if (requiredNames.size() != 0) { final StringBuilder builder = new StringBuilder(); for (final String longName : requiredNames) { final String shortName = nameToAltName.get(longName); if (shortName.equals(longName)) builder.append("\nMissing argument: -").append(shortName); else builder.append("\nMissing argument: -").append(shortName).append(",--").append(longName); } Options.trapPrintHelp(apacheOptions, cliArguments, builder.substring(1), System.out); } } // Include default values for options that are not specified if (binding.getOption() != null) { for (final Cli.Option option : binding.getOption()) { if (option.getArgument() != null && option.getArgument().getDefault() != null) { final String optionName = option.getName().getLong() != null ? option.getName().getLong() : option.getName().getShort(); if (!optionsMap.containsKey(optionName)) { final String valueSeparator = option.getArgument().getValueSeparator(); final String defaultValue = option.getArgument().getDefault(); optionsMap.put(optionName, valueSeparator != null ? new Option(optionName, valueSeparator.charAt(0), defaultValue) : new Option(optionName, defaultValue)); } } } } // Check pattern for specified and default options if (binding.getOption() != null) { final StringBuilder builder = new StringBuilder(); for (final Cli.Option option : binding.getOption()) { if (option.getArgument() != null && option.getArgument().getPattern() != null) { final String optionName = option.getName().getLong() != null ? option.getName().getLong() : option.getName().getShort(); final Option opt = optionsMap.get(optionName); if (opt != null) { for (final String value : opt.getValues()) { if (!value.matches(option.getArgument().getPattern())) { if (option.getName().getLong() == null || option.getName().getShort() == null) builder.append("\nIncorrect argument form: -").append(optionName); else builder.append("\nIncorrect argument form: -") .append(option.getName().getShort()).append(",--") .append(option.getName().getLong()); builder.append(' ').append(value).append("\n Required: ") .append(option.getArgument().getPattern()); } } } } } if (builder.length() > 0) Options.trapPrintHelp(apacheOptions, cliArguments, builder.substring(1), System.out); } return new Options(mainClass, args, optionsMap.values(), arguments == null || arguments.size() == 0 ? null : arguments.toArray(new String[arguments.size()])); }
From source file:org.libx4j.cli.Options.java
public static Options parse(final cli_cli binding, final Class<?> mainClass, final String[] args) throws OptionsException { final Set<String> requiredNames = new HashSet<String>(); final Map<String, String> nameToAltName = new HashMap<String, String>(); final org.apache.commons.cli.Options apacheOptions = new org.apache.commons.cli.Options(); apacheOptions.addOption(null, "help", false, "Print help and usage."); int argumentsMinOccurs = 0; int argumentsMaxOccurs = 0; final cli_cli._arguments cliArguments; if (binding != null) { cliArguments = binding._arguments(0); if (!cliArguments.isNull()) { argumentsMinOccurs = cliArguments._minOccurs$().text(); argumentsMaxOccurs = "unbounded".equals(cliArguments._maxOccurs$().text()) ? Integer.MAX_VALUE : Integer.parseInt(cliArguments._maxOccurs$().text()); if (argumentsMaxOccurs < argumentsMinOccurs) { logger.error("minOccurs > maxOccurs on <arguments> element"); System.exit(1);/* ww w . j a va 2 s . co m*/ } } if (binding._option() != null) { for (final cli_cli._option option : binding._option()) { final cli_cli._option._name optionName = option._name(0); final String longName = optionName._long$().isNull() ? null : optionName._long$().text(); final String shortName = optionName._short$().isNull() ? null : optionName._short$().text(); final String name = longName != null ? longName : shortName; if (longName == null && shortName == null) { logger.error("both [long] and [short] option names are null in cli spec"); System.exit(1); } nameToAltName.put(name, shortName != null ? shortName : longName); OptionBuilder.withLongOpt(name == longName ? longName : null); // Record which options are required if (option._argument() != null && option._argument().size() != 0) { final cli_cli._option._argument argument = option._argument(0); final boolean isRequired = $cli_use.required.text().equals(argument._use$().text()); if (isRequired) { OptionBuilder.isRequired(); requiredNames.add(longName); } final int maxOccurs = argument._maxOccurs$().isNull() ? 1 : "unbounded".equals(argument._maxOccurs$().text()) ? Integer.MAX_VALUE : Integer.parseInt(argument._maxOccurs$().text()); if (maxOccurs == 1) { if (isRequired) OptionBuilder.hasArgs(1); else OptionBuilder.hasOptionalArgs(1); } else if (maxOccurs == Integer.MAX_VALUE) { if (isRequired) OptionBuilder.hasArgs(); else OptionBuilder.hasOptionalArgs(); } else { if (isRequired) OptionBuilder.hasArgs(maxOccurs); else OptionBuilder.hasOptionalArgs(maxOccurs); } final char valueSeparator = argument._valueSeparator$().text() != null ? argument._valueSeparator$().text().charAt(0) : ' '; OptionBuilder.withArgName( formatArgumentName(argument._label$().text(), maxOccurs, valueSeparator)); OptionBuilder.withValueSeparator(valueSeparator); if (option._description(0).isNull()) { logger.error("missing <description> for " + name + " option"); System.exit(1); } final StringBuilder description = new StringBuilder(option._description(0).text()); if (!option._argument(0)._default$().isNull()) description.append("\nDefault: ").append(option._argument(0)._default$().text()); OptionBuilder.withDescription(description.toString()); } apacheOptions.addOption(OptionBuilder.create(shortName)); } } } else { cliArguments = null; } final Map<String, Option> optionsMap = new HashMap<String, Option>(); final Set<String> specifiedLongNames; CommandLine commandLine = null; if (args != null && args.length != 0) { specifiedLongNames = new HashSet<String>(); final CommandLineParser parser = new PosixParser(); do { try { commandLine = parser.parse(apacheOptions, args); } catch (final UnrecognizedOptionException e) { if (e.getMessage().startsWith("Unrecognized option: ")) { final String unrecognizedOption = e.getMessage().substring(21); logger.error("Unrecognized option: " + unrecognizedOption); for (int i = 0; i < args.length; i++) if (args[i].equals(unrecognizedOption)) args[i] = "--help"; } else { throw new OptionsException(e); } } catch (final org.apache.commons.cli.ParseException e) { Options.trapPrintHelp(apacheOptions, cliArguments, null, System.err); } } while (commandLine == null); } else { specifiedLongNames = null; } final Collection<String> arguments = commandLine != null ? commandLine.getArgList() : null; if (arguments != null && arguments.size() > 0) { if (argumentsMaxOccurs < arguments.size() || arguments.size() < argumentsMinOccurs) { Options.trapPrintHelp(apacheOptions, cliArguments, null, System.err); } } else if (argumentsMinOccurs > 0) { Options.trapPrintHelp(apacheOptions, cliArguments, null, System.err); } if (commandLine != null) { for (final org.apache.commons.cli.Option option : commandLine.getOptions()) { specifiedLongNames.add(option.getLongOpt()); if ("help".equals(option.getLongOpt())) Options.trapPrintHelp(apacheOptions, cliArguments, null, System.out); final String optionName = option.getLongOpt() != null ? option.getLongOpt() : option.getOpt(); optionsMap.put(optionName, option.getValue() != null ? new Option(optionName, option.getValueSeparator(), option.getValues()) : new Option(optionName, option.getValueSeparator(), "true")); } } // See if some arguments are missing if (requiredNames.size() != 0) { if (specifiedLongNames != null) requiredNames.removeAll(specifiedLongNames); if (requiredNames.size() != 0) { final StringBuilder builder = new StringBuilder(); for (final String longName : requiredNames) { final String shortName = nameToAltName.get(longName); if (shortName.equals(longName)) builder.append("\nMissing argument: -").append(shortName); else builder.append("\nMissing argument: -").append(shortName).append(",--").append(longName); } Options.trapPrintHelp(apacheOptions, cliArguments, builder.substring(1), System.out); } } // Include default values for options that are not specified if (binding._option() != null) { for (final cli_cli._option option : binding._option()) { if (!option._argument(0)._default$().isNull()) { final String optionName = !option._name(0)._long$().isNull() ? option._name(0)._long$().text() : option._name(0)._short$().text(); if (!optionsMap.containsKey(optionName)) { final String valueSeparator = option._argument(0)._valueSeparator$().text(); final String defaultValue = option._argument(0)._default$().text(); optionsMap.put(optionName, valueSeparator != null ? new Option(optionName, valueSeparator.charAt(0), defaultValue) : new Option(optionName, defaultValue)); } } } } // Check pattern for specified and default options if (binding._option() != null) { final StringBuilder builder = new StringBuilder(); for (final cli_cli._option option : binding._option()) { if (!option._argument(0)._pattern$().isNull()) { final String optionName = !option._name(0)._long$().isNull() ? option._name(0)._long$().text() : option._name(0)._short$().text(); final Option opt = optionsMap.get(optionName); if (opt != null) { for (final String value : opt.getValues()) { if (!value.matches(option._argument(0)._pattern$().text())) { if (option._name(0)._long$().isNull() || option._name(0)._short$().isNull()) builder.append("\nIncorrect argument form: -").append(optionName); else builder.append("\nIncorrect argument form: -") .append(option._name(0)._short$().text()).append(",--") .append(option._name(0)._long$().text()); builder.append(" ").append(value).append("\n Required: ") .append(option._argument(0)._pattern$().text()); } } } } } if (builder.length() > 0) Options.trapPrintHelp(apacheOptions, cliArguments, builder.substring(1), System.out); } return new Options(mainClass, args, optionsMap.values(), arguments == null || arguments.size() == 0 ? null : arguments.toArray(new String[arguments.size()])); }
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); OptionBuilder.isRequired();//from ww w. j ava2s . com 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.neovera.jdiablo.internal.OptionAnnotatedProperty.java
@SuppressWarnings("static-access") public org.apache.commons.cli.Option getCliOption() { if (_cliOption != null) { return _cliOption; } else {/*from ww w . java 2s . c o m*/ Option option = getOption(); OptionBuilder builder = OptionBuilder.withDescription(option.description()); if (StringUtils.isNotBlank(option.argName())) { builder = builder.withArgName(option.argName()); } if (option.args() != 0) { builder = builder.hasArgs(option.args()); } if (option.hasArgs()) { builder = builder.hasArgs(); } if (StringUtils.isNotBlank(option.longOption())) { builder = builder.withLongOpt(option.longOption()); } if (option.optionalArgs() != 0) { builder = builder.hasOptionalArgs(option.optionalArgs()); } if (option.required() && !getOptionProperty().isOptionNotRequiredOverride()) { builder = builder.isRequired(); } if (option.valueSeparator() != ' ') { builder = builder.withValueSeparator(option.valueSeparator()); } setCliOption(builder.create(option.shortOption())); return getCliOption(); } }
From source file:org.nuunframework.cli.NuunCliPlugin.java
private Option createOptionFromField(Field field) { Option option = null;/* w w w. j a va 2 s. c o m*/ // 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.psystems.dicomweb.Dcm2DcmCopy.java
private static CommandLine parse(String[] args) { Options opts = new Options(); opts.addOption(null, "no-fmi", false, "Encode result without File Meta Information. At default, " + " File Meta Information is included."); opts.addOption("e", "explicit", false, "Encode result with Explicit VR Little Endian Transfer Syntax. " + "At default, Implicit VR Little Endian is used."); opts.addOption("b", "big-endian", false, "Encode result with Explicit VR Big Endian Transfer Syntax. " + "At default, Implicit VR Little Endian is used."); opts.addOption("z", "deflated", false, "Encode result with Deflated Explicit VR Little Endian Syntax. " + "At default, Implicit VR Little Endian is used."); OptionBuilder.withArgName("[seq/]attr=value"); OptionBuilder.hasArgs(2); OptionBuilder.withValueSeparator('='); OptionBuilder.withDescription(//from w w w .j a v a 2 s .com "specify value to set in the output stream. Currently only works when transcoding images."); opts.addOption(OptionBuilder.create("s")); opts.addOption("t", "syntax", true, "Encode result with the specified transfer syntax - recodes" + " the image typically."); OptionBuilder.withArgName("KB"); OptionBuilder.hasArg(); OptionBuilder.withDescription("transcoder buffer size in KB, 1KB by default"); OptionBuilder.withLongOpt("buffer"); opts.addOption(OptionBuilder.create(null)); opts.addOption("h", "help", false, "print this message"); opts.addOption("V", "version", false, "print the version information and exit"); CommandLine cl = null; try { cl = new PosixParser().parse(opts, args); } catch (ParseException e) { exit("dcm2dcm: " + e.getMessage()); throw new RuntimeException("unreachable"); } if (cl.hasOption('V')) { Package p = Dcm2DcmCopy.class.getPackage(); System.out.println("dcm2dcm v" + p.getImplementationVersion()); System.exit(0); } if (cl.hasOption('h') || cl.getArgList().size() < 2) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(USAGE, DESCRIPTION, opts, EXAMPLE); System.exit(0); } return cl; }
From source file:org.psystems.dicomweb.Dcm2DcmOld.java
private static CommandLine parse(String[] args) { Options opts = new Options(); opts.addOption(null, "no-fmi", false, "Encode result without File Meta Information. At default, " + " File Meta Information is included."); opts.addOption("e", "explicit", false, "Encode result with Explicit VR Little Endian Transfer Syntax. " + "At default, Implicit VR Little Endian is used."); opts.addOption("b", "big-endian", false, "Encode result with Explicit VR Big Endian Transfer Syntax. " + "At default, Implicit VR Little Endian is used."); opts.addOption("z", "deflated", false, "Encode result with Deflated Explicit VR Little Endian Syntax. " + "At default, Implicit VR Little Endian is used."); OptionBuilder.withArgName("[seq/]attr=value"); OptionBuilder.hasArgs(2); OptionBuilder.withValueSeparator('='); OptionBuilder.withDescription(//from www.j a v a 2 s.c o m "specify value to set in the output stream. Currently only works when transcoding images."); opts.addOption(OptionBuilder.create("s")); opts.addOption("t", "syntax", true, "Encode result with the specified transfer syntax - recodes" + " the image typically."); OptionBuilder.withArgName("KB"); OptionBuilder.hasArg(); OptionBuilder.withDescription("transcoder buffer size in KB, 1KB by default"); OptionBuilder.withLongOpt("buffer"); opts.addOption(OptionBuilder.create(null)); opts.addOption("h", "help", false, "print this message"); opts.addOption("V", "version", false, "print the version information and exit"); CommandLine cl = null; try { cl = new PosixParser().parse(opts, args); } catch (ParseException e) { exit("dcm2dcm: " + e.getMessage()); throw new RuntimeException("unreachable"); } if (cl.hasOption('V')) { Package p = Dcm2DcmOld.class.getPackage(); System.out.println("dcm2dcm v" + p.getImplementationVersion()); System.exit(0); } if (cl.hasOption('h') || cl.getArgList().size() < 2) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(USAGE, DESCRIPTION, opts, EXAMPLE); System.exit(0); } return cl; }
From source file:updater.builder.SoftwarePatchBuilder.java
public static void main(String[] args) { Options options = new Options(); // utilities/*from w w w .j a va 2 s . c om*/ options.addOption(OptionBuilder.hasArg().withArgName("file") .withDescription("generate SHA-256 checksum of the file").create("sha256")); // cipher key options.addOption(OptionBuilder.hasArgs(2).withArgName("method length").withValueSeparator(' ') .withDescription("AES|RSA for 'method'; generate cipher key with specified key length in bits") .create("genkey")); options.addOption(OptionBuilder.hasArg().withArgName("file") .withDescription("renew the IV in the AES key file").create("renew")); // diff options.addOption(OptionBuilder.hasArgs(2).withArgName("old new").withValueSeparator(' ') .withDescription("generate a binary diff file of 'new' from 'old'").create("diff")); options.addOption(OptionBuilder.hasArgs(2).withArgName("file patch").withValueSeparator(' ') .withDescription("patch the 'file' with the 'patch'").create("diffpatch")); // compression options.addOption(OptionBuilder.hasArg().withArgName("file") .withDescription("compress the 'file' using XZ/LZMA2").create("compress")); options.addOption(OptionBuilder.hasArg().withArgName("file") .withDescription("decompress the 'file' using XZ/LZMA2").create("decompress")); // create & apply patch options.addOption(OptionBuilder.hasArgs(2).withArgName("folder patch").withValueSeparator(' ') .withDescription("apply the patch to the specified folder").create("do")); options.addOption(OptionBuilder.hasArg().withArgName("folder") .withDescription("create a full patch for upgrade from all version (unless specified)") .create("full")); options.addOption(OptionBuilder.hasArgs(2).withArgName("old new").withValueSeparator(' ').withDescription( "create a patch for upgrade from 'old' to 'new'; 'old' and 'new' are the directory of the two versions") .create("patch")); // patch packer, extractor options.addOption(OptionBuilder.hasArgs(2).withArgName("file folder").withValueSeparator(' ') .withDescription("extract the patch 'file' to the folder").create("extract")); options.addOption(OptionBuilder.hasArg().withArgName("folder").withDescription("pack the folder to a patch") .create("pack")); // catalog options.addOption(OptionBuilder.hasArgs(2).withArgName("mode file").withValueSeparator(' ') .withDescription("e|d for 'mode', e for encrypt, d for decrypt; 'file' is the catalog file") .create("catalog")); // script validation options.addOption(OptionBuilder.hasArg().withArgName("file").withDescription("validate a XML script file") .create("validate")); // subsidary options options.addOption(OptionBuilder.hasArg().withArgName("file").withDescription("specify output to which file") .withLongOpt("output").create("o")); options.addOption(OptionBuilder.hasArg().withArgName("file").withDescription("specify the key file to use") .withLongOpt("key").create("k")); options.addOption(OptionBuilder.hasArg().withArgName("version").withDescription("specify the version-from") .withLongOpt("from").create("f")); options.addOption(OptionBuilder.hasArg().withArgName("version") .withDescription("specify the version-from-subsequent").withLongOpt("from-sub").create("fs")); options.addOption(OptionBuilder.hasArg().withArgName("version").withDescription("specify the version-to") .withLongOpt("to").create("t")); options.addOption(new Option("h", "help", false, "print this message")); options.addOption(new Option("v", "version", false, "show the version of this software")); options.addOption( new Option("vb", "verbose", false, "turn on verbose mode, output details when encounter error")); CommandLineParser parser = new GnuParser(); CommandLine line = null; try { line = parser.parse(options, args); if (line.hasOption("sha256")) { sha256(line, options); } else if (line.hasOption("genkey")) { genkey(line, options); } else if (line.hasOption("renew")) { renew(line, options); } else if (line.hasOption("diff")) { diff(line, options); } else if (line.hasOption("diffpatch")) { diffpatch(line, options); } else if (line.hasOption("compress")) { compress(line, options); } else if (line.hasOption("decompress")) { decompress(line, options); } else if (line.hasOption("do")) { doPatch(line, options); } else if (line.hasOption("full")) { full(line, options); } else if (line.hasOption("patch")) { patch(line, options); } else if (line.hasOption("extract")) { extract(line, options); } else if (line.hasOption("pack")) { pack(line, options); } else if (line.hasOption("catalog")) { catalog(line, options); } else if (line.hasOption("validate")) { validate(line, options); } else if (line.hasOption("version")) { version(); } else if (line.hasOption("help")) { showHelp(options); } else { version(); System.out.println(); showHelp(options); } } catch (ParseException ex) { System.out.println(ex.getMessage()); showHelp(options); } catch (Exception ex) { if (line.hasOption("verbose")) { ex.printStackTrace(System.out); } else { System.out.println(ex.getMessage()); } } }