Class CommandLineParser


  • public class CommandLineParser
    extends Object
    Simple command line parsing utility.

    The parser can only handle parameters that take 0 or 1 arguments. That is, you can parse command lines like

        doit -f -i file1 -o file2 --dir file3 /h file4 file5 file6
     

    The syntax of parameters is left to the user, no common prefix is assumed or enforced. Parameter names can be arbitrarily long. You can define aliases for parameters: -h, /H and --help can all mean the same parameter if so configured.

    • Constructor Detail

      • CommandLineParser

        public CommandLineParser()
        Create a new command line parser.
    • Method Detail

      • addParameter

        public boolean addParameter​(String paramName,
                                    boolean hasArg)
        Add a new parameter name.
        Parameters:
        paramName - The name of the parameter.
        hasArg - If the command line argument following this parameter should be interpreted as an argument to the parameter.
        Returns:
        false iff paramName already exists.
      • addParameter

        public boolean addParameter​(String paramName)
        Add a new switch. This is the same as calling addParameter(name, false).
        Parameters:
        paramName - The name of the parameter.
        Returns:
        false iff paramName already exists.
      • addAlias

        public boolean addAlias​(String param,
                                String alias)
        Add an alias for an already defined parameter.
        Parameters:
        param - A known parameter.
        alias - The alias.
        Returns:
        false iff the parameter does not exist or the alias is already known.
      • parseCmdLine

        public void parseCmdLine​(String[] args)
                          throws Exception
        Parse the command line.
        Parameters:
        args - The command line args as passed to main().
        Throws:
        Exception - If a parameter that requires an argument does not have one (i.e., is the last parameter in the list).
      • getRestArgs

        public String[] getRestArgs()
        Get the rest of the args, i.e., args that follow the last know parameter.
        Returns:
        The tail end of the args list, usually file name arguments.
      • isKnownParameter

        public boolean isKnownParameter​(String paramName)
        Check if the given parameter name is known to this parser.
        Parameters:
        paramName - The name of the parameter.
        Returns:
        true iff the name was added with addParameter() or addAlias().
      • isInArgsList

        public boolean isInArgsList​(String paramName)
        Check if the parameter was used on the command line.
        Parameters:
        paramName - The name of the parameter.
        Returns:
        true iff the name is known and was used as a command line argument.
      • getParamArgument

        public String getParamArgument​(String paramName)
        Get the argument to a parameter, if it exists.
        Parameters:
        paramName - The name of the parameter.
        Returns:
        The argument to the parameter if the parameter was used and takes an argument; null, else.