Class GetOpt


  • public class GetOpt
    extends Object
    The main entry point for the getopt-databind library.
    Author:
    John Dunlap
    • Constructor Detail

      • GetOpt

        public GetOpt()
    • Method Detail

      • 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 to
        args - 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 as read(Class, String[]) except that it returns a ParseContext instead 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 to
        args - The arguments to bind to the class type
        Returns:
        A ParseContext containing 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 to
        args - 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