Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java

Idiom #162 Execute procedures depending on options

execute bat if b is a program option and fox if f is a program option.

import java.util.Set;
var argsSet = Set.of(args);
if (argsSet.contains("b")) bat();
if (argsSet.contains("f")) fox();
import static java.util.List.of;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class X {
    void bat() {}
    void fox() {}
    public static void main(String[] args) {
        List<String> s = of(args);
        interface F { void set(); }
        Map<String, F> m
            = new LinkedHashMap<>();
        X x = new X();
        m.put("b", x::bat);
        m.put("f", x::fox);
        for (Entry e : m.entrySet())
            if (s.contains(e.getKey()))
                ((F) e.getValue()).set();
    }
}

"... The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program."
—https://docs.oracle.com/javase/tutorial/getStarted/application/index.html#MAIN
#include <unistd.h>
int main(int argc, char * argv[])
{
        int optch;
        while ((optch = getopt(argc, argv, "bf")) != -1) {
                switch (optch) {
                        case 'b': bat(); break;
                        case 'f': fox(); break;
                }
        }
        return 0;
}

New implementation...
Bzzzzzzzzzz