I made a command line application which uses parameters. So I try to make a system like the linux command.
So I have 4 parameters, initialized to a default value, used if wrong or none parameter are given. The user can modify those parameter by calling associate options "-options".

   public static void main(String [] args) {
    String locParameter = "centralOffice";
    String dirResult = "//home//"+System.getProperty("user.name")+"//Documents//ResultMyApp";
    int periodParameter = 24;
    String counterParameter = "ALL";
    int i = 0;

    for (String s: args) {
        switch (s) {
            case "-o":  if (args[i+1].equalsIgnoreCase("remote")){
                            locParameter = "remoteOffice";
                        } else if (args[i+1].equalsIgnoreCase("central")){
                            locParameter = "centralOffice";
                        } else {
                            System.err.println("Unknow parameter for location, default value: centralOffice");
            case "-p":  dirResult = args[i+1];
            case "-kpi": counterParameter = args[i+1].toUpperCase();
            case "-t":  try { 
                            periodParameter = Integer.parseInt(args[i+1]) ;               
                        } catch(NumberFormatException e){
                            System.err.println("Error parsing parameter, default value 24 hours");
                        } finally {

    System.out.println("parameter choosed: " + locParameter + ", " + periodParameter + ", " + counterParameter );}

So I am wondering, is this ok? Of course it works but maybe it is not the easiest way to do it or the most optimized code.

  • 4
    \$\begingroup\$ Unless you are doing it as an exercise, it would make more sense to use an existing library such as jcommander or equivalent. \$\endgroup\$ – assylias Feb 5 '16 at 10:21
  • \$\begingroup\$ It is not an exercise, I searched a bit on google before doing this but I found only people speaking about doing their own code. Thanks I will look at this. \$\endgroup\$ – Charly Roch Feb 5 '16 at 10:22
  • 1
    \$\begingroup\$ Another one is jopt simple - their home page lists alternatives. \$\endgroup\$ – assylias Feb 5 '16 at 10:24
  • \$\begingroup\$ jcommander is great, but you have to add your own fix against program -- or something like that - the last time I used it, running it with -- as arguments crashed the library \$\endgroup\$ – Pimgd Feb 5 '16 at 10:26
  • \$\begingroup\$ Why does dirResult have double-slashes? \$\endgroup\$ – 200_success Feb 6 '16 at 8:10

Parameter validation is sorely missing. For example, if you call this with -o as last argument (no more arguments), the program will crash with an array index out of bounds exception and a stack trace. Getting a lengthy stack trace for invalid output is not very UNIX-y ;-)

The finally in the exception handling is not necessary and a bit confusing. I suggest simply moving the break after the catch.

The indenting is a bit strange. I suggest to use an IDE to reformat nicely.

In any case, instead of worrying your own, I suggest using a library. Recently I tried Argparse4j, and I'm happy to say I really like it, works very well for me.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.