1

I am making a Command Line program, I am using if conditions to check what the user entered and launch a specific method.

input = bufferedReader.readLine;

if (input.equals("search"))
    search();


So as for now, to preform a search, the user must enter:

>>> search
>>> aTextToSearchFor

But I want the user to be able to enter

>>> search -c 3

so, 3 will be passed as a parameter to the search() method
also, search only should be valid and a default of 0 will be passed instead


Note:

  • I am open to implement this without the -c, so the command will be search 5
  • I have found about Apache Commons CLI & Jcommander, but I can't find a simple enough explanation to understand any thing :D
Omar Elrefaei
  • 379
  • 2
  • 9
  • If you can use it take a look at the [Apache Commons CLI library](http://commons.apache.org/proper/commons-cli/), it's quite easy to use. There are enough examples on SO or just google it :) – xander Mar 15 '17 at 07:07

1 Answers1

1

You might find String.split to be helpful. Maybe something like this will help you get started?

input = bufferedReader.readLine();
String[] args = input.split(" +"); // split on one or more spaces
if (args[0].equals("search")) {
    if (args.length > 2) {
        // args may be an array like {"search", "-c", "5"}.
        // After you confirm that, you may need to convert args[2]
        // from a String to an int using Integer.parseInt
        // (more code here)
    }
}

It is fine to start simple like this when your program is small. If your program is starting to get big and you feel like you're writing similar code over and over, then it's probably a good time to look into a command parsing library.

You mentioned Apache Commons CLI or Jcommander, but I personally find argparse4j to be a good balance between features and simplicity. It has a fluent API, meaning that your code would look something like:

ArgumentParser p = ArgumentParsers.newArgumentParser(myCommandName)
        .description(myCommandDescription);
p.addArgument(...).help(...)...
...
p.parseArgsOrFail(args);
Brandon
  • 2,367
  • 26
  • 32
  • Thanks for your answer, I am already familiar with this staff (the first version was +600 lines of code). I wanted to expand the code functionality, so I asked because I felt I was going to repeat code (I think this is also the case when using (varargs). I am feeling like there is a gap between the tutorial/examples that I already knew and what I can't understand, I would appreciate if you explains how to get started with one of these librarie, and then I can figure things out from the documentation. (something like a hello world application) – Omar Elrefaei Mar 15 '17 at 08:01
  • what about the examples here? http://stackoverflow.com/questions/367706/how-to-parse-command-line-arguments-in-java – xander Mar 15 '17 at 08:09
  • Yeh, I have seen this before. The thing is: I want to use the options while the program is being executed NOT when luanching the .class file – Omar Elrefaei Mar 15 '17 at 08:43
  • OK, sorry, your question didn't make it clear that you have solid programming experience. Sorry about that. I've personally found [Argparse4j](https://argparse4j.github.io/) to be a good balance of functionality and simplicity. I'll edit my answer to mention that. – Brandon Mar 15 '17 at 19:25