Class KCmdArgParser
java.lang.Object
ch.k43.util.KCmdArgParser
Yet another simple command line argument parser.
The pattern to initialize the parser must be in the following format: "xyyyyz:xyyyyz":
Command line pattern syntax: - Multiple patterns are separated by colons (e.g. ?-l:?-a) - 1st character must be be ? for an optional or ! for a required argument - The following string is either a short or long option (e.g. -x or --xxx) or a specifies a single or multiple argument (. or ..) - The last character can be set for an optional ? or required option value ? Example: - ?-l:?-a:!. Allows optional arguments -l and -a, but requires one single argument - ?-n?:!.. Allows the optional argument -n (with an optional value, e.g. -n or -n=10) and at least one argument - ?--header=! Allows an optional long option. If specified, it requires an option value, e.g. --header=yes - (empty) Empty pattern prohibits any arguments Command line parsing rules: - All arguments are case sensitive - Short options may be combined, e.g. -la instead of -l -a - Short option characters are a-z, A-Z and 0-9 - Long option names may include - (e.g. --top-header) - Short or long option may have a value (e.g. -n=10 or --header=none) - All tokens after '--' are treated as arguments to allow arguments with a starting dash, e.g. cmd -l -- -filename.txt
Code example: KCmdArgParser argParser = new KCmdArgParser("?-l:?-a:!."); if (!argParser(args)) { System.err.println("Syntax error: " + argParser.getLastError()); System.err.println("Usage: cmd -l -a filename"); System.exit(1); } if (argParser.hasOption("-l")) { ... }
- Since:
- 2025.09.02
-
Constructor Summary
ConstructorsConstructorDescriptionKCmdArgParser
(String argPattern) Initialize the parser with the syntax pattern for each valid command line argument. -
Method Summary
Modifier and TypeMethodDescriptionReturn the first command line argument.int
Return number of command line arguments.String[]
Return all command line arguments.Return all arguments as given in the command line (e.g.Return the last error message or null for no errors.getOptionValue
(String argOption) Get the passed option value.boolean
Check if any command line argument given.boolean
Check if the passed option is present.boolean
hasOptionValue
(String argOption) Check if the passed option value is present.boolean
Parse the passed command line arguments.toString()
String representation of object.
-
Constructor Details
-
KCmdArgParser
Initialize the parser with the syntax pattern for each valid command line argument.- Parameters:
argPattern
- Command line pattern rule or null to prevent any argument
-
-
Method Details
-
getArgumentCount
public int getArgumentCount()Return number of command line arguments.- Returns:
- Number of command line arguments
-
getArguments
Return all command line arguments.- Returns:
- Array with all command line arguments.
-
getCommandLine
Return all arguments as given in the command line (e.g. "-l -a arg1 arg2 arg3").- Returns:
- Command line arguments separated by a blank character or null if no argument were given
-
getLastError
Return the last error message or null for no errors.- Returns:
- Last error message
-
getOptionValue
-
getArgument
Return the first command line argument.- Returns:
- First command line argument or null.
-
hasArgument
public boolean hasArgument()Check if any command line argument given.- Returns:
- True if any argument given or false
-
hasOption
Check if the passed option is present.- Parameters:
argOption
- Option name (e.g. -l or --headers)- Returns:
- True if found, else otherwise
-
hasOptionValue
Check if the passed option value is present.- Parameters:
argOption
- Option name (e.g. -l or --headers)- Returns:
- True if found, else otherwise
-
parse
Parse the passed command line arguments.- Parameters:
argArgs
- Array of Strings as passed to any main() method.- Returns:
- True if parsing successful, false otherwise
- See Also:
-
toString
-