2

I need a library which can take command line options of the form java -jar --aaa=a --bbb=b ---ccc=c and return an array whose values can be accessed as argsArray['aaa'], argsArray['bbb'] etc.

Is there some library with examples to do this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
vfclists
  • 19,193
  • 21
  • 73
  • 92
  • 2
    *"I don't know much Java but I need it fast."* -1 So hire someone (pay them top rate to reflect your urgency) & stop bothering people on public forums who will answer *if* they feel like it, *when* they are good & ready. – Andrew Thompson Jul 05 '11 at 14:51
  • @Andrew_Thompson My question is whether a library exists that does what I need out of the box, with some sample code. Anyone with knowledge of the answer only needs to point me to it. The programming I am writing does not use any Java at all and just needs a few lines to work with a Java based libraries. – vfclists Jul 05 '11 at 15:55
  • @davogotland I'm not familiar with stackoverflow etiquette, but what if the answers aren't exactly what you seek? e.g looking at the Related questions, this link http://stackoverflow.com/questions/3259143/split-a-string-containing-command-line-parameters-into-a-string-in-java and http://www.docjar.com/docs/api/org/apache/tools/ant/types/Commandline.html#translateCommandline%28String%29 are a closer match. I'm not unappreciative of the top answer here, but do I accept an answer when a better one might exist? I am a total Java newbie, but a closer solution exists, perhaps edit the question? – vfclists Jul 05 '11 at 16:17
  • @vfclists: my answer answers your question. You requested a library and example code; and I have provided you with both. Not only that but the Commons CLI is the benchmark lib for this. What more do you need? – Richard H Jul 06 '11 at 08:22

4 Answers4

4

A great parser for command line options in Java is the Apache Commons CLI.

Options can have arguments or not, can be optional or required, and you can set up descriptions for each for usage help. A brief example usage:

public static void main(String[] args) {

   Options options = new Options();

   try {
      options.addOption(OptionBuilder.withArgName("help").hasArgs(0).withDescription("Prints this help message.").isRequired(false).create("h"));
      options.addOption(OptionBuilder.withArgName("debug logging").hasArgs(0).withDescription("Enable debug logging").isRequired(false).create("1"));

      CommandLineParser parser = new PosixParser();
      CommandLine cmd = parser.parse(options, args);

      if (cmd.hasOption("h")) {
         new HelpFormatter().printHelp(400, "load_page_spool.sh", "OPTIONS", options, "Loads crawl data from pages pool, updating FRONTIER, HISTORY and PageTable", true);
         return;
      }

      ....

   } catch (MissingOptionException e) {
       HelpFormatter formatter = new HelpFormatter();
       formatter.printHelp("LoadPageSpool", options);
   }


}
Richard H
  • 38,037
  • 37
  • 111
  • 138
  • i read the question as asking about a library that simply parse what's there and return a data structure *without stating the names / symbols you're looking for*. if this lib does so, the example doesn't show it. pretty much every language has the shown functionality, but finding the requested functionality seems much more difficult. – nrser Jun 10 '15 at 19:15
0

Try Apache Commons CLI.

Another simple solution might be the helper class presented in this article.

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

If you keep them in a specific order you can access them from the string array that is the parameter for the main method.

http://download.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html

MCBrandenburg
  • 72
  • 2
  • 9
0

Another option to parse command lines would be jcommander. I haven't used it myself but the examples on the website look good and easy to use.

Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118