List of usage examples for org.apache.commons.cli CommandLine getArgList
public List getArgList()
From source file:lu.tudor.santec.dicom.gui.header.Dcm2Xml.java
private static CommandLine parse(String[] args) { Options opts = new Options(); Option basedir = new Option("d", true, "store extracted values in files under <basedir>. Cannot be " + "specified together with option -o <xmlfile>."); basedir.setArgName("basedir"); opts.addOption(basedir);/*from w w w . ja v a2s. com*/ Option xmlfile = new Option("o", true, "file to write XML to, standard output by default"); xmlfile.setArgName("xmlfile"); opts.addOption(xmlfile); Option exclude = new Option("x", true, "tag (e.g.: 7FE00010) or name (e.g.: PixelData) of attribute " + "to exclude from XML output"); exclude.setArgName("tag"); opts.addOption(exclude); opts.addOption("X", false, "exclude pixel data from XML output " + "(= shortcut for -x 7FE00010)."); Option xslurl = new Option("T", true, "transform XML output by applying specified XSL stylesheet."); xslurl.setArgName("xslurl"); opts.addOption(xslurl); Option xsltparams = new Option("P", true, "pass specified parameters to the XSL stylesheet."); xsltparams.setArgName("param=value,..."); xsltparams.setValueSeparator('='); xsltparams.setArgs(2); opts.addOption(xsltparams); opts.addOption("I", "incxslt", false, "enable incremental XSLT"); opts.addOption("c", "compact", false, "suppress additional whitespaces in XML output"); opts.addOption("C", "comments", false, "include attribute names as comments in XML output"); 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("dcm2xml: " + e.getMessage()); throw new RuntimeException("unreachable"); } if (cl.hasOption('V')) { Package p = Dcm2Xml.class.getPackage(); System.out.println("dcm2xml v" + p.getImplementationVersion()); System.exit(0); } if (cl.hasOption('h') || cl.getArgList().isEmpty()) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(USAGE, DESCRIPTION, opts, EXAMPLE); System.exit(0); } if (cl.hasOption("o") && cl.hasOption("d")) exit("dcm2xml: Option -o <xmlfile> and -d <basedir> are mutual" + "exclusive"); return cl; }
From source file:io.mindmaps.graql.GraqlShell.java
public static void runShell(String[] args, String version, GraqlClient client) { Options options = new Options(); options.addOption("n", "name", true, "name of the graph"); options.addOption("e", "execute", true, "query to execute"); options.addOption("f", "file", true, "graql file path to execute"); options.addOption("u", "uri", true, "uri to connect to engine"); options.addOption("h", "help", false, "print usage message"); options.addOption("v", "version", false, "print version"); CommandLineParser parser = new DefaultParser(); CommandLine cmd; try {// www.j a v a 2 s . c o m cmd = parser.parse(options, args); } catch (ParseException e) { System.err.println(e.getMessage()); return; } String query = cmd.getOptionValue("e"); String filePath = cmd.getOptionValue("f"); // Print usage message if requested or if invalid arguments provided if (cmd.hasOption("h") || !cmd.getArgList().isEmpty()) { HelpFormatter helpFormatter = new HelpFormatter(); PrintWriter printWriter = new PrintWriter(System.out); int width = helpFormatter.getWidth(); int leftPadding = helpFormatter.getLeftPadding(); int descPadding = helpFormatter.getDescPadding(); helpFormatter.printHelp(printWriter, width, "graql.sh", null, options, leftPadding, descPadding, null); printWriter.flush(); return; } if (cmd.hasOption("v")) { System.out.println(version); return; } String namespace = cmd.getOptionValue("n", DEFAULT_NAMESPACE); String uriString = cmd.getOptionValue("u", DEFAULT_URI); try (GraqlShell shell = new GraqlShell(namespace)) { client.connect(shell, new URI("ws://" + uriString + REMOTE_SHELL_URI)); if (filePath != null) { query = loadQuery(filePath); } if (query != null) { shell.executeQuery(query); shell.commit(); } else { shell.executeRepl(); } } catch (IOException | InterruptedException | ExecutionException | URISyntaxException e) { System.err.println(e.toString()); } finally { client.close(); } }
From source file:com.partagames.imageresizetool.SimpleImageResizeTool.java
/** * Parses all command line arguments and prepares them. * * @param args Command line arguments. * @param options Apache CLI options//from w w w.j ava 2s . c o m * @return True if arguments were prepared correctly and we can continue execution */ private static boolean parseAndPrepareArguments(String[] args, Options options) { // parse through arguments and prepare them appropriately final CommandLineParser parser = new DefaultParser(); final CommandLine cmd; try { cmd = parser.parse(options, args); } catch (MissingOptionException | MissingArgumentException e) { System.out.println(e.getMessage() + "\n"); printHelpAndUsage(); return false; } catch (ParseException e2) { System.out.println( "Error: There was a problem parsing the command line arguments, please check your command.\n"); printHelpAndUsage(); throw new RuntimeException(e2); } // show help if (cmd.hasOption(ARG_HELP)) { printHelpAndUsage(); return false; } if (cmd.getArgList().isEmpty()) { System.out.println("Error: Missing argument: comma-separated list of images!\n"); printHelpAndUsage(); return false; } else { imageFileStrings = cmd.getArgList().get(0).split(","); } // prepare mandatory arguments if (cmd.hasOption(ARG_DIMENSIONS)) { final String[] dimensionStrings = cmd.getOptionValue(ARG_DIMENSIONS).split("x"); try { dimensions = new Dimensions(Integer.parseInt(dimensionStrings[0]), Integer.parseInt(dimensionStrings[1])); } catch (Exception e) { System.out.println("Error: Dimension argument was not correct!\n"); printHelpAndUsage(); return false; } } // prepare optional arguments if (cmd.hasOption(ARG_OUTPUT)) { outputFolder = Paths.get(cmd.getOptionValue(ARG_OUTPUT)); } if (cmd.hasOption(ARG_FORMAT)) { final String outputFormatString = cmd.getOptionValue("format").toLowerCase(); if (Constants.OUTPUT_IMAGE_FORMATS.contains(outputFormatString)) { format = outputFormatString; } else { System.out.println("Error: Wrong output image format!\n"); printHelpAndUsage(); return false; } } if (cmd.hasOption(ARG_HINT)) { final String scalingHintString = cmd.getOptionValue(ARG_HINT); if (SUPPORTED_SCALING_HINTS.contains(scalingHintString)) { scalingHint = scalingHintString; } else { System.out.println("Error: Wrong scaling hint!\n"); printHelpAndUsage(); return false; } } return true; }
From source file:Generate.java
private static void parseCommandline(String[] args) { CommandLineParser parser = new DefaultParser(); HelpFormatter formatter = new HelpFormatter(); CommandLine cmd = null; int compileOptSetTimes = 0; try {/*w ww . ja va 2s .c o m*/ cmd = parser.parse(options, args); } catch (ParseException e) { System.out.println(e.getMessage()); formatter.printHelp("Pyjama:", options); System.exit(1); } //Parse the compilation option if (cmd.hasOption("j2c")) { compileFlag = CompileOption.J2C; compileOptSetTimes++; } else if (cmd.hasOption("j2j")) { compileFlag = CompileOption.J2J; compileOptSetTimes++; } else if (cmd.hasOption("p2j")) { compileFlag = CompileOption.P2J; compileOptSetTimes++; } else if (cmd.hasOption("p2c")) { compileFlag = CompileOption.P2C; compileOptSetTimes++; } //Parse the source files sourceFileNames = cmd.getArgList(); if (cmd.hasOption("h")) { formatter.printHelp("Pyjama", options); } //Parse the target file directory if (cmd.hasOption("d")) { targetFileDirectory = cmd.getOptionValue("d"); } if (compileOptSetTimes > 1) { System.err.println("Error: More than one compile options are set."); System.exit(1); } if (sourceFileNames.isEmpty()) { System.err.println("Error: no input files."); formatter.printHelp("Pyjama", options); } }
From source file:ai.grakn.graql.GraqlShell.java
public static void runShell(String[] args, String version, String historyFilename, GraqlClient client) { Options options = new Options(); options.addOption("k", "keyspace", true, "keyspace of the graph"); options.addOption("e", "execute", true, "query to execute"); options.addOption("f", "file", true, "graql file path to execute"); options.addOption("r", "uri", true, "uri to factory to engine"); options.addOption("b", "batch", true, "graql file path to batch load"); options.addOption("s", "size", true, "the size of the batches (must be used with -b)"); options.addOption("a", "active", true, "the number of active tasks (must be used with -b)"); options.addOption("o", "output", true, "output format for results"); options.addOption("u", "user", true, "username to sign in"); options.addOption("p", "pass", true, "password to sign in"); options.addOption("i", "implicit", false, "show implicit types"); options.addOption("n", "infer", false, "perform inference on results"); options.addOption("m", "materialise", false, "materialise inferred results"); options.addOption("h", "help", false, "print usage message"); options.addOption("v", "version", false, "print version"); CommandLineParser parser = new DefaultParser(); CommandLine cmd; try {//from w w w. j a v a2s . co m cmd = parser.parse(options, args); } catch (ParseException e) { System.err.println(e.getMessage()); return; } Optional<List<String>> queries = Optional.ofNullable(cmd.getOptionValue("e")).map(Lists::newArrayList); String[] filePaths = cmd.getOptionValues("f"); // Print usage message if requested or if invalid arguments provided if (cmd.hasOption("h") || !cmd.getArgList().isEmpty()) { printUsage(options, null); return; } if (cmd.hasOption("v")) { System.out.println(version); return; } String keyspace = cmd.getOptionValue("k", DEFAULT_KEYSPACE); String uriString = cmd.getOptionValue("r", DEFAULT_URI); String outputFormat = cmd.getOptionValue("o", DEFAULT_OUTPUT_FORMAT); Optional<String> username = Optional.ofNullable(cmd.getOptionValue("u")); Optional<String> password = Optional.ofNullable(cmd.getOptionValue("p")); boolean showImplicitTypes = cmd.hasOption("i"); boolean infer = cmd.hasOption("n"); boolean materialise = cmd.hasOption("m"); if (cmd.hasOption("b")) { try { Optional<Integer> activeTasks = Optional.empty(); Optional<Integer> batchSize = Optional.empty(); if (cmd.hasOption("a")) { activeTasks = Optional.of(Integer.parseInt(cmd.getOptionValue("a"))); } if (cmd.hasOption("s")) { batchSize = Optional.of(Integer.parseInt(cmd.getOptionValue("s"))); } try { sendBatchRequest(client.loaderClient(keyspace, uriString), cmd.getOptionValue("b"), activeTasks, batchSize); } catch (IOException e) { throw new RuntimeException(e); } } catch (NumberFormatException e) { printUsage(options, "Cannot cast argument to an integer " + e.getMessage()); } return; } else if (cmd.hasOption("a") || cmd.hasOption("s")) { printUsage(options, "The active or size option has been specified without batch."); return; } try { if (filePaths != null) { queries = Optional.of(loadQueries(filePaths)); } URI uri = new URI("ws://" + uriString + REMOTE_SHELL_URI); new GraqlShell(historyFilename, keyspace, username, password, client, uri, queries, outputFormat, showImplicitTypes, infer, materialise); } catch (java.net.ConnectException e) { System.err.println(ErrorMessage.COULD_NOT_CONNECT.getMessage()); } catch (Throwable e) { System.err.println(getFullStackTrace(e)); } }
From source file:com.netcrest.pado.tools.pado.command.key.java
@Override public void run(CommandLine commandLine, String command) throws Exception { if (commandLine.getArgList().size() < 2) { PadoShell.println(this, padoShell.getKeyClassName()); } else {//from w ww .ja v a 2s . c om if (commandLine.getArgList().size() > 1) { padoShell.setKeyClass((String) commandLine.getArgList().get(1)); } } }
From source file:com.netcrest.pado.tools.pado.command.value.java
@Override public void run(CommandLine commandLine, String command) throws Exception { if (commandLine.getArgList().size() < 2) { PadoShell.println(this, padoShell.getValueClassName()); PadoShell.println(" Use value <class name> to set the value class"); } else {// ww w .ja va 2 s . co m if (commandLine.getArgList().size() > 1) { padoShell.setValueClass((String) commandLine.getArgList().get(1)); } } }
From source file:com.netcrest.pado.tools.pado.command.echo.java
@Override public void run(CommandLine commandLine, String command) throws Exception { if (commandLine.getArgList().size() >= 2) { if (((String) commandLine.getArgList().get(1)).equalsIgnoreCase("true")) { padoShell.setEcho(true);// ww w . j a v a2s . c o m } else if (((String) commandLine.getArgList().get(1)).equalsIgnoreCase("false")) { padoShell.setEcho(false); } else { // message // command is already trimmed. no need to trim int index = command.indexOf(' '); String message = command.substring(index + 1); PadoShell.println(message); return; } } else { PadoShell.println(); } }
From source file:com.netcrest.pado.tools.pado.command.biz.java
@SuppressWarnings("unchecked") @Override/*from w w w .j av a2 s.c o m*/ public void run(CommandLine commandLine, String command) throws Exception { List<String> argList = commandLine.getArgList(); ICatalog catalog = SharedCache.getSharedCache().getPado().getCatalog(); String[] bizClassNames = catalog.getAllBizClassNames(); List<String> list = Arrays.asList(bizClassNames); PrintUtil.printList(list); }
From source file:com.netcrest.pado.tools.pado.command.unsetenv.java
@SuppressWarnings("unchecked") @Override/*from ww w . j ava 2 s.co m*/ public void run(CommandLine commandLine, String command) throws Exception { List<String> argList = commandLine.getArgList(); if (argList.size() == 1) { PadoShell.printlnError(this, "usage: unsetenv <variable>"); return; } else { String variable = argList.get(1); padoShell.setEnv(variable, null); } }