apache-commons-cli

With CommonsCli, how do I parse an Option which can occur several times and has a flexible number of values?

≯℡__Kan透↙ 提交于 2021-02-11 15:00:45
问题 In some legacy code I'm porting a diy-command-line parser to Apache CommonsCli. I can't break previously allowed+documented Options, and one of the Options is giving me trouble: The Option has either one or two args, and can be specified as many times as desired. Option: [-option arg1 [arg2]]+ I want the result as String[][] as following: cli -option a b -option c should result in [ [a, b], [c,] ] and cli -option a -option b c should result in [ [a,], [b, c] ] My code looks something like

With CommonsCli, how do I parse an Option which can occur several times and has a flexible number of values?

和自甴很熟 提交于 2021-02-11 14:58:18
问题 In some legacy code I'm porting a diy-command-line parser to Apache CommonsCli. I can't break previously allowed+documented Options, and one of the Options is giving me trouble: The Option has either one or two args, and can be specified as many times as desired. Option: [-option arg1 [arg2]]+ I want the result as String[][] as following: cli -option a b -option c should result in [ [a, b], [c,] ] and cli -option a -option b c should result in [ [a,], [b, c] ] My code looks something like

Apache CLI: Required options contradicts with help option.

强颜欢笑 提交于 2020-06-11 16:53:08
问题 If I have 2 options defined as required such as: public static void main(String [] args){ Options options= new Options(); Option inputFileOp=Option.builder("i").longOpt("input").hasArg().desc("Input file").argName("file").required().build(); options.addOption(inputFileOp); Option outputFileOp=Option.builder("o").longOpt("output").hasArg().desc("Output file").argName("file").required().build(); options.addOption(outputFileOp); and a help option Option helpOp =new Option("h",false,"Show Help");

How to use Apache Commons CLI to parse the property file and --help option?

99封情书 提交于 2019-12-24 12:57:57
问题 I have a property file which is like this - hostName=machineA.domain.host.com emailFrom=tester@host.com emailTo=world@host.com emailCc=hello@host.com And now I am reading the above property file from my Java program as shown below. I am parsing the above property file manual way as of now - public class FileReaderTask { private static String hostName; private static String emailFrom; private static String emailTo; private static String emailCc; private static final String configFileName =

Using Apache common-cli to parse arguments

ⅰ亾dé卋堺 提交于 2019-12-24 11:12:24
问题 Apache common-cli has a example on its web site for ls command: options.addOption( "a", "all", false, "do not hide entries starting with ." ); options.addOption( "A", "almost-all", false, "do not list implied . and .." ); options.addOption( "b", "escape", false, "print octal escapes for nongraphic " + "characters" ); options.addOption( OptionBuilder.withLongOpt( "block-size" ) .withDescription( "use SIZE-byte blocks" ) .hasArg() .withArgName("SIZE") .create() ); This shows help like this: -a,

Why default parser (using commons-cli) does not throw exception when option is not recognized?

丶灬走出姿态 提交于 2019-12-24 10:38:14
问题 I would like to have a printed usage help message, when option which is specified as an program argument is not valid (not available in predefined options list). CommandLineParser parser = new BasicParser(); try { CommandLine line = parser.parse(getOptions(), args); } catch( ParseException exp ) { getLogger().info("Invalid command line option name. "+exp.getMessage()); HelpFormatter hf = new HelpFormatter(); hf.printHelp("destDir", getOptions()); return false; } return true; I type as an

Hadoop NoSuchMethodError apache.commons.cli

徘徊边缘 提交于 2019-12-23 09:33:25
问题 I'm using hadoop-2.7.2 and I did a MapReduceJob with IntelliJ. In my job, I'm using apache.commons.cli-1.3.1 and I put the lib in the jar. When I use the MapReduceJob on my Hadoop cluster I have a NoSuchMethodError : Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.cli.Option.builder(Ljava/lang/String;)Lorg/apache/commons/cli/Option$Builder; I don't understand because the method exist in the class Option and the class Option is extracted from the commons-cli.jar to

Apache Commons CLI - ordering help options?

岁酱吖の 提交于 2019-12-23 07:39:18
问题 I'm using the Apache Commons CLI. By default it orders help options on the command line alphabetically by key. So, what appears is: -csv -ip -msisdn -xml But I want to order them as follows: -csv -xml -ip -msisdn I know that there is a OptionFormatter class you can use and pass to the HelpFormatter, but can't see any examples on how to use it for my purposes above (http://www.marko.homeunix.org/programming/java/commons-cli/api/org/apache/commons/cli/HelpFormatter.OptionComparator.html). Just

command line parsing using apache commons cli

青春壹個敷衍的年華 提交于 2019-12-22 10:53:16
问题 M tryng to use apache commons cli , My use case is variable number of arguments with some options. Say -p str1 str2; It can be -p str1 str2 str3 .. strn Another is -m str1 -h with cmdline.getOptionValues("p"); It fetches only last string.How can I fetch all the values of an particular option? Edit: if(cmdline.hasOption("p")){ String[] argsList = cmdline.getOptionValues(p); String strLine = Arrays.toString(argsList); argsList = strLine.split(","); } M i doing it right? will string consist of

Apache Commons CLI - print trailing args in help output

▼魔方 西西 提交于 2019-12-22 10:44:27
问题 I'm using Apache Commons CLI 1.2 to parse a command line that takes options and extra arguments at the end. Ex: mycmd -d DIR extra stuff I know how to get 'extra' and 'stuff' using CommandLine.getArgs() , but I don't know how to display those extra arguments in my help output. When I make a call like this: new HelpFormatter().printHelp("mycmd", opts, true); I get output like: usage: mycmd -d DIR without the extra arguments. Could someone point me in the right direction? 回答1: As far as I can