Class FileArgument

  • All Implemented Interfaces:
    java.io.Serializable

    @Mutable
    @ThreadSafety(level=NOT_THREADSAFE)
    public final class FileArgument
    extends Argument
    This class defines an argument that is intended to hold values which refer to files on the local filesystem. File arguments must take values, and it is possible to restrict the values to files that exist, or whose parent exists.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      FileArgument​(java.lang.Character shortIdentifier, java.lang.String longIdentifier, boolean isRequired, int maxOccurrences, java.lang.String valuePlaceholder, java.lang.String description)
      Creates a new file argument with the provided information.
      FileArgument​(java.lang.Character shortIdentifier, java.lang.String longIdentifier, boolean isRequired, int maxOccurrences, java.lang.String valuePlaceholder, java.lang.String description, boolean fileMustExist, boolean parentMustExist, boolean mustBeFile, boolean mustBeDirectory)
      Creates a new file argument with the provided information.
      FileArgument​(java.lang.Character shortIdentifier, java.lang.String longIdentifier, boolean isRequired, int maxOccurrences, java.lang.String valuePlaceholder, java.lang.String description, boolean fileMustExist, boolean parentMustExist, boolean mustBeFile, boolean mustBeDirectory, java.util.List<java.io.File> defaultValues)
      Creates a new file argument with the provided information.
      FileArgument​(java.lang.Character shortIdentifier, java.lang.String longIdentifier, java.lang.String description)
      Creates a new file argument with the provided information.
    • Constructor Detail

      • FileArgument

        public FileArgument​(@Nullable
                            java.lang.Character shortIdentifier,
                            @Nullable
                            java.lang.String longIdentifier,
                            @NotNull
                            java.lang.String description)
                     throws ArgumentException
        Creates a new file argument with the provided information. It will not be required, will permit at most one occurrence, will use a default placeholder, will not have any default values, and will not impose any constraints on the kinds of values it can have.
        Parameters:
        shortIdentifier - The short identifier for this argument. It may not be null if the long identifier is null.
        longIdentifier - The long identifier for this argument. It may not be null if the short identifier is null.
        description - A human-readable description for this argument. It must not be null.
        Throws:
        ArgumentException - If there is a problem with the definition of this argument.
      • FileArgument

        public FileArgument​(@Nullable
                            java.lang.Character shortIdentifier,
                            @Nullable
                            java.lang.String longIdentifier,
                            boolean isRequired,
                            int maxOccurrences,
                            @Nullable
                            java.lang.String valuePlaceholder,
                            @NotNull
                            java.lang.String description)
                     throws ArgumentException
        Creates a new file argument with the provided information. There will not be any default values or constraints on the kinds of values it can have.
        Parameters:
        shortIdentifier - The short identifier for this argument. It may not be null if the long identifier is null.
        longIdentifier - The long identifier for this argument. It may not be null if the short identifier is null.
        isRequired - Indicates whether this argument is required to be provided.
        maxOccurrences - The maximum number of times this argument may be provided on the command line. A value less than or equal to zero indicates that it may be present any number of times.
        valuePlaceholder - A placeholder to display in usage information to indicate that a value must be provided. It may be null if a default placeholder should be used.
        description - A human-readable description for this argument. It must not be null.
        Throws:
        ArgumentException - If there is a problem with the definition of this argument.
      • FileArgument

        public FileArgument​(@Nullable
                            java.lang.Character shortIdentifier,
                            @Nullable
                            java.lang.String longIdentifier,
                            boolean isRequired,
                            int maxOccurrences,
                            @Nullable
                            java.lang.String valuePlaceholder,
                            @NotNull
                            java.lang.String description,
                            boolean fileMustExist,
                            boolean parentMustExist,
                            boolean mustBeFile,
                            boolean mustBeDirectory)
                     throws ArgumentException
        Creates a new file argument with the provided information. It will not have any default values.
        Parameters:
        shortIdentifier - The short identifier for this argument. It may not be null if the long identifier is null.
        longIdentifier - The long identifier for this argument. It may not be null if the short identifier is null.
        isRequired - Indicates whether this argument is required to be provided.
        maxOccurrences - The maximum number of times this argument may be provided on the command line. A value less than or equal to zero indicates that it may be present any number of times.
        valuePlaceholder - A placeholder to display in usage information to indicate that a value must be provided. It may be null if a default placeholder should be used.
        description - A human-readable description for this argument. It must not be null.
        fileMustExist - Indicates whether each value must refer to a file that exists.
        parentMustExist - Indicates whether each value must refer to a file whose parent directory exists.
        mustBeFile - Indicates whether each value must refer to a regular file, if it exists.
        mustBeDirectory - Indicates whether each value must refer to a directory, if it exists.
        Throws:
        ArgumentException - If there is a problem with the definition of this argument.
      • FileArgument

        public FileArgument​(@Nullable
                            java.lang.Character shortIdentifier,
                            @Nullable
                            java.lang.String longIdentifier,
                            boolean isRequired,
                            int maxOccurrences,
                            @Nullable
                            java.lang.String valuePlaceholder,
                            @NotNull
                            java.lang.String description,
                            boolean fileMustExist,
                            boolean parentMustExist,
                            boolean mustBeFile,
                            boolean mustBeDirectory,
                            @Nullable
                            java.util.List<java.io.File> defaultValues)
                     throws ArgumentException
        Creates a new file argument with the provided information.
        Parameters:
        shortIdentifier - The short identifier for this argument. It may not be null if the long identifier is null.
        longIdentifier - The long identifier for this argument. It may not be null if the short identifier is null.
        isRequired - Indicates whether this argument is required to be provided.
        maxOccurrences - The maximum number of times this argument may be provided on the command line. A value less than or equal to zero indicates that it may be present any number of times.
        valuePlaceholder - A placeholder to display in usage information to indicate that a value must be provided. It may be null if a default placeholder should be used.
        description - A human-readable description for this argument. It must not be null.
        fileMustExist - Indicates whether each value must refer to a file that exists.
        parentMustExist - Indicates whether each value must refer to a file whose parent directory exists.
        mustBeFile - Indicates whether each value must refer to a regular file, if it exists.
        mustBeDirectory - Indicates whether each value must refer to a directory, if it exists.
        defaultValues - The set of default values to use for this argument if no values were provided.
        Throws:
        ArgumentException - If there is a problem with the definition of this argument.
    • Method Detail

      • fileMustExist

        public boolean fileMustExist()
        Indicates whether each value must refer to a file that exists.
        Returns:
        true if the target files must exist, or false if it is acceptable for values to refer to files that do not exist.
      • parentMustExist

        public boolean parentMustExist()
        Indicates whether each value must refer to a file whose parent directory exists.
        Returns:
        true if the parent directory for target files must exist, or false if it is acceptable for values to refer to files whose parent directories do not exist.
      • mustBeFile

        public boolean mustBeFile()
        Indicates whether each value must refer to a regular file (if it exists).
        Returns:
        true if each value must refer to a regular file (if it exists), or false if it may refer to a directory.
      • mustBeDirectory

        public boolean mustBeDirectory()
        Indicates whether each value must refer to a directory (if it exists).
        Returns:
        true if each value must refer to a directory (if it exists), or false if it may refer to a regular file.
      • getDefaultValues

        @Nullable
        public java.util.List<java.io.File> getDefaultValues()
        Retrieves the list of default values for this argument, which will be used if no values were provided.
        Returns:
        The list of default values for this argument, or null if there are no default values.
      • getRelativeBaseDirectory

        @Nullable
        public java.io.File getRelativeBaseDirectory()
        Retrieves the directory that will serve as the base directory for relative paths, if one has been defined.
        Returns:
        The directory that will serve as the base directory for relative paths, or null if relative paths will be relative to the current working directory.
      • setRelativeBaseDirectory

        public void setRelativeBaseDirectory​(@Nullable
                                             java.io.File relativeBaseDirectory)
        Specifies the directory that will serve as the base directory for relative paths.
        Parameters:
        relativeBaseDirectory - The directory that will serve as the base directory for relative paths. It may be null if relative paths should be relative to the current working directory.
      • addValueValidator

        public void addValueValidator​(@NotNull
                                      ArgumentValueValidator validator)
        Updates this argument to ensure that the provided validator will be invoked for any values provided to this argument. This validator will be invoked after all other validation has been performed for this argument.
        Parameters:
        validator - The argument value validator to be invoked. It must not be null.
      • addValue

        protected void addValue​(@NotNull
                                java.lang.String valueString)
                         throws ArgumentException
        Adds the provided value to the set of values for this argument. This method should only be called by the argument parser.
        Specified by:
        addValue in class Argument
        Parameters:
        valueString - The string representation of the value.
        Throws:
        ArgumentException - If the provided value is not acceptable, if this argument does not accept values, or if this argument already has the maximum allowed number of values.
      • getValue

        @Nullable
        public java.io.File getValue()
        Retrieves the value for this argument, or the default value if none was provided. If there are multiple values, then the first will be returned.
        Returns:
        The value for this argument, or the default value if none was provided, or null if there is no value and no default value.
      • getValues

        @NotNull
        public java.util.List<java.io.File> getValues()
        Retrieves the set of values for this argument.
        Returns:
        The set of values for this argument.
      • getFileInputStream

        @Nullable
        public java.io.InputStream getFileInputStream()
                                               throws java.io.IOException
        Retrieves an input stream that may be used to read the contents of the file specified as the value to this argument. If there are multiple values for this argument, then the file specified as the first value will be used.
        Returns:
        An input stream that may be used to read the data from the file, or null if no values were provided.
        Throws:
        java.io.IOException - If the specified file does not exist or if a problem occurs while obtaining the input stream.
      • getFileLines

        @Nullable
        public java.util.List<java.lang.String> getFileLines()
                                                      throws java.io.IOException
        Reads the contents of the file specified as the value to this argument and retrieves a list of the lines contained in it. If there are multiple values for this argument, then the file specified as the first value will be used.
        Returns:
        A list containing the lines of the target file, or null if no values were provided.
        Throws:
        java.io.IOException - If the specified file does not exist or a problem occurs while reading the contents of the file.
      • getNonBlankFileLines

        @Nullable
        public java.util.List<java.lang.String> getNonBlankFileLines()
                                                              throws java.io.IOException
        Reads the contents of the file specified as the value to this argument and retrieves a list of the non-blank lines contained in it. If there are multiple values for this argument, then the file specified as the first value will be used.
        Returns:
        A list containing the non-blank lines of the target file, or null if no values were provided.
        Throws:
        java.io.IOException - If the specified file does not exist or a problem occurs while reading the contents of the file.
      • getFileBytes

        @Nullable
        public byte[] getFileBytes()
                            throws java.io.IOException
        Reads the contents of the file specified as the value to this argument. If there are multiple values for this argument, then the file specified as the first value will be used.
        Returns:
        A byte array containing the contents of the target file, or null if no values were provided.
        Throws:
        java.io.IOException - If the specified file does not exist or a problem occurs while reading the contents of the file.
      • getValueStringRepresentations

        @NotNull
        public java.util.List<java.lang.String> getValueStringRepresentations​(boolean useDefault)
        Retrieves a list containing the string representations of the values for this argument, if any. The list returned does not necessarily need to include values that will be acceptable to the argument, but it should imply what the values are (e.g., in the case of a boolean argument that doesn't take a value, it may be the string "true" or "false" even if those values are not acceptable to the argument itself).
        Specified by:
        getValueStringRepresentations in class Argument
        Parameters:
        useDefault - Indicates whether to use any configured default value if the argument doesn't have a user-specified value.
        Returns:
        A string representation of the value for this argument, or an empty list if the argument does not have a value.
      • hasDefaultValue

        protected boolean hasDefaultValue()
        Indicates whether this argument has one or more default values that will be used if it is not provided on the command line.
        Specified by:
        hasDefaultValue in class Argument
        Returns:
        true if this argument has one or more default values, or false if not.
      • getDataTypeName

        @NotNull
        public java.lang.String getDataTypeName()
        Retrieves a concise name of the data type with which this argument is associated.
        Specified by:
        getDataTypeName in class Argument
        Returns:
        A concise name of the data type with which this argument is associated.
      • getValueConstraints

        @NotNull
        public java.lang.String getValueConstraints()
        Retrieves a human-readable string with information about any constraints that may be imposed for values of this argument.
        Overrides:
        getValueConstraints in class Argument
        Returns:
        A human-readable string with information about any constraints that may be imposed for values of this argument, or null if there are none.
      • reset

        protected void reset()
        Resets this argument so that it appears in the same form as before it was used to parse arguments. Subclasses that override this method must call super.reset() to ensure that all necessary reset processing is performed.
        Overrides:
        reset in class Argument
      • getCleanCopy

        @NotNull
        public FileArgument getCleanCopy()
        Creates a copy of this argument that is "clean" and appears as if it has not been used in the course of parsing an argument set. The new argument will have all of the same identifiers and constraints as this parser.
        Specified by:
        getCleanCopy in class Argument
        Returns:
        The "clean" copy of this argument.
      • addToCommandLine

        protected void addToCommandLine​(@NotNull
                                        java.util.List<java.lang.String> argStrings)
        Updates the provided list to add any strings that should be included on the command line in order to represent this argument's current state.
        Specified by:
        addToCommandLine in class Argument
        Parameters:
        argStrings - The list to update with the string representation of the command-line arguments.
      • toString

        public void toString​(@NotNull
                             java.lang.StringBuilder buffer)
        Appends a string representation of this argument to the provided buffer.
        Specified by:
        toString in class Argument
        Parameters:
        buffer - The buffer to which the information should be appended.