Package net.morimekta.console.args
Class ArgumentParser
- java.lang.Object
-
- net.morimekta.console.args.ArgumentParser
-
public class ArgumentParser extends java.lang.Object
Argument argumentParser class. This is the actual argumentParser that is initialized with a set of options, arguments and properties, and is then initialized with the appropriate fields.
-
-
Constructor Summary
Constructors Constructor Description ArgumentParser(java.lang.String program, java.lang.String version, java.lang.String description)
Create an argument argumentParser instance.ArgumentParser(java.lang.String program, java.lang.String version, java.lang.String description, ArgumentOptions argumentOptions)
Create an argument argumentParser instance.ArgumentParser(ArgumentParser parent, java.lang.String subCommand, java.lang.String description)
Create a sub-command argument parser for the given sub-command.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <A extends BaseArgument>
ArgumentParseradd(A arg)
Add a sub-command.<O extends BaseOption>
ArgumentParseradd(O option)
Add a command line option.java.lang.String
getDescription()
Short description of the program.java.lang.String
getProgram()
The name of the program.java.lang.String
getProgramDescription()
Get the program description line.java.lang.String
getSingleLineUsage()
Get the single line usage string for the parser.java.lang.String
getVersion()
The program version string.void
parse(java.lang.String... args)
Parse arguments from the main method.void
parse(ArgumentList args)
Parse arguments from the main method.void
printUsage(java.io.OutputStream out)
Print the option usage list.void
printUsage(java.io.OutputStream out, boolean showHidden)
Print the option usage list.void
printUsage(java.io.PrintWriter writer, boolean showHidden)
Print the option usage list.void
validate()
Validate all options and arguments.
-
-
-
Constructor Detail
-
ArgumentParser
public ArgumentParser(java.lang.String program, java.lang.String version, java.lang.String description)
Create an argument argumentParser instance.- Parameters:
program
- The program name.version
- The program version.description
- The program description.
-
ArgumentParser
public ArgumentParser(java.lang.String program, java.lang.String version, java.lang.String description, ArgumentOptions argumentOptions)
Create an argument argumentParser instance.- Parameters:
program
- The program name.version
- The program version.description
- The program description.argumentOptions
- The argument options.
-
ArgumentParser
public ArgumentParser(ArgumentParser parent, java.lang.String subCommand, java.lang.String description)
Create a sub-command argument parser for the given sub-command.- Parameters:
parent
- Parent argument parser to defer with on unknown options.subCommand
- The name of the sub-command the parser is created for.description
- The sub-command description.
-
-
Method Detail
-
getProgram
public java.lang.String getProgram()
The name of the program. Should be essentially what the user types on the command line to invoke the program.- Returns:
- The program name.
-
getVersion
public java.lang.String getVersion()
The program version string.- Returns:
- The program version.
-
getDescription
public java.lang.String getDescription()
Short description of the program. Should be the string that is shown on the top of the program usage help, usually just a few words. Should be capitalized.- Returns:
- The program description.
-
add
public <O extends BaseOption> ArgumentParser add(O option)
Add a command line option.- Type Parameters:
O
- The base option type.- Parameters:
option
- The option to add.- Returns:
- The argument argumentParser.
-
add
public <A extends BaseArgument> ArgumentParser add(A arg)
Add a sub-command.- Type Parameters:
A
- The base argument type.- Parameters:
arg
- The command to add.- Returns:
- The argument argumentParser.
-
parse
public void parse(java.lang.String... args)
Parse arguments from the main method.- Parameters:
args
- The argument array.
-
parse
public void parse(ArgumentList args)
Parse arguments from the main method.- Parameters:
args
- The argument list.
-
validate
public void validate()
Validate all options and arguments.
-
printUsage
public void printUsage(java.io.OutputStream out)
Print the option usage list. Essentially printed as a list of options with the description indented where it overflows the available line width.- Parameters:
out
- The output stream.
-
printUsage
public void printUsage(java.io.OutputStream out, boolean showHidden)
Print the option usage list. Essentially printed as a list of options with the description indented where it overflows the available line width.- Parameters:
out
- The output stream.showHidden
- Whether to show hidden options.
-
printUsage
public void printUsage(java.io.PrintWriter writer, boolean showHidden)
Print the option usage list. Essentially printed as a list of options with the description indented where it overflows the available line width.- Parameters:
writer
- The output printer.showHidden
- Whether to show hidden options.
-
getProgramDescription
public java.lang.String getProgramDescription()
Get the program description line. Contains essentially the line "description - version".- Returns:
- The program description.
-
getSingleLineUsage
public java.lang.String getSingleLineUsage()
Get the single line usage string for the parser. Contains essentially the line "program options args".- Returns:
- The single line usage.
-
-