List of usage examples for org.apache.commons.cli OptionGroup addOption
public OptionGroup addOption(Option option)
Option
to this group. From source file:org.cleanlogic.sxf4j.utils.Sxf2Pgsql.java
public static void main(String... args) { Options options = new Options(); Option sridOption = new Option("s", true, "Set the SRID field. Defaults to detect from passport or 0. Optionally reprojects from given SRID"); sridOption.setArgName("[<from>:]<srid>"); options.addOption(sridOption);/*from w w w . j av a 2 s . c om*/ Option stTransformOption = new Option("t", false, "Use only PostGIS coordinates transform (ST_Transform), Use with -s option. Not worked with -D. Default: client side convert."); options.addOption(stTransformOption); Option geometryColumnOption = new Option("g", true, "Specify the name of the geometry/geography column"); geometryColumnOption.setArgName("geocolumn"); options.addOption(geometryColumnOption); Option pgdumpFormatOption = new Option("D", false, "Use postgresql dump format (COPY from stdin) (defaults to SQL insert statements)."); options.addOption(pgdumpFormatOption); Option transactionOption = new Option("e", false, "Execute each statement individually, do not use a transaction. Not compatible with -D."); options.addOption(transactionOption); Option spatialIndexOption = new Option("I", false, "Create a spatial index on the geocolumn."); options.addOption(spatialIndexOption); Option geometryTypeOption = new Option("w", false, "Output WKT instead of WKB. Note that this can result in coordinate drift."); options.addOption(geometryTypeOption); Option encodingOption = new Option("W", true, "Specify the character encoding of SXF attribute column. (default: \"UTF-8\")"); encodingOption.setArgName("encoding"); options.addOption(encodingOption); Option tablespaceTableOption = new Option("T", true, "Specify the tablespace for the new table. Note that indexes will still use the default tablespace unless the -X flag is also used."); tablespaceTableOption.setArgName("tablespace"); options.addOption(tablespaceTableOption); Option tablespaceIndexOption = new Option("X", true, "Specify the tablespace for the table's indexes. This applies to the primary key, and the spatial index if the -I flag is used."); tablespaceIndexOption.setArgName("tablespace"); options.addOption(tablespaceIndexOption); Option helpOption = new Option("h", "help", false, "Display this help screen."); options.addOption(helpOption); OptionGroup optionGroup = new OptionGroup(); Option dropTableOption = new Option("d", false, "Drops the table, then recreates it and populates it with current shape file data."); optionGroup.addOption(dropTableOption); Option createTableOption = new Option("c", false, "Creates a new table and populates it, this is the default if you do not specify any options."); optionGroup.addOption(createTableOption); options.addOptionGroup(optionGroup); CommandLineParser commandLineParser = new DefaultParser(); HelpFormatter helpFormatter = new HelpFormatter(); CommandLine commandLine; try { commandLine = commandLineParser.parse(options, args); if (commandLine.hasOption("help") || commandLine.getArgList().size() == 0) { helpFormatter.printHelp("sxf2pgsql [<options>] <sxfile|dir> [[<schema>.]<table>]", options); return; } if (commandLine.hasOption('s')) { String srid = commandLine.getOptionValue('s'); String[] sridPair = srid.split(":"); if (sridPair.length == 2) { sxf2PgsqlOptions.srcSRID = Integer.parseInt(sridPair[0]); sxf2PgsqlOptions.dstSRID = Integer.parseInt(sridPair[1]); } else if (sridPair.length == 1) { sxf2PgsqlOptions.dstSRID = Integer.parseInt(srid); } } sxf2PgsqlOptions.stTransform = commandLine.hasOption('t'); sxf2PgsqlOptions.dropTable = commandLine.hasOption('d'); if (commandLine.hasOption('g')) { sxf2PgsqlOptions.geocolumnName = commandLine.getOptionValue('g'); } sxf2PgsqlOptions.pgdumpFormat = commandLine.hasOption('D'); sxf2PgsqlOptions.transaction = !commandLine.hasOption('e'); sxf2PgsqlOptions.spatialIndex = commandLine.hasOption('I'); if (commandLine.hasOption('w')) { sxf2PgsqlOptions.geometryFormat = "WKT"; } if (commandLine.hasOption('W')) { sxf2PgsqlOptions.encoding = commandLine.getOptionValue('W'); } List<File> files = new ArrayList<>(); if (commandLine.getArgList().size() > 0) { File file = new File(commandLine.getArgList().get(0)); if (file.isFile()) { files.add(file); } else if (file.isDirectory()) { Utils.search(file, files, ".sxf"); } } boolean useNomenclature = true; if (commandLine.getArgList().size() == 2) { String[] schemaTablePair = commandLine.getArgList().get(1).split("."); if (schemaTablePair.length == 2) { sxf2PgsqlOptions.schemaName = schemaTablePair[0]; sxf2PgsqlOptions.tableName = schemaTablePair[1]; } else if (schemaTablePair.length == 0) { sxf2PgsqlOptions.tableName = commandLine.getArgList().get(1); } useNomenclature = false; } // Begin document System.out.println("SET CLIENT_ENCODING TO UTF8;"); System.out.println("SET STANDARD_CONFORMING_STRINGS TO ON;"); System.out.println("SET STATEMENT_TIMEOUT TO 0;"); System.out.println("SET CLIENT_MIN_MESSAGES TO WARNING;"); System.out.println("CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;"); // Create schema.table at once (use from command line params) if (!useNomenclature && sxf2PgsqlOptions.dropTable) { System.out.print(dropTables()); } // Single table mode if (!useNomenclature) { // if (sxf2PgsqlOptions.transaction) { // System.out.println("BEGIN;"); // } System.out.print(createTables()); } for (File file : files) { try { SXFReader sxfReader = new SXFReader(file, true, true); SXFPassport sxfPassport = sxfReader.getPassport(); int srid = sxfPassport.srid(); // if (!Utils.SRID_EX.containsKey(srid)) { Proj4FileReader proj4FileReader = new Proj4FileReader(); String params[] = proj4FileReader.readParametersFromFile("EPSG", String.valueOf(srid)); if (params == null || params.length == 0) { // Wrong srid. Force from passport. srid = sxfPassport.srid(true); } } // if (srid != 0) { sxf2PgsqlOptions.srcSRID = srid; } if (!commandLine.hasOption("t")) { if (sxf2PgsqlOptions.srcSRID != sxf2PgsqlOptions.dstSRID && sxf2PgsqlOptions.dstSRID != 0) { coordinateTransform = createCoordinateTransform(); } } // Each file in separate transaction if (useNomenclature) { sxf2PgsqlOptions.tableName = sxfPassport.getNomenclature(); if (sxf2PgsqlOptions.dropTable) { System.out.print(dropTables()); } if (sxf2PgsqlOptions.transaction) { System.out.println("BEGIN;"); } System.out.print(createTables()); } if (!sxf2PgsqlOptions.pgdumpFormat) { for (int i = 0; i < sxfReader.getCount(); i++) { SXFRecord sxfRecord = sxfReader.getRecordByIncode(i); if (sxfRecord.getLocal() != null) { System.out.print(createInsert(sxfRecord)); } } } else { createCopy(sxfReader); } if (useNomenclature) { if (sxf2PgsqlOptions.spatialIndex) { for (Local local : Local.values()) { System.out.print(createIndex(local)); } } System.out.println("END;"); } coordinateTransform = null; } catch (IOException e) { e.printStackTrace(); } finally { coordinateTransform = null; } } if (!useNomenclature) { if (sxf2PgsqlOptions.spatialIndex) { for (Local local : Local.values()) { System.out.print(createIndex(local)); } } // if (sxf2PgsqlOptions.transaction) { // System.out.println("END;"); // } } } catch (ParseException e) { System.out.println(e.getMessage()); helpFormatter.printHelp("sxf2pgsql [<options>] <sxfile|dir> [[<schema>.]<table>]", options); System.exit(1); } }
From source file:org.codesecure.dependencycheck.utils.CliParser.java
/** * Generates an Options collection that is used to parse the command line * and to display the help message.//from w w w . java2s .c om * * @return the command line options used for parsing the command line */ @SuppressWarnings("static-access") private Options createCommandLineOptions() { Option help = new Option(ArgumentName.HELP_SHORT, ArgumentName.HELP, false, "print this message."); Option advancedHelp = new Option(ArgumentName.ADVANCED_HELP_SHORT, ArgumentName.ADVANCED_HELP, false, "shows additional help regarding properties file."); Option version = new Option(ArgumentName.VERSION_SHORT, ArgumentName.VERSION, false, "print the version information."); Option noupdate = new Option(ArgumentName.DISABLE_AUTO_UPDATE_SHORT, ArgumentName.DISABLE_AUTO_UPDATE, false, "disables the automatic updating of the CPE data."); Option appname = OptionBuilder.withArgName("name").hasArg().withLongOpt(ArgumentName.APPNAME) .withDescription("the name of the application being scanned.").create(ArgumentName.APPNAME_SHORT); Option path = OptionBuilder.withArgName("path").hasArg().withLongOpt(ArgumentName.SCAN) .withDescription("the path to scan - this option can be specified multiple times.") .create(ArgumentName.SCAN_SHORT); Option load = OptionBuilder.withArgName("file").hasArg().withLongOpt(ArgumentName.CPE) .withDescription("load the CPE xml file.").create(ArgumentName.CPE_SHORT); Option props = OptionBuilder.withArgName("file").hasArg().withLongOpt(ArgumentName.PROP) .withDescription("a property file to load.").create(ArgumentName.PROP_SHORT); Option out = OptionBuilder.withArgName("folder").hasArg().withLongOpt(ArgumentName.OUT) .withDescription("the folder to write reports to.").create(ArgumentName.OUT_SHORT); //TODO add the ability to load a properties file to override the defaults... OptionGroup og = new OptionGroup(); og.addOption(path); og.addOption(load); Options opts = new Options(); opts.addOptionGroup(og); opts.addOption(out); opts.addOption(appname); opts.addOption(version); opts.addOption(help); opts.addOption(noupdate); opts.addOption(props); opts.addOption(advancedHelp); return opts; }
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/* ww w. jav a2s . c o 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.dataaccessioner.DataAccessioner.java
/** * @param args the command line arguments * @throws org.apache.commons.cli.ParseException *//* ww w.j a v a2 s . c om*/ public static void main(String[] args) throws ParseException { DataAccessioner da = new DataAccessioner(); //Default settings String collectionName = ""; String accessionNumber = ""; String submitterName = ""; String srcNote = ""; String addNote = ""; String runTime = ""; Options options = new Options(); options.addOption("c", true, "Collection Name (required if no GUI)"); options.addOption("a", true, "Accession Number (required if no GUI)"); options.addOption("n", true, "Submitter name (required if no GUI)"); options.addOption(Option.builder().hasArg().longOpt("about-srcnote") .desc("Note about the source (optional)").argName("SRC NOTE").build()); options.addOption(Option.builder().hasArg().longOpt("add-note").desc("Additional note (optional)") .argName("ADDL NOTE").build()); options.addOption("u", false, "Do not start GUI; requires a source and destination"); options.addOption("v", false, "print version information"); options.addOption("h", false, "print this message"); OptionGroup fitsOptions = new OptionGroup(); fitsOptions.addOption(new Option("s", false, "Run FITS on source")); fitsOptions.addOption(new Option("x", false, "Don't run FITS; only copy")); options.addOptionGroup(fitsOptions); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("h")) { printHelp(options); System.exit(0); } if (cmd.hasOption("v")) { System.out.println(DataAccessioner.VERSION); System.exit(0); } if (cmd.hasOption("c")) { collectionName = cmd.getOptionValue("c"); } if (cmd.hasOption("a")) { accessionNumber = cmd.getOptionValue("a"); } if (cmd.hasOption("n")) { submitterName = cmd.getOptionValue("n"); } if (cmd.hasOption("about-srcnote")) { srcNote = cmd.getOptionValue("about-srcnote"); } if (cmd.hasOption("add-note")) { addNote = cmd.getOptionValue("add-note"); } try { if (cmd.hasOption("x")) { da.fits = null; } else { logger.info("Starting FITS"); da.fits = new Fits(); } } catch (FitsException ex) { System.err.println("FITS failed to initialize."); Exceptions.printStackTrace(ex); } //Get the destination & source File destination = null; List<File> sources = new ArrayList<File>(); if (!cmd.getArgList().isEmpty()) { destination = new File((String) cmd.getArgList().remove(0)); //validate sources or reject them for (Object sourceObj : cmd.getArgList()) { File source = new File(sourceObj.toString()); if (source.canRead()) { sources.add(source); } } } if (cmd.hasOption("u")) {//Unattended if (collectionName.isEmpty() || accessionNumber.isEmpty() || submitterName.isEmpty()) { System.err.println( "A collection name, a submitter name, and an accession number must be provided if not using the GUI."); printHelp(options); } else if (destination == null || !(destination.isDirectory() && destination.canWrite())) { String destinationStr = "<blank>"; if (destination != null) { destinationStr = destination.toString(); } System.err.println("Cannot run automatically. The destination (" + destinationStr + ") is either not a valid or writable directory."); printHelp(options); } else if (sources.isEmpty()) { System.err.println("Cannot run automatically. At least one valid source is required."); printHelp(options); } else { HashMap<String, String> daCmdlnMetadata = new HashMap<>(); daCmdlnMetadata.put("collectionName", collectionName); daCmdlnMetadata.put("accessionNumber", accessionNumber); daCmdlnMetadata.put("submitterName", submitterName); daCmdlnMetadata.put("aboutSourceNote", srcNote); daCmdlnMetadata.put("addNote", addNote); da.runUnattended(destination, sources, daCmdlnMetadata); } } else { //Start GUI try { // Set cross-platform Java L&F (also called "Metal") UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (UnsupportedLookAndFeelException e) { // handle exception } catch (ClassNotFoundException e) { // handle exception } catch (InstantiationException e) { // handle exception } catch (IllegalAccessException e) { // handle exception } DASwingView view = new DASwingView(da); view.pack(); view.setVisible(true); } }
From source file:org.dcm4che.tool.common.CLIUtils.java
@SuppressWarnings("static-access") public static void addPriorityOption(Options opts) { OptionGroup group = new OptionGroup(); group.addOption( OptionBuilder.withLongOpt("prior-high").withDescription(rb.getString("prior-high")).create()); group.addOption(OptionBuilder.withLongOpt("prior-low").withDescription(rb.getString("prior-low")).create()); opts.addOptionGroup(group);/*from ww w . j ava 2 s . c o m*/ }
From source file:org.dcm4che.tool.common.CLIUtils.java
@SuppressWarnings("static-access") public static void addEncodingOptions(Options opts) { opts.addOption(null, "group-len", false, rb.getString("group-len")); OptionGroup sqlenGroup = new OptionGroup(); sqlenGroup.addOption(OptionBuilder.withLongOpt("expl-seq-len").withDescription(rb.getString("expl-seq-len")) .create(null));//from w ww . ja v a 2s . co m sqlenGroup.addOption(OptionBuilder.withLongOpt("undef-seq-len") .withDescription(rb.getString("undef-seq-len")).create(null)); opts.addOptionGroup(sqlenGroup); OptionGroup itemlenGroup = new OptionGroup(); itemlenGroup.addOption(OptionBuilder.withLongOpt("expl-item-len") .withDescription(rb.getString("expl-item-len")).create(null)); itemlenGroup.addOption(OptionBuilder.withLongOpt("undef-item-len") .withDescription(rb.getString("undef-item-len")).create(null)); opts.addOptionGroup(itemlenGroup); }
From source file:org.dcm4che.tool.common.CLIUtils.java
@SuppressWarnings("static-access") public static void addTransferSyntaxOptions(Options opts) { OptionGroup group = new OptionGroup(); group.addOption( OptionBuilder.withLongOpt("explicit-vr").withDescription(rb.getString("explicit-vr")).create()); group.addOption(/*from www . j a v a 2s .c o m*/ OptionBuilder.withLongOpt("big-endian").withDescription(rb.getString("big-endian")).create()); group.addOption( OptionBuilder.withLongOpt("implicit-vr").withDescription(rb.getString("implicit-vr")).create()); opts.addOptionGroup(group); }
From source file:org.dcm4che.tool.dcm2dcm.Dcm2Dcm.java
@SuppressWarnings("static-access") private static CommandLine parseComandLine(String[] args) throws ParseException { Options opts = new Options(); CLIUtils.addCommonOptions(opts);/*w w w . j a v a2s . co m*/ CLIUtils.addEncodingOptions(opts); OptionGroup tsGroup = new OptionGroup(); tsGroup.addOption(OptionBuilder.withLongOpt("transfer-syntax").hasArg().withArgName("uid") .withDescription(rb.getString("transfer-syntax")).create("t")); tsGroup.addOption(OptionBuilder.withLongOpt("jpeg").withDescription(rb.getString("jpeg")).create()); tsGroup.addOption(OptionBuilder.withLongOpt("jpll").withDescription(rb.getString("jpll")).create()); tsGroup.addOption(OptionBuilder.withLongOpt("jpls").withDescription(rb.getString("jpls")).create()); tsGroup.addOption(OptionBuilder.withLongOpt("j2kr").withDescription(rb.getString("j2kr")).create()); tsGroup.addOption(OptionBuilder.withLongOpt("j2ki").withDescription(rb.getString("j2ki")).create()); opts.addOptionGroup(tsGroup); OptionGroup fmiGroup = new OptionGroup(); fmiGroup.addOption(OptionBuilder.withLongOpt("no-fmi").withDescription(rb.getString("no-fmi")).create("F")); fmiGroup.addOption( OptionBuilder.withLongOpt("retain-fmi").withDescription(rb.getString("retain-fmi")).create("f")); opts.addOptionGroup(fmiGroup); opts.addOption(OptionBuilder.hasArg().withArgName("max-error").withType(PatternOptionBuilder.NUMBER_VALUE) .withDescription(rb.getString("verify")).withLongOpt("verify").create()); opts.addOption(OptionBuilder.hasArg().withArgName("size").withType(PatternOptionBuilder.NUMBER_VALUE) .withDescription(rb.getString("verify-block")).withLongOpt("verify-block").create()); opts.addOption(OptionBuilder.hasArg().withArgName("quality").withType(PatternOptionBuilder.NUMBER_VALUE) .withDescription(rb.getString("quality")).create("q")); opts.addOption( OptionBuilder.hasArg().withArgName("encoding-rate").withType(PatternOptionBuilder.NUMBER_VALUE) .withDescription(rb.getString("encoding-rate")).create("Q")); opts.addOption(OptionBuilder.hasArgs().withArgName("name=value").withValueSeparator() .withDescription(rb.getString("compression-param")).create("C")); CommandLine cl = CLIUtils.parseComandLine(args, opts, rb, Dcm2Dcm.class); return cl; }
From source file:org.dcm4che.tool.dcm2xml.Dcm2Xml.java
@SuppressWarnings("static-access") private static void addBulkdataOptions(Options opts) { OptionGroup group = new OptionGroup(); group.addOption( OptionBuilder.withLongOpt("no-bulkdata").withDescription(rb.getString("no-bulkdata")).create("B")); group.addOption(OptionBuilder.withLongOpt("with-bulkdata").withDescription(rb.getString("with-bulkdata")) .create("b")); opts.addOptionGroup(group);//from www. ja v a 2s . c o m opts.addOption(OptionBuilder.withLongOpt("blk-file-dir").hasArg().withArgName("directory") .withDescription(rb.getString("blk-file-dir")).create("d")); opts.addOption(OptionBuilder.withLongOpt("blk-file-prefix").hasArg().withArgName("prefix") .withDescription(rb.getString("blk-file-prefix")).create()); opts.addOption(OptionBuilder.withLongOpt("blk-file-suffix").hasArg().withArgName("suffix") .withDescription(rb.getString("blk-file-dir")).create()); opts.addOption("c", "cat-blk-files", false, rb.getString("cat-blk-files")); opts.addOption(OptionBuilder.withLongOpt("blk-spec").hasArg().withArgName("xml-file") .withDescription(rb.getString("blk-spec")).create("X")); }
From source file:org.dcm4che.tool.dcmdir.DcmDir.java
@SuppressWarnings("static-access") private static void addCommandOptions(OptionGroup cmdGroup) { cmdGroup.addOption( OptionBuilder.hasArg().withArgName("dicomdir").withDescription(rb.getString("list")).create("l")); cmdGroup.addOption(/* w ww.j av a2s . c om*/ OptionBuilder.hasArg().withArgName("dicomdir").withDescription(rb.getString("create")).create("c")); cmdGroup.addOption( OptionBuilder.hasArg().withArgName("dicomdir").withDescription(rb.getString("update")).create("u")); cmdGroup.addOption( OptionBuilder.hasArg().withArgName("dicomdir").withDescription(rb.getString("delete")).create("d")); cmdGroup.addOption( OptionBuilder.hasArg().withArgName("dicomdir").withDescription(rb.getString("purge")).create("p")); cmdGroup.addOption(OptionBuilder.hasArg().withArgName("dicomdir").withDescription(rb.getString("compact")) .create("z")); }