Last active
September 21, 2017 14:27
-
-
Save eldargab/d933334e01b288cc362bbb0f047d4e28 to your computer and use it in GitHub Desktop.
A small wrapper around args4j
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.kohsuke.args4j.ClassParser; | |
import org.kohsuke.args4j.CmdLineException; | |
import org.kohsuke.args4j.CmdLineParser; | |
import org.kohsuke.args4j.Option; | |
import java.io.PrintStream; | |
import java.io.StringWriter; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.function.Consumer; | |
public class Args { | |
private static class Help { | |
@Option(name = "-help", help = true) | |
boolean help; | |
} | |
private StringBuilder help = new StringBuilder(); | |
private Help helpOption = new Help(); | |
private CmdLineParser parser = new CmdLineParser(helpOption); | |
public void add(Object options) { | |
new ClassParser().parse(options, parser); | |
} | |
public void usage(String line) { | |
help.append(line); | |
help.append('\n'); | |
} | |
public void usage() { | |
help.append('\n'); | |
} | |
public void parse(String[] args) { | |
try { | |
parser.parseArgument(args); | |
} catch (CmdLineException e) { | |
fail(e.getMessage()); | |
} | |
if (helpOption.help) { | |
String text = help.toString(); | |
StringWriter usage = new StringWriter(); | |
parser.printUsage(usage, null); | |
System.out.println(); | |
System.out.println(text | |
.replace("{options}", usage.toString()) | |
); | |
System.out.println(); | |
System.exit(0); | |
} | |
} | |
public void parse(String[] args, Object... options) { | |
for (Object o: options) { | |
add(o); | |
} | |
parse(args); | |
} | |
public static void fail(String msg) { | |
System.err.println(msg); | |
System.exit(1); | |
} | |
public interface Main { | |
void main(String[] args) throws Exception; | |
} | |
public interface Commands { | |
void def(String name, String description, Main main); | |
} | |
private static class Command { | |
String name; | |
String description; | |
Main main; | |
} | |
public static void parseCommands(String[] args, Consumer<Commands> spec) throws Exception { | |
List<Command> commands = new ArrayList<>(); | |
spec.accept((name, description, main) -> { | |
Command cmd = new Command(); | |
cmd.name = name; | |
cmd.description = description; | |
cmd.main = main; | |
commands.add(cmd); | |
}); | |
if (commands.size() == 0) throw new IllegalArgumentException("No commands given"); | |
if (args.length == 0) { | |
usage(System.err, commands); | |
System.exit(1); | |
} | |
String cmd = args[0]; | |
String[] cmdArgs = Arrays.copyOfRange(args, 1, args.length); | |
for (Command c : commands) { | |
if (cmd.equals(c.name)) { | |
c.main.main(cmdArgs); | |
return; | |
} | |
} | |
if (cmd.equals("help") || cmd.equals("-help")) { | |
usage(System.out, commands); | |
} else { | |
System.err.println("Unknown command: " + cmd + "\n"); | |
usage(System.err, commands); | |
System.exit(1); | |
} | |
} | |
private static void usage(PrintStream out, List<Command> commands) { | |
out.println("Usage: <command> [options...]\n"); | |
out.println("Available commands are:\n"); | |
int width = commands.stream().mapToInt(c -> c.name.length()).max().getAsInt(); | |
for (Command c : commands) { | |
out.print(c.name); | |
for (int i = 0; i < width - c.name.length() + 3; i++) { | |
out.print(' '); | |
} | |
out.println(c.description); | |
} | |
out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment