Package pro.johndunlap.getopt
Class GetOpt
- java.lang.Object
-
- pro.johndunlap.getopt.GetOpt
-
public class GetOpt extends Object
The main entry point for the getopt-databind library.- Author:
- John Dunlap
-
-
Constructor Summary
Constructors Constructor Description GetOpt()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> List<OptionInfo>extract(Class<T> classType)This method is shared between the bind and help methods.PrintStreamgetErr()PrintStreamgetOut()<T> Stringhelp(Class<T> classType)Generates a help message for the given class type.<T> Tread(Class<T> classType, String[] args)Binds the given arguments to the given class type.<T> ParseContext<T>readContext(Class<T> classType, String[] args)Same asread(Class, String[])except that it returns aParseContextinstead of the instance.GetOptregister(Class<?> type, TypeConverter<?> typeConverter)GetOptregister(Map<Class<?>,TypeConverter<?>> typeConverters)<T> Trun(Class<T> classType, String[] args)This method automates the mechanics behind binding, error handling, and displaying the help message.GetOptsetErr(PrintStream err)GetOptsetExitMechanism(ExitMechanism exitMechanism)GetOptsetOut(PrintStream out)protected <T> voidshowHelp(Class<T> classType)
-
-
-
Method Detail
-
register
public GetOpt register(Class<?> type, TypeConverter<?> typeConverter)
-
register
public GetOpt register(Map<Class<?>,TypeConverter<?>> typeConverters)
-
read
public <T> T read(Class<T> classType, String[] args) throws ParseException
Binds the given arguments to the given class type.- Type Parameters:
T- The type of the class to bind the arguments to- Parameters:
classType- The class type to bind the arguments toargs- The arguments to bind to the class type- Returns:
- An instance of the class type with the arguments bound to it
- Throws:
ParseException- If the arguments could not be bound to the class type
-
readContext
public <T> ParseContext<T> readContext(Class<T> classType, String[] args) throws ParseException
Same asread(Class, String[])except that it returns aParseContextinstead of the instance.- Type Parameters:
T- The type of the class to bind the arguments to- Parameters:
classType- The class type to bind the arguments toargs- The arguments to bind to the class type- Returns:
- A
ParseContextcontaining the instance of the class type with the arguments - Throws:
ParseException- If the arguments could not be bound to the class type
-
showHelp
protected <T> void showHelp(Class<T> classType)
-
run
public <T> T run(Class<T> classType, String[] args)
This method automates the mechanics behind binding, error handling, and displaying the help message. Incoming arguments are bound to the specified class type and the run method is invoked after binding has been completed. If binding fails, validation messages will be printed to stderr and an appropriate exit status will be set. If the help message is requested, it will be printed to stdout prior to exit.- Type Parameters:
T- The type of the class to bind the arguments to- Parameters:
classType- The class type to bind the arguments toargs- The arguments to bind to the class type
-
help
public <T> String help(Class<T> classType)
Generates a help message for the given class type.- Type Parameters:
T- The type of the class for which a help message should be generated- Parameters:
classType- The class type to generate a help message for- Returns:
- A help message for the given class type
-
extract
protected <T> List<OptionInfo> extract(Class<T> classType)
This method is shared between the bind and help methods.- Type Parameters:
T- The generic type of the class from which metadata is being extracted- Parameters:
classType- The class type from which metadata should be extracted- Returns:
- A list of objects representing the fields which can be bound to
-
setOut
public GetOpt setOut(PrintStream out)
-
setErr
public GetOpt setErr(PrintStream err)
-
setExitMechanism
public GetOpt setExitMechanism(ExitMechanism exitMechanism)
-
getOut
public PrintStream getOut()
-
getErr
public PrintStream getErr()
-
-