0

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

1
  • 1
    This question is more appropriate for codereview.se Commented Mar 18, 2013 at 13:55

1 Answer 1

1
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?

Sign up to request clarification or add additional context in comments.

1 Comment

I like the ideas, thanks. I hadn't even considered the possibility of accounting for hyphens being legal arguments. Somehow I got it in my head that the split("-") method would work a little differently. I was considering scanning the args[] array for strings that began with valid parameters and joining that one with the index+1 value then parsing that. Would that perhaps be a better solution?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.