public ThompsonTraillCompletenessParameters(String[] arguments) throws ParseException { super(arguments); if (cmd.hasOption("fileName")) fileName = cmd.getOptionValue("fileName"); if (fileName.equals("stdout")) useStandardOutput = true; if (cmd.hasOption("limit")) limit = Integer.parseInt(cmd.getOptionValue("limit")); if (cmd.hasOption("offset")) offset = Integer.parseInt(cmd.getOptionValue("offset")); if (offset > -1 && limit > -1) limit += offset; }
/** * validArgs checks the command line parameters and verifies the existence of Files. * * @param args Command line parameters. * @return A list of file(s) that are to be transformed. */ static Collection<Path> validArgs(String... args) { Collection<Path> returnValue = new LinkedList<>(); try { CommandLine line = cli(args); if (shouldContinue(line)) { returnValue = checkArgs(line); } else { formatter.printHelp("convert", options, true); } } catch (ParseException pe) { DEV_LOG.error(CLI_PROBLEM, pe); } return returnValue; }
public void extractTables(CommandLine line) throws ParseException { if (line.hasOption('b')) { if (line.getArgs().length != 0) { throw new ParseException("Filename specified with batch\nTry --help for help"); } File pdfDirectory = new File(line.getOptionValue('b')); if (!pdfDirectory.isDirectory()) { throw new ParseException("Directory does not exist or is not a directory"); } extractDirectoryTables(line, pdfDirectory); return; } if (line.getArgs().length != 1) { throw new ParseException("Need exactly one filename\nTry --help for help"); } File pdfFile = new File(line.getArgs()[0]); if (!pdfFile.exists()) { throw new ParseException("File does not exist"); } extractFileTables(line, pdfFile); }
@Test public void itShouldGetTheExternalFields() throws ParseException { String[] args = new String[] { "-xmlpath", "testData/XMLFiles/DelimitedInputAndOutput.xml", "-param", "PATH=testData/Input/delimitedInputFile","-param","PATH2=testData/XMLFiles/schema2.xml", "-param", "input=input'1","-param", "out&put=output"}; try { ParameterSubstitutor parameterSubstitutor = new ParameterSubstitutor( new UserParameters(args)); String xmlContents = XmlParsingUtils.getXMLStringFromPath(args[1]); ParseExternalElements parseExternalElements = new ParseExternalElements( parameterSubstitutor.substitute(xmlContents), parameterSubstitutor); Document xmlDom = parseExternalElements.getXmlDom(); NodeList nodes = xmlDom.getElementsByTagName("schema"); Assert.assertEquals(3,nodes.item(0).getChildNodes().getLength()); } catch (IOException e) { LOG.error("", e); } }
/** * Parse the arguments for commands * * @param args the argument to be parsed * @param helpDescription help information to be printed out * @param out Printer * @param printGenericCommandUsage whether to print the * generic command usage defined in ToolRunner * @return true when the argument matches help option, false if not */ public static boolean parseHelpArgument(String[] args, String helpDescription, PrintStream out, boolean printGenericCommandUsage) { if (args.length == 1) { try { CommandLineParser parser = new PosixParser(); CommandLine cmdLine = parser.parse(helpOptions, args); if (cmdLine.hasOption(helpOpt.getOpt()) || cmdLine.hasOption(helpOpt.getLongOpt())) { // should print out the help information out.println(helpDescription + "\n"); if (printGenericCommandUsage) { ToolRunner.printGenericCommandUsage(out); } return true; } } catch (ParseException pe) { return false; } } return false; }
private void validateOptions() throws ParseException { if (!compress.equals("none") && !compress.equals("lzo") && !compress.equals("gz")) { throw new ParseException("Unknown compression scheme: " + compress); } if (minKeyLen >= maxKeyLen) { throw new ParseException( "Max key length must be greater than min key length."); } if (minValLength >= maxValLength) { throw new ParseException( "Max value length must be greater than min value length."); } if (minWordLen >= maxWordLen) { throw new ParseException( "Max word length must be greater than min word length."); } return; }
public MyOptions(String[] args) { seed = System.nanoTime(); try { Options opts = buildOptions(); CommandLineParser parser = new GnuParser(); CommandLine line = parser.parse(opts, args, true); processOptions(line, opts); validateOptions(); } catch (ParseException e) { System.out.println(e.getMessage()); System.out.println("Try \"--help\" option for details."); setStopProceed(); } }
private void validateOptions() throws ParseException { if (!compress.equals("none") && !compress.equals("lzo") && !compress.equals("gz")) { throw new ParseException("Unknown compression scheme: " + compress); } if (!format.equals("tfile") && !format.equals("seqfile")) { throw new ParseException("Unknown file format: " + format); } if (minWordLen >= maxWordLen) { throw new ParseException( "Max word length must be greater than min word length."); } return; }
/** * @param args cmd params */ public QConfig prepareCLI(String[] args) { try { // parse the command line arguments line = parser.parse(options, args); } catch (ParseException exp) { // oops, something went wrong throw new RuntimeException("Parsing failed. Reason: ", exp); } new HelpFormatter().printHelp("command line parameters for QMS QSystem...", options); // automatically generate the help statement if (line.hasOption("help") || line.hasOption("h") || line.hasOption("?")) { System.exit(0); } QLog.l().logger().info("Properties are ready."); return this; }
@Test public void testDefaults() { String[] arguments = new String[]{"a-marc-file.mrc"}; try { ValidatorParameters parameters = new ValidatorParameters(arguments); assertNotNull(parameters.getArgs()); assertEquals(1, parameters.getArgs().length); assertEquals("a-marc-file.mrc", parameters.getArgs()[0]); assertFalse(parameters.doHelp()); assertNotNull(parameters.getFileName()); assertEquals("validation-report.txt", parameters.getFileName()); assertFalse(parameters.useStandardOutput()); assertEquals(-1, parameters.getLimit()); assertEquals(-1, parameters.getOffset()); assertFalse(parameters.doSummary()); assertEquals(ValidationErrorFormat.TEXT, parameters.getFormat()); } catch (ParseException e) { e.printStackTrace(); } }
private void validateOptions() throws ParseException { if (!compress.equals("none") && !compress.equals("lzo") && !compress.equals("gz") && !compress.equals("snappy")) { throw new ParseException("Unknown compression scheme: " + compress); } if (minKeyLen >= maxKeyLen) { throw new ParseException( "Max key length must be greater than min key length."); } if (minValLength >= maxValLength) { throw new ParseException( "Max value length must be greater than min value length."); } if (minWordLen >= maxWordLen) { throw new ParseException( "Max word length must be greater than min word length."); } return; }
/** * Gets the first introduced argument of an option parsed to double. * * @param optionArgs The option introduced arguments. * @param emptyValue Value which is returned if the option was not introduced, or was introduced * with no argument. * @param argsOption The option. * @return The option first introduced argument parsed to double. * If the option was not introduced, or was introduced with no argument, emptyValue is * returned. * @throws ParseException If an error occurs parsing the argument to double. */ private static double getOptionFirstArgDouble( String[] optionArgs, double emptyValue, ArgsOptions argsOption) throws ParseException { String optionArg = getOptionFirstArg(optionArgs); if (optionArg == null || optionArg.isEmpty()) { return emptyValue; } try { return Double.parseDouble(optionArg); } catch (NumberFormatException numberFormatException) { throw new ParseException(String.format( "Option %s must be a decimal number.", argsOption.display())); } }
@Test public void testSearch() { String[] arguments = new String[]{"--search", "920$a=book", "a-marc-file.mrc"}; try { FormatterParameters parameters = new FormatterParameters(arguments); assertNotNull(parameters.getSearch()); assertTrue(parameters.hasSearch()); assertEquals("920$a=book", parameters.getSearch()); assertEquals("920$a", parameters.getPath()); assertEquals("book", parameters.getQuery()); } catch (ParseException e) { e.printStackTrace(); } }
private static boolean parseArguments(String[] args) { CommandLineParser parser = new BasicParser(); try { CommandLine commandLine = parser.parse(OPTIONS, args); inputPath = commandLine.getOptionValue(INPUT_PATH.getOpt()); outputPath = new Path(commandLine.getOptionValue(OUTPUT_PATH.getOpt())); pattern = Pattern.compile(commandLine.getOptionValue(PATTERN.getOpt())); if (commandLine.hasOption(SAMPLE_FRACTION.getOpt())) { sampleFraction = Double.valueOf(commandLine.getOptionValue(SAMPLE_FRACTION.getOpt())); } if (sampleFraction < 0 || sampleFraction > 1) { return false; } return true; } catch (ParseException | IllegalArgumentException e) { return false; } }
public static void main(String[] args) { Options options = getOptions(); CommandLine cmd = null; try { cmd = parseCommandLineArgs(options, args); } catch (ParseException e) { System.out.format("%s.%n", e.getMessage()); final HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("ConsoleReader", options); System.exit(1); } final String scope = cmd.getOptionValue("scope") == null ? Constants.DEFAULT_SCOPE : cmd.getOptionValue("scope"); final String streamName = cmd.getOptionValue("name") == null ? Constants.DEFAULT_STREAM_NAME : cmd.getOptionValue("name"); final String uriString = cmd.getOptionValue("uri") == null ? Constants.DEFAULT_CONTROLLER_URI : cmd.getOptionValue("uri"); final URI controllerURI = URI.create(uriString); ConsoleReader reader = new ConsoleReader(scope, streamName, controllerURI); reader.run(); }
@Test public void testDefaults() { String[] arguments = new String[]{"a-marc-file.mrc"}; try { FormatterParameters parameters = new FormatterParameters(arguments); assertNotNull(parameters.getArgs()); assertEquals(1, parameters.getArgs().length); assertEquals("a-marc-file.mrc", parameters.getArgs()[0]); assertFalse(parameters.doHelp()); assertNull(parameters.getId()); assertNull(parameters.getFormat()); } catch (ParseException e) { e.printStackTrace(); } }
static void parseForHelp(String[] args) { // parse for help options try { Options helpOnly = new Options(); helpOnly.addOption(options.getOption("h")); CommandLine cli = new DefaultParser().parse(helpOnly, args, true); // no -h option - do nothing if (!cli.hasOption("h")) { return; } } catch (ParseException pe) { System.err.println(pe.getMessage()); System.exit(-1); } // print help text new HelpFormatter().printHelp("java -jar sgc-xxx.jar", "Simple Glacier Client (sgc) | Version: 0.1\n\n", options, "\n"+"See: https://github.com/arjuan/simple-glacier-client"+"\n\n", true); }
/** * "Parsing" stage of command-line processing demonstrated with * Apache Commons CLI. * * @param options Options from "definition" stage. * @param commandLineArguments Command-line arguments provided to application. * @return Instance of CommandLine as parsed from the provided Options and * command line arguments; may be {@code null} if there is an exception * encountered while attempting to parse the command line options. */ private static CommandLine generateCommandLine( final Options options, final String[] commandLineArguments) { final CommandLineParser cmdLineParser = new DefaultParser(); CommandLine commandLine = null; try { commandLine = cmdLineParser.parse(options, commandLineArguments); } catch (ParseException parseException) { out.println( "ERROR: Unable to parse command-line arguments " + Arrays.toString(commandLineArguments) + " due to: " + parseException); } return commandLine; }
public static void main(String[] args) { CommandLineParser parser = new DefaultParser(); try { // parse the command line arguments CommandLine line = parser.parse(buildOptions(), args); if (line.hasOption('h')) { printHelp(); System.exit(0); } if (line.hasOption('v')) { System.out.println(VERSION_STRING); System.exit(0); } new CommandLineApp(System.out, line).extractTables(line); } catch (ParseException exp) { System.err.println("Error: " + exp.getMessage()); System.exit(1); } System.exit(0); }
public static void main(String[] args) { Options options = getOptions(); CommandLine cmd = null; try { cmd = parseCommandLineArgs(options, args); } catch (ParseException e) { System.out.format("%s.%n", e.getMessage()); final HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("HelloWorldWriter", options); System.exit(1); } final String scope = cmd.getOptionValue("scope") == null ? Constants.DEFAULT_SCOPE : cmd.getOptionValue("scope"); final String streamName = cmd.getOptionValue("name") == null ? Constants.DEFAULT_STREAM_NAME : cmd.getOptionValue("name"); final String uriString = cmd.getOptionValue("uri") == null ? Constants.DEFAULT_CONTROLLER_URI : cmd.getOptionValue("uri"); final URI controllerURI = URI.create(uriString); HelloWorldWriter hww = new HelloWorldWriter(scope, streamName, controllerURI); final String routingKey = cmd.getOptionValue("routingKey") == null ? Constants.DEFAULT_ROUTING_KEY : cmd.getOptionValue("routingKey"); final String message = cmd.getOptionValue("message") == null ? Constants.DEFAULT_MESSAGE : cmd.getOptionValue("message"); hww.run(routingKey, message); }
@Override public CliCommand parse(String[] cmdArgs) throws CliParseException { Parser parser = new PosixParser(); CommandLine cl; try { cl = parser.parse(options, cmdArgs); } catch (ParseException ex) { throw new CliParseException(ex); } args = cl.getArgs(); if (args.length < 2) { throw new CliParseException(getUsageStr()); } return this; }
@Override public void importTable(com.cloudera.sqoop.manager.ImportJobContext context) throws IOException, ImportException { context.setConnManager(this); // The user probably should have requested --direct to invoke external // table option. // Display a warning informing them of this fact. if (!NetezzaManager.directModeWarningPrinted) { LOG.warn("It looks like you are importing from Netezza."); LOG.warn("This transfer can be faster! Use the --direct"); LOG.warn("option to exercise a Netezza-specific fast path."); NetezzaManager.directModeWarningPrinted = true; // don't display this // twice. } try { handleNetezzaImportExtraArgs(context); } catch (ParseException pe) { throw (ImportException) new ImportException(pe.getMessage(), pe); } // Then run the normal importTable() method. super.importTable(context); }
private void handleNetezzaImportExtraArgs(ImportJobContext context) throws ParseException { SqoopOptions opts = context.getOptions(); Configuration conf = opts.getConf(); String[] extraArgs = opts.getExtraArgs(); conf.setBoolean(NETEZZA_DATASLICE_ALIGNED_ACCESS_OPT, false); if (extraArgs != null && extraArgs.length > 0 && ConfigurationHelper.getConfNumMaps(conf) > 1) { RelatedOptions netezzaOpts = getNetezzaExtraOpts(); CommandLine cmdLine = new GnuParser().parse(netezzaOpts, extraArgs, true); if (cmdLine.hasOption(NETEZZA_DATASLICE_ALIGNED_ACCESS_LONG_ARG)) { conf.setBoolean(NETEZZA_DATASLICE_ALIGNED_ACCESS_OPT, true); context.setInputFormat(NetezzaDataDrivenDBInputFormat.class); } } }
public static CommandLine parseCommandLine(String nameApp, String descriptionApp, String[] args, Options options) { CommandLineParser parser = new BasicParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException e) { usage(nameApp, descriptionApp, options); } return cmd; }
private int configureChildTool(SqoopOptions childOptions, SqoopTool childTool, String [] childArgv) { // Within the child arguments there may be a '--' followed by // dependent args. Stash them off to the side. // Everything up to the '--'. String [] parseableChildArgv = getElementsUpToDoubleDash(childArgv); // The '--' and any subsequent args. String [] extraChildArgv = getElementsAfterDoubleDash(childArgv); // Now feed the arguments into the tool itself. try { childOptions = childTool.parseArguments(parseableChildArgv, null, childOptions, false); childTool.appendArgs(extraChildArgv); childTool.validateOptions(childOptions); } catch (ParseException pe) { LOG.error("Error parsing arguments to the job-specific tool."); LOG.error("See 'sqoop help <tool>' for usage."); return 1; } catch (SqoopOptions.InvalidOptionsException e) { System.err.println(e.getMessage()); return 1; } return 0; // Success. }
@Override /** * Processes arguments to options but only strips matched quotes. */ public void processArgs(Option opt, ListIterator iter) throws ParseException { // Loop until an option is found. while (iter.hasNext()) { String str = (String) iter.next(); if (getOptions().hasOption(str) && str.startsWith("-")) { // found an Option, not an argument. iter.previous(); break; } // Otherwise, this is a value. try { // Note that we only strip matched quotes here. addValForProcessing.invoke(opt, stripMatchedQuotes(str)); } catch (IllegalAccessException iae) { throw new RuntimeException(iae); } catch (java.lang.reflect.InvocationTargetException ite) { // Any runtime exception thrown within addValForProcessing() // will be wrapped in an InvocationTargetException. iter.previous(); break; } catch (RuntimeException re) { iter.previous(); break; } } if (opt.getValues() == null && !opt.hasOptionalArg()) { throw new MissingArgumentException(opt); } }
/** * Tries to parse commandline arguments. * @param args Array of commandline arguments. * @return CommandLine object containing arguments. */ private static CommandLine getArgs(String[] args) { BasicParser parser = new BasicParser(); try { return parser.parse(getOptions(), args, false); } catch (ParseException e) { logger.error("[!] Error while parsing command-line arguments: " + Arrays.toString(args), e); System.exit(-1); return null; } }
@Test void shouldAllowEmptyTemplateScope() throws ParseException { //when Assertions.assertThrows(MissingArgumentException.class, () -> { CommandLine line = ConversionEntry.cli("-t"); ConversionEntry.shouldContinue(line); }); }
@Test void shouldAllowValidTemplateScopes() throws ParseException { //when CommandLine line = ConversionEntry.cli(new String[] {"file.txt", "-t", QrdaScope.ACI_SECTION.name() + "," + QrdaScope.IA_SECTION.name()}); boolean result = ConversionEntry.shouldContinue(line); //then assertWithMessage("Both should be valid scopes") .that(result).isTrue(); }
@Test void testHandleSkipDefaults() throws ParseException { CommandLine line = ConversionEntry.cli(SKIP_DEFAULTS); assertWithMessage("Should have a skip default option") .that(line.hasOption(ConversionEntry.SKIP_DEFAULTS)) .isTrue(); }
public void parse(String ... argv) throws ParseException { CommandLineParser parser = new PosixParser(); CommandLine cmdLine = parser.parse(options, argv); if (cmdLine.hasOption(helpOpt.getOpt()) || cmdLine.hasOption(helpOpt.getLongOpt())) { shouldPrintHelp = true; return; } boolean hasGetEdit = cmdLine.hasOption(geteditsizeOpt.getOpt()); boolean hasCheckpoint = cmdLine.hasOption(checkpointOpt.getOpt()); if (hasGetEdit && hasCheckpoint) { throw new ParseException("May not pass both " + geteditsizeOpt.getOpt() + " and " + checkpointOpt.getOpt()); } if (hasGetEdit) { cmd = Command.GETEDITSIZE; } else if (hasCheckpoint) { cmd = Command.CHECKPOINT; String arg = cmdLine.getOptionValue(checkpointOpt.getOpt()); if ("force".equals(arg)) { shouldForce = true; } else if (arg != null) { throw new ParseException("-checkpoint may only take 'force' as an " + "argument"); } } if (cmdLine.hasOption(formatOpt.getOpt())) { shouldFormat = true; } }
@Test void testHandleSkipValidationAbbreviated() throws ParseException { CommandLine line = ConversionEntry.cli("-v"); assertWithMessage("Should have a skip validation option") .that(line.hasOption(ConversionEntry.SKIP_VALIDATION)) .isTrue(); }
@Test void testHandleCombo() throws ParseException { CommandLine line = ConversionEntry.cli("-dvt", "meep"); assertWithMessage("Should have a skip validation option") .that(line.hasOption(ConversionEntry.SKIP_VALIDATION)).isTrue(); assertWithMessage("Should have a skip default option") .that(line.hasOption(ConversionEntry.SKIP_DEFAULTS)).isTrue(); assertThat(line.getOptionValue(ConversionEntry.TEMPLATE_SCOPE)) .isEqualTo("meep"); }
@Test void testHandleTemplateScope() throws ParseException { CommandLine line = ConversionEntry.cli(TEMPLATE_SCOPE, "meep"); assertWithMessage("Should have a template scope option") .that(line.hasOption(ConversionEntry.TEMPLATE_SCOPE)) .isTrue(); assertThat(line.getOptionValue(ConversionEntry.TEMPLATE_SCOPE)) .isEqualTo("meep"); }
public static List<Integer> parsePagesOption(String pagesSpec) throws ParseException { if (pagesSpec.equals("all")) { return null; } List<Integer> rv = new ArrayList<>(); String[] ranges = pagesSpec.split(","); for (int i = 0; i < ranges.length; i++) { String[] r = ranges[i].split("-"); if (r.length == 0 || !Utils.isNumeric(r[0]) || r.length > 1 && !Utils.isNumeric(r[1])) { throw new ParseException("Syntax error in page range specification"); } if (r.length < 2) { rv.add(Integer.parseInt(r[0])); } else { int t = Integer.parseInt(r[0]); int f = Integer.parseInt(r[1]); if (t > f) { throw new ParseException("Syntax error in page range specification"); } rv.addAll(Utils.range(t, f + 1)); } } Collections.sort(rv); return rv; }
public static void main(String[] args) throws ParseException { Options options = new Options() .addOption("region", true, "the region containing the kinesis stream") .addOption("bucket", true, "the bucket containing the raw event data") .addOption("prefix", true, "the prefix of the objects containing the raw event data") .addOption("stream", true, "the name of the kinesis stream the events are sent to") .addOption("speedup", true, "the speedup factor for replaying events into the kinesis stream") .addOption("aggregate", "turn on aggregation of multiple events into a kinesis record") .addOption("seek", true, "start replaying events at given timestamp") .addOption("statisticsFrequency", true, "print statistics every statisticFrequency ms") .addOption("help", "print this help message"); CommandLine line = new DefaultParser().parse(options, args); if (line.hasOption("help")) { new HelpFormatter().printHelp(MethodHandles.lookup().lookupClass().getName(), options); } else { StreamPopulator populator = new StreamPopulator( line.getOptionValue("region", "eu-west-1"), line.getOptionValue("bucket", "aws-bigdata-blog"), line.getOptionValue("prefix", "artifacts/flink-refarch/data/"), line.getOptionValue("stream", "taxi-trip-events"), line.hasOption("aggregate"), Float.valueOf(line.getOptionValue("speedup", "6480")), Long.valueOf(line.getOptionValue("statisticsFrequency", "60000")) ); if (line.hasOption("seek")) { populator.seek(new DateTime(line.getOptionValue("seek"))); } populator.populate(); } }
private static CommandLine parseArguments(Configuration conf, Options options, String[] args) throws ParseException, IOException { GenericOptionsParser genParser = new GenericOptionsParser(conf, args); String[] remainingArgs = genParser.getRemainingArgs(); CommandLineParser parser = new PosixParser(); return parser.parse(options, remainingArgs); }
@SuppressWarnings("unchecked") public int rm(String[] args) { Option recursive = OptionBuilder.withArgName("recursive") .withDescription("delete recursively") .create("r"); Options rmOption = new Options(); rmOption.addOption(recursive); boolean recursiveOpt = false; CommandLineParser parser = new GnuParser(); try { CommandLine line = parser.parse(rmOption, args); List<String> argsList = line.getArgList(); if (argsList.size() != 2) { return usageError("RM requires exactly one path argument", RM_USAGE); } if (!validatePath(argsList.get(1))) { return -1; } try { if (line.hasOption("r")) { recursiveOpt = true; } registry.delete(argsList.get(1), recursiveOpt); return 0; } catch (Exception e) { syserr.println(analyzeException("rm", e, argsList)); } return -1; } catch (ParseException exp) { return usageError("Invalid syntax " + exp.toString(), RM_USAGE); } }