Package org.openstreetmap.josm.tools
Class OptionParser
- java.lang.Object
-
- org.openstreetmap.josm.tools.OptionParser
-
public class OptionParser extends java.lang.Object
A replacement of getopt.Allows parsing command line options
- Since:
- 14415
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static interfaceOptionParser.AvailableOptionprivate static classOptionParser.FoundOptionstatic classOptionParser.OptionCountHow often an option may / must be specified on the command line.static classOptionParser.OptionParseExceptionException thrown when an option cannot be parsed.
-
Field Summary
Fields Modifier and Type Field Description private java.util.HashMap<java.lang.String,OptionParser.AvailableOption>availableOptionsprivate java.lang.Stringprogram
-
Constructor Summary
Constructors Constructor Description OptionParser(java.lang.String program)Create a new option parser.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description OptionParseraddArgumentParameter(java.lang.String optionName, OptionParser.OptionCount count, java.util.function.Consumer<java.lang.String> handler)Add a parameter that expects a string attribute.OptionParseraddFlagParameter(java.lang.String optionName, java.lang.Runnable handler)Adds an option that may be used as a flag, e.g.OptionParseraddShortAlias(java.lang.String optionName, java.lang.String shortName)Adds an alias for the long option --optionName to the short version -nameprivate voidcheckOptionName(java.lang.String optionName)private OptionParser.AvailableOptionfindParameter(java.lang.String optionName)java.util.List<java.lang.String>parseOptions(java.util.List<java.lang.String> arguments)Parses the options.java.util.List<java.lang.String>parseOptionsOrExit(java.util.List<java.lang.String> arguments)Same asparseOptions(List), but exits if option parsing fails.
-
-
-
Field Detail
-
availableOptions
private final java.util.HashMap<java.lang.String,OptionParser.AvailableOption> availableOptions
-
program
private final java.lang.String program
-
-
Constructor Detail
-
OptionParser
public OptionParser(java.lang.String program)
Create a new option parser.- Parameters:
program- The program name.
-
-
Method Detail
-
addShortAlias
public OptionParser addShortAlias(java.lang.String optionName, java.lang.String shortName)
Adds an alias for the long option --optionName to the short version -name- Parameters:
optionName- The long optionshortName- The short version- Returns:
- this
OptionParser
-
addFlagParameter
public OptionParser addFlagParameter(java.lang.String optionName, java.lang.Runnable handler)
Adds an option that may be used as a flag, e.g. --debug- Parameters:
optionName- The parameter namehandler- The handler that is called when the flag is encountered.- Returns:
- this
OptionParser
-
checkOptionName
private void checkOptionName(java.lang.String optionName)
-
addArgumentParameter
public OptionParser addArgumentParameter(java.lang.String optionName, OptionParser.OptionCount count, java.util.function.Consumer<java.lang.String> handler)
Add a parameter that expects a string attribute. E.g.: --config=/path/to/file- Parameters:
optionName- The name of the parameter.count- The number of times the parameter may occur.handler- A function that gets the current object and the parameter. It should throw anOptionParser.OptionParseExceptionif the parameter cannot be handled / is invalid.- Returns:
- this
OptionParser
-
parseOptionsOrExit
public java.util.List<java.lang.String> parseOptionsOrExit(java.util.List<java.lang.String> arguments)
Same asparseOptions(List), but exits if option parsing fails.- Parameters:
arguments- The options- Returns:
- The remaining program arguments that are no options.
-
parseOptions
public java.util.List<java.lang.String> parseOptions(java.util.List<java.lang.String> arguments)
Parses the options.It first checks if all required options are present, if all options are known and validates the option count.
Then, all option handlers are called in the order in which the options are encountered.
- Parameters:
arguments- Program arguments- Returns:
- The remaining program arguments that are no options.
- Throws:
OptionParser.OptionParseException- The error to display if option parsing failed.
-
findParameter
private OptionParser.AvailableOption findParameter(java.lang.String optionName)
-
-