Showing posts with label arguments. Show all posts
Showing posts with label arguments. Show all posts

Sunday, February 18, 2024

CommandLine Arguments

Here's an interesting way to check for commandline arguments. First we setup an enum that holds our commands. As you can see, I can pass in either -h or -helpK into my program. To do this we convert the values for a specific command into a HashSet and then compare the values to what was passed in. If what we passed in is in the HashSet then we return a true vlaue, otherwise we return a false value.

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public enum Commands {

    HELP("-h,-help");

    private final String values;
    Commands(String values) {
        this.values = values;
    }

    public String getValues() {
        return this.values;
    }

    public Set<String> getValuesAsArray() {
        return new HashSet<>(Arrays.asList(this.getValues().split(",")));
    }

    public boolean isSelected(String[] args) {
        for (String arg : args) {
            if (getValuesAsArray().contains(arg)) {
                return true;
            }
        }
        return false;
    }
}

In our main method of the program, we can check for values like this:

public class Main {

    public static void main(String[] args) {
        if (Commands.HELP.isSelected(args)) {
            System.out.println("Help");
        }
    }
}

At which point we perform whatever code we need to perform on it. There is a way to pass in parameters to the arguments that we are passing to the program too. But that's another story altogether.

I'm not saying this is the perfect or correct way of doing this, but it is a way of doing this for sure.

Sunday, February 4, 2024

Other kinds of Argument checking

So we've talked about a possible argument checking in Java. What if you just want to pass in files to be worked on?

That's simple enough, just loop through your argument array and perform the work on the files in question. Here's a simple way of doing that.

public static void main(String[] args) {
    for (String arg : args) {
    	doSomethingSpecial(arg);
    }
}

You can easily save the results back out to the same file as well since you will have that current argument in your posession. Easy right? Yeah something like that.

What if you want to perform an action based if an argument is passed in?

Let's say that you pass the filename as the first argument, then you pass the operations you wish to perform on that argument in the following arguments.

You can easily get the first argument by doing args[0], and then you can put the remaining arguments in a HashSet and check one by one if the specified argument is there. If it is, then perform the specified operation on it that you define in your codebase. Let me see if I have an example of this:

public static void main(String[] args) {
        Set arguments = new HashSet<>(Arrays.asList(args));
        if (arguments.contains("doit")) {
            System.out.println("Doing it!");
        }
    }

Yep I did have an example. How's that for something? It all works out in the end. You can of course modify the code based upon whatever circumstance you need to work with.

Slump

 I feel like I'm in a slump. I can't even think of what to write about. The cursor just sits there. It's a staring match that wo...