Class Arguments


  • public class Arguments
    extends Object
    A helper class for parsing command-line arguments. Instance of this class are usually created inside main methods. For example:
     public static void main(String[] args) {
         Arguments arguments = new Arguments(args);
     }
     
    Then, method likes getRequiredString(java.lang.String) or getOptionalString(java.lang.String) can be used. If a parameter is badly formatted or if a required parameter is not presents, then the method illegalArgument(java.lang.Exception) will be invoked with a message that describes the error. The default implementation print the localized error message to standard output out and exits the virtual machine with a call to System.exit(int) with error code 1.
    Since:
    2.0
    Author:
    Martin Desruisseaux (IRD)
    • Field Detail

      • out

        public final PrintWriter out
        Output stream to the console. This output stream may use encoding specified in the "-encoding" argument, if presents.
      • err

        public final PrintWriter err
        Error stream to the console.
      • locale

        public final Locale locale
        The locale. Locale will be fetch from the "-locale" argument, if presents. Otherwise, the default locale will be used.
    • Constructor Detail

      • Arguments

        public Arguments​(String... args)
        Constructs a set of arguments.
        Parameters:
        args - Command line arguments. Arguments "-encoding" and "-locale" will be automatically parsed.
    • Method Detail

      • getOptionalString

        public String getOptionalString​(String name)
        Returns an optional string value from the command line. This method should be called exactly once for each parameter. Second invocation for the same parameter will returns null, unless the same parameter appears many times on the command line.

        Paramater may be instructions like "-encoding cp850" or "-encoding=cp850". Both forms (with or without "=") are accepted. Spaces around the '=' character, if any, are ignored.

        Parameters:
        name - The parameter name (e.g. "-encoding"). Name are case-insensitive.
        Returns:
        The parameter value, of null if there is no parameter given for the specified name.
      • getOptionalInteger

        public Integer getOptionalInteger​(String name)
        Returns an optional integer value from the command line. Numbers are parsed as of the Integer.parseInt(String) method, which means that the parsing is locale-insensitive. Locale insensitive parsing is required in order to use arguments in portable scripts.
        Parameters:
        name - The parameter name. Name are case-insensitive.
        Returns:
        The parameter value, of null if there is no parameter given for the specified name.
      • getRequiredInteger

        public int getRequiredInteger​(String name)
        Returns a required integer value from the command line. Numbers are parsed as of the Integer.parseInt(String) method, which means that the parsing is locale-insensitive. Locale insensitive parsing is required in order to use arguments in portable scripts.
        Parameters:
        name - The parameter name. Name are case-insensitive.
        Returns:
        The parameter value.
      • getOptionalDouble

        public Double getOptionalDouble​(String name)
        Returns an optional floating-point value from the command line. Numbers are parsed as of the Double.parseDouble(String) method, which means that the parsing is locale-insensitive. Locale insensitive parsing is required in order to use arguments in portable scripts.
        Parameters:
        name - The parameter name. Name are case-insensitive.
        Returns:
        The parameter value, of null if there is no parameter given for the specified name.
      • getRequiredDouble

        public double getRequiredDouble​(String name)
        Returns a required floating-point value from the command line. Numbers are parsed as of the Double.parseDouble(String) method, which means that the parsing is locale-insensitive. Locale insensitive parsing is required in order to use arguments in portable scripts.
        Parameters:
        name - The parameter name. Name are case-insensitive.
        Returns:
        The parameter value.
      • getOptionalBoolean

        public Boolean getOptionalBoolean​(String name)
        Returns an optional boolean value from the command line. The value, if defined, must be "true" or "false".
        Parameters:
        name - The parameter name. Name are case-insensitive.
        Returns:
        The parameter value, of null if there is no parameter given for the specified name.
      • getRequiredBoolean

        public boolean getRequiredBoolean​(String name)
        Returns a required boolean value from the command line. The value must be "true" or "false".
        Parameters:
        name - The parameter name. Name are case-insensitive.
        Returns:
        The parameter value.
      • getFlag

        public boolean getFlag​(String name)
        Returns true if the specified flag is set on the command line. This method should be called exactly once for each flag. Second invocation for the same flag will returns false (unless the same flag appears many times on the command line).
        Parameters:
        name - The flag name.
        Returns:
        true if this flag appears on the command line, or false otherwise.
      • getReader

        public static Reader getReader​(InputStream in)
        Gets a reader for the specified input stream.
        Parameters:
        in - The input stream to wrap.
        Returns:
        A Reader wrapping the specified input stream.
      • getWriter

        public static Writer getWriter​(OutputStream out)
        Gets a writer for the specified output stream.
        Parameters:
        out - The output stream to wrap.
        Returns:
        A Writer wrapping the specified output stream.
      • getPrintWriter

        public static PrintWriter getPrintWriter​(PrintStream out)
        Gets a print writer for the specified print stream.
        Parameters:
        out - The print stream to wrap.
        Returns:
        A PrintWriter wrapping the specified print stream.
      • getRemainingArguments

        public String[] getRemainingArguments​(int max)
        Returns the list of unprocessed arguments. If the number of remaining arguments is greater than the specified maximum, then this method invokes illegalArgument(java.lang.Exception).
        Parameters:
        max - Maximum remaining arguments autorized.
        Returns:
        An array of remaining arguments. Will never be longer than max.
      • getRemainingArguments

        public String[] getRemainingArguments​(int max,
                                              char forbiddenPrefix)
        Returns the list of unprocessed arguments, which should not begin by the specified prefix. This method invokes getRemainingArguments(max) and verifies that none of the remaining arguments start with forbiddenPrefix. The forbidden prefix is usually '-', the character used for options as in " -locale", etc.
        Parameters:
        max - Maximum remaining arguments autorized.
        forbiddenPrefix - The forbidden prefix, usually '-'.
        Returns:
        An array of remaining arguments. Will never be longer than max.
        Since:
        2.4
      • printSummary

        public void printSummary​(Exception exception)
        Prints a summary of the specified exception, without stack trace. This method is invoked when a non-fatal (and somewhat expected) error occured, for example FileNotFoundException when the file were specified in argument.
        Parameters:
        exception - An exception with a message describing the user's error.
        Since:
        2.3
      • illegalArgument

        protected void illegalArgument​(Exception exception)
        Invoked when an the user has specified an illegal parameter. The default implementation prints the localized error message to the standard output out, and then exit the virtual machine. User may override this method if they want a different behavior.

        This method is not invoked when an anormal error occured (for example an unexpected NullPointerException in some of developper's module). If such an error occurs, the normal exception mechanism will be used.

        Parameters:
        exception - An exception with a message describing the user's error.