0
\$\begingroup\$

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";
                            break;
                        } else if (args[i+1].equalsIgnoreCase("central")){
                            locParameter = "centralOffice";
                            break;
                        } else {
                            System.err.println("Unknow parameter for location, default value: centralOffice");
                            break;
                        }
            case "-p":  dirResult = args[i+1];
                        break;
            case "-kpi": counterParameter = args[i+1].toUpperCase();
                        break;
            case "-t":  try { 
                            periodParameter = Integer.parseInt(args[i+1]) ;               
                        } catch(NumberFormatException e){
                            System.err.println("Error parsing parameter, default value 24 hours");
                        } finally {
                            break;
                        }
        }
        i++;
    }

    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.

\$\endgroup\$
6
  • 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
    Commented Feb 5, 2016 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\$ Commented Feb 5, 2016 at 10:22
  • 1
    \$\begingroup\$ Another one is jopt simple - their home page lists alternatives. \$\endgroup\$
    – assylias
    Commented Feb 5, 2016 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
    Commented Feb 5, 2016 at 10:26
  • \$\begingroup\$ Why does dirResult have double-slashes? \$\endgroup\$ Commented Feb 6, 2016 at 8:10

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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