CommandLineParser cmd = new CommandLineParser(args);
cmd.addArgument(new CommandLineParser.Argument("-t", "--test", "0")); // set with short key, long key and default value
cmd.addOption(new CommandLineParser.Option("-q", false)); // set with short key and default value
cmd.addOption(new CommandLineParser.Option("-v", false)); // ...
cmd.addOption(new CommandLineParser.Option("-a", "--amend", true));
cmd.parseCommandLine();
System.out.println("-t Value: " + cmd.getArgumentValue("-t"));
System.out.println("-q Set: " + cmd.isOptionSet("-q"));
System.out.println("-v Set: " + cmd.isOptionSet("-v"));
System.out.println("-a Set: " + cmd.isOptionSet("-a"));
Last active
May 25, 2020 10:53
-
-
Save sebastianknopf/0b93a9efd7214a6aad1b50cbdee36680 to your computer and use it in GitHub Desktop.
a basic parser for command line arguments in java
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 java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Command line argument parser class for parsing command lines like | |
* program.jar -i input.txt -o output.txt --verbose. | |
*/ | |
public class CommandLineParser { | |
/** | |
* The original command line arguments | |
*/ | |
private String[] commandLine; | |
/** | |
* The list of known arguments to parse | |
*/ | |
private List<Argument> argumentList; | |
/** | |
* The list of known options to parse | |
*/ | |
private List<Option> optionList; | |
/** | |
* Initializes a new instance of command line parser class. | |
* | |
* @param commandLine The original command line arguments | |
*/ | |
public CommandLineParser(String[] commandLine) { | |
this.commandLine = commandLine; | |
this.argumentList = new ArrayList<>(); | |
this.optionList = new ArrayList<>(); | |
} | |
/** | |
* Adds a new argument to the list of known arguments. Throws an exception, if there's already | |
* at least one existing argument/option with the same short/long key. | |
* | |
* @param argument The argument to take notice about | |
*/ | |
public void addArgument(Argument argument) { | |
// check for existing arguments | |
for (Argument existingArgument : this.argumentList) { | |
if (existingArgument.getKey().equals(argument.getKey())) { | |
throw new RuntimeException("there's already one argument with short key " + argument.getKey()); | |
} | |
if (existingArgument.getLongKey().equals(argument.getLongKey())) { | |
throw new RuntimeException("there's already one argument with long key " + argument.getKey()); | |
} | |
} | |
// check for existing options | |
for (Option existingOption : this.optionList) { | |
if (existingOption.getKey().equals(argument.getKey())) { | |
throw new RuntimeException("there's already one option with short key " + argument.getKey()); | |
} | |
if (existingOption.getLongKey().equals(argument.getLongKey())) { | |
throw new RuntimeException("there's already one option with long key " + argument.getKey()); | |
} | |
} | |
this.argumentList.add(argument); | |
} | |
/** | |
* Adds a new option to the list of known options. Throws an exception, if there's already | |
* at least one existing argument/option with the same short/long key. | |
* | |
* @param option The option to take notice about | |
*/ | |
public void addOption(Option option) { | |
// check for existing arguments | |
for (Argument existingArgument : this.argumentList) { | |
if (existingArgument.getKey().equals(option.getKey())) { | |
throw new RuntimeException("there's already one argument with short key " + option.getKey()); | |
} | |
if (existingArgument.getLongKey().equals(option.getLongKey())) { | |
throw new RuntimeException("there's already one argument with long key " + option.getKey()); | |
} | |
} | |
// check for existing options | |
for (Option existingOption : this.optionList) { | |
if (existingOption.getKey().equals(option.getKey())) { | |
throw new RuntimeException("there's already one option with short key " + option.getKey()); | |
} | |
if (existingOption.getLongKey().equals(option.getLongKey())) { | |
throw new RuntimeException("there's already one option with long key " + option.getKey()); | |
} | |
} | |
this.optionList.add(option); | |
} | |
/** | |
* Returns the value for a certain argument in the command line. If the argument is not | |
* in the command line, the default value is returned. | |
* | |
* @param argumentKey The (long) key for the argument | |
* @return The argument value | |
*/ | |
public String getArgumentValue(String argumentKey) { | |
for (Argument argument : this.argumentList) { | |
if (argument.getKey().equals(argumentKey) || argument.getLongKey().equals(argumentKey)) { | |
return argument.getValue(); | |
} | |
} | |
return null; | |
} | |
/** | |
* Returns whether a certain option is set in the command line. If the options is not | |
* set, the default state is returned. | |
* | |
* @param optionKey The (long) key for the option | |
* @return Whether the option is set or not | |
*/ | |
public boolean isOptionSet(String optionKey) { | |
for (Option option : this.optionList) { | |
if (option.getKey().equals(optionKey) || option.getLongKey().equals(optionKey)) { | |
return option.isActive(); | |
} | |
} | |
return false; | |
} | |
/** | |
* Parses the input command line against the list of well-known arguments and options. | |
*/ | |
public void parseCommandLine() { | |
// iterate over each cmd argument | |
for (int i = 0; i < this.commandLine.length; i++) { | |
// take a look at the arguments at first ... | |
for (int a = 0; a < this.argumentList.size(); a++) { | |
Argument argument = this.argumentList.get(a); | |
if ((argument.getKey().equals(this.commandLine[i]) || argument.getLongKey().equals(this.commandLine[i])) && (i+1) < this.commandLine.length) { | |
argument.setValue(this.commandLine[++i]); | |
} | |
} | |
// ... and afterwards at the list of options | |
for (int o = 0; o < this.optionList.size(); o++) { | |
Option option = this.optionList.get(o); | |
if (option.getKey().equals(this.commandLine[i]) || option.getLongKey().equals(this.commandLine[i])) { | |
option.setActive(true); | |
} | |
} | |
} | |
} | |
/** | |
* Class for describing a command line argument with short and long key, value and default value. | |
*/ | |
public static class Argument { | |
/** | |
* The argument short key | |
*/ | |
private String argKey; | |
/** | |
* The argument long key | |
*/ | |
private String argLongKey; | |
/** | |
* The argument value after parsing | |
*/ | |
private String argValue; | |
/** | |
* The default value of this argument | |
*/ | |
private String argDefaultValue; | |
/** | |
* Initializes a new instance of argument class. | |
* | |
* @param argKey The short key | |
* @param argDefaultValue The default value | |
*/ | |
public Argument(String argKey, String argDefaultValue) { | |
this(argKey, "", argDefaultValue); | |
} | |
/** | |
* Initializes a new instance of argument class. | |
* | |
* @param argKey The short key | |
* @param argLongKey The long key | |
* @param argDefaultValue The default value | |
*/ | |
public Argument(String argKey, String argLongKey, String argDefaultValue) { | |
this.argKey = argKey; | |
this.argLongKey = argLongKey; | |
if (argDefaultValue != null) { | |
this.argValue = null; | |
this.argDefaultValue = argDefaultValue; | |
} else { | |
this.argValue = null; | |
this.argDefaultValue = null; | |
} | |
} | |
/** | |
* Returns the short key. | |
* | |
* @return The argument's short key | |
*/ | |
public String getKey() { | |
return this.argKey; | |
} | |
/** | |
* Returns the long key. | |
* | |
* @return The argument's long key | |
*/ | |
public String getLongKey() { | |
return this.argLongKey; | |
} | |
/** | |
* Returns the argument value. If not applicable, the default value is returned. | |
* | |
* @return The argument's value. | |
*/ | |
public String getValue() { | |
if (this.argValue != null) { | |
return this.argValue; | |
} else { | |
return this.argDefaultValue; | |
} | |
} | |
/** | |
* Assigns a value to the argument. | |
* | |
* @param argValue The new argument's value | |
*/ | |
public void setValue(String argValue) { | |
this.argValue = argValue; | |
} | |
} | |
/** | |
* Class for describing a command line option with short key, long key and default state. | |
*/ | |
public static class Option { | |
/** | |
* The option short key | |
*/ | |
private String optKey; | |
/** | |
* The option long key | |
*/ | |
private String optLongKey; | |
/** | |
* The option state (set/not set) | |
*/ | |
private boolean active; | |
/** | |
* Initializes a new instance of the option class. | |
* | |
* @param optKey The short key | |
* @param defaultActive The default state | |
*/ | |
public Option(String optKey, boolean defaultActive) { | |
this(optKey, "", defaultActive); | |
} | |
/** | |
* Initializes a new instance of the option class. | |
* | |
* @param optKey The short key | |
* @param optLongKey The long key | |
* @param defaultActive The default state | |
*/ | |
public Option(String optKey, String optLongKey, boolean defaultActive) { | |
this.optKey = optKey; | |
this.optLongKey = optLongKey; | |
this.active = defaultActive; | |
} | |
/** | |
* Returns the short key. | |
* | |
* @return The option's short key | |
*/ | |
public String getKey() { | |
return this.optKey; | |
} | |
/** | |
* Returns the long key. | |
* | |
* @return The option's long key | |
*/ | |
public String getLongKey() { | |
return this.optLongKey; | |
} | |
/** | |
* Returns the current state. | |
* | |
* @return The option's current state | |
*/ | |
public boolean isActive() { | |
return this.active; | |
} | |
/** | |
* Assigns a state to the option. | |
* | |
* @param active The new option's state | |
*/ | |
public void setActive(boolean active) { | |
this.active = active; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment