D. Collins
D. Collins

Reputation: 3

Efficient Command Line Parsing

I have programmed a small Java application for fun, and it all works well. My problem is that when I was working on my method to parse the command-line parameters, I just felt that there should be a much more efficient way of doing it. My program accepts:

-l I (I is an integer for a new lower bound) -u I (I is an integer for a new upper bound) -? (Outputs available command-line options) -v (Activates Verbose output for the main class)

I implemented my parseArgs method in the following way:

private static void parseArgs(String[] arguments) {

String t = "";

for (int c = 0; c < arguments.length; c++) t += arguments[c];

if (t.contains("-")) {
    String[] params = t.split("-");
    for (int c = 0; c < params.length; c++) params[c] = params[c].trim();

    for (int c = 0; c < params.length; c++) {
    if (params[c].startsWith("?") && !docOnly) {
        docOnly = true;
        printHelp();
    }
    if (params[c].startsWith("l") && startPoint == 1) {
        try {
        startPoint = Integer.parseInt(params[c].substring(1));
        if (startPoint < 0) {
            startPoint = 0;
        }
        } catch (NumberFormatException e) {
        error = true;
        System.out.println("\tParameter Error: " + params[c]);
        }
    }
    if (params[c].startsWith("u") && endPoint == 1000) {
        try {
        endPoint = Integer.parseInt(params[c].substring(1));
        if (endPoint < 0) {
            endPoint = 1000;
        }
        } catch (NumberFormatException e) {
        error = true;
        System.out.println("\tParameter Error: " + params[c]);
        }
    }
    if (params[c].startsWith("v") && !verbose) {
        verbose = true;
    }
    }
} else {
    error = true;
    System.out.println("\tError in Parameters. Use -? for available options.");
}
}

As I said, my code all works fine and you can take a look at it if you'd like to verify that. I'm just curious how other programmers, professional or not, would tackle the situation of passing a variable number and order of parameters and having an efficient codepath that would parse them accurately. I know mine isn't the best, which is why I'm wondering. Any language will be okay for an answer, I'm just curious on procedure. I can adapt any language necessary to learn a bit more from it.

Thanks in advance. ~ David Collins

Upvotes: 0

Views: 208

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283783

String t = "";
for (int c = 0; c < arguments.length; c++) t += arguments[c];

Use a StringBuilder instead. Or even easier, join() provided by any of a number of popular libraries.


if (t.contains("-")) {
    String[] params = t.split("-");

Better:

String[] params = t.split("-");
if (params.length > 1) {

While that works for your particular case (arguments are non-negative integers), in general it will be problematic. You should look for - only at the beginning of parameters, so that someone can request a log file named my-log.txt.


startPoint = Integer.parseInt(params[c].substring(1));
if (startPoint < 0) {
    startPoint = 0;
}

All - signs got eaten by split, so startPoint < 0 will never be true.


Why do you set an error flag for non-numeric data, silently ignore numbers out of range or repeated arguments?

Upvotes: 1

Related Questions