Package Apache.CLI



Class CommandLine

  • All Implemented Interfaces:
    java.io.Serializable

    public class CommandLine
    extends java.lang.Object
    implements java.io.Serializable
    This Class-File has been copied, directly from the Apache Commons Software Foundation. Other than the minor, cosmetic changes (mentioned in the second-list below), this file is perfectly identical to the original that may be found on the Apache Website: apache.org.

    1. The suggested license for using this file may be read here: Apache License
    2. The Source-Code Header for this file may be viewed here: Source-File Header
    3. Partial contents of the 'site/resources/images' directory: Resource Images

    Notes about changes that have been made as a result of the import process:

    • Within all files, the original Package-Name declaration has been changed:
      Original:   package org.apache.commons.cli;
      Updated:   package Apache.CLI;

    • All Classes and Class-Members (Methods, Fields, etc...) that were previously Annotated with the @Deprecated Annotation have been summarily removed.

    • Code Formattings and Styling has been heavily modified to conform to the Standard Java-HTML Indentation & Styling Choices. The code itself remains identical, with only a few squiggly-braces '{' and '}' being removed, for cosmetic reasons (and for lowering "Developer Stress" Levels)..
    Represents list of arguments parsed against a Options descriptor.

    It allows querying of a boolean hasOption(String opt), in addition to retrieving the getOptionValue(String opt) for options requiring arguments.

    Additionally, any left-over or unrecognized arguments, are available for further processing.
    See Also:
    Serialized Form


    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  CommandLine.Builder
      A nested builder class to create CommandLine instance using descriptive methods.
    • Constructor Summary

       
      Protected, Internal Constructor
      Constructor
      protected CommandLine()
      Creates a command line.
    • Method Summary

       
      Retrieve all Post-Processed Options
      Modifier and Type Method
      Option[] getOptions()
      Gets an array of the processed Option's.
       
      Retrieve Post-Processed Option-Information, via an Option-Instance as Input
      Modifier and Type Method
      Properties getOptionProperties​(Option option)
      Gets the map of values associated to the option.
      String getOptionValue​(Option option)
      Gets the first argument, if any, of this option.
      String getOptionValue​(Option option, String defaultValue)
      Gets the first argument, if any, of an option.
      String[] getOptionValues​(Option option)
      Gets the array of values, if any, of an option.
      Object getParsedOptionValue​(Option option)
      Gets a version of this Option converted to a particular type.
      boolean hasOption​(Option opt)
      Tests to see if an option has been set.
       
      Retrieve Post-Processed Option-Information, via the Option Name-as-String as Input
      Modifier and Type Method
      Properties getOptionProperties​(String opt)
      Gets the map of values associated to the option.
      String getOptionValue​(char opt, String defaultValue)
      Gets the argument, if any, of an option.
      String getOptionValue​(String opt)
      Gets the first argument, if any, of this option.
      String getOptionValue​(String opt, String defaultValue)
      Gets the first argument, if any, of an option.
      String[] getOptionValues​(String opt)
      Gets the array of values, if any, of an option.
      Object getParsedOptionValue​(String opt)
      Gets a version of this Option converted to a particular type.
      boolean hasOption​(String opt)
      Tests to see if an option has been set.
       
      Retrieve Post-Processed Option-Information, via Single-Character Option-Name as Input
      Modifier and Type Method
      String getOptionValue​(char opt)
      Gets the first argument, if any, of this option.
      String[] getOptionValues​(char opt)
      Gets the array of values, if any, of an option.
      Object getParsedOptionValue​(char opt)
      Gets a version of this Option converted to a particular type.
      boolean hasOption​(char opt)
      Tests to see if an option has been set.
       
      Retrieve all Unrecognized 'String[] argv' Options
      Modifier and Type Method
      List<String> getArgList()
      Gets any left-over non-recognized options and arguments
      String[] getArgs()
      Gets any left-over non-recognized options and arguments
       
      Methods: interface java.lang.Iterator
      Modifier and Type Method
      Iterator<Option> iterator()
      Returns an iterator over the Option members of CommandLine.
       
      Protected, Internal Methods
      Modifier and Type Method
      protected void addArg​(String arg)
      Adds left-over unrecognized option/argument.
      protected void addOption​(Option opt)
      Adds an option to the command line.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • addArg

        🡅  🡇     🗕  🗗  🗖
        protected void addArg​(java.lang.String arg)
        Adds left-over unrecognized option/argument.
        Parameters:
        arg - the unrecognized option/argument.
        Code:
        Exact Method Body:
         if (arg != null) args.add(arg);
        
      • addOption

        🡅  🡇     🗕  🗗  🗖
        protected void addOption​(Option opt)
        Adds an option to the command line. The values of the option are stored.
        Parameters:
        opt - the processed option.
        Code:
        Exact Method Body:
         if (opt != null) options.add(opt);
        
      • getArgList

        🡅  🡇     🗕  🗗  🗖
        public java.util.List<java.lang.String> getArgList()
        Gets any left-over non-recognized options and arguments
        Returns:
        remaining items passed in but not parsed as a List.
        Code:
        Exact Method Body:
         return args;
        
      • getArgs

        🡅  🡇     🗕  🗗  🗖
        public java.lang.String[] getArgs()
        Gets any left-over non-recognized options and arguments
        Returns:
        remaining items passed in but not parsed as an array.
        Code:
        Exact Method Body:
         return args.toArray(Util.EMPTY_STRING_ARRAY);
        
      • getOptionProperties

        🡅  🡇     🗕  🗗  🗖
        public java.util.Properties getOptionProperties​(Option option)
        Gets the map of values associated to the option. This is convenient for options specifying Java properties like -Dparam1=value1 -Dparam2=value2. The first argument of the option is the key, and the 2nd argument is the value. If the option has only one argument (-Dfoo) it is considered as a boolean flag and the value is "true".
        Parameters:
        option - name of the option.
        Returns:
        The Properties mapped by the option, never null even if the option doesn't exists.
        Code:
        Exact Method Body:
         final Properties props = new Properties();
        
         for (final Option processedOption : options)
         {
             if (processedOption.equals(option))
             {
                 final List<String> values = processedOption.getValuesList();
        
                 if (values.size() >= 2)
        
                     // use the first 2 arguments as the key/value pair
                     props.put(values.get(0), values.get(1));
        
                 else if (values.size() == 1)
        
                     // no explicit value, handle it as a boolean
                     props.put(values.get(0), "true");
             }
         }
        
         return props;
        
      • getOptionProperties

        🡅  🡇     🗕  🗗  🗖
        public java.util.Properties getOptionProperties​(java.lang.String opt)
        Gets the map of values associated to the option. This is convenient for options specifying Java properties like -Dparam1=value1 -Dparam2=value2. The first argument of the option is the key, and the 2nd argument is the value. If the option has only one argument (-Dfoo) it is considered as a boolean flag and the value is "true".
        Parameters:
        opt - name of the option.
        Returns:
        The Properties mapped by the option, never null even if the option doesn't exists.
        Code:
        Exact Method Body:
         final Properties props = new Properties();
        
         for (final Option option : options)
         {
             if (opt.equals(option.getOpt()) || opt.equals(option.getLongOpt()))
             {
                 final List<String> values = option.getValuesList();
        
                 if (values.size() >= 2)
        
                     // use the first 2 arguments as the key/value pair
                     props.put(values.get(0), values.get(1));
        
                 else if (values.size() == 1)
        
                     // no explicit value, handle it as a boolean
                     props.put(values.get(0), "true");
             }
         }
        
         return props;
        
      • getOptionValue

        🡅  🡇     🗕  🗗  🗖
        public java.lang.String getOptionValue​(char opt)
        Gets the first argument, if any, of this option.
        Parameters:
        opt - the character name of the option.
        Returns:
        Value of the argument if option is set, and has an argument, otherwise null.
        Code:
        Exact Method Body:
         return getOptionValue(String.valueOf(opt));
        
      • getOptionValue

        🡅  🡇     🗕  🗗  🗖
        public java.lang.String getOptionValue​(char opt,
                                               java.lang.String defaultValue)
        Gets the argument, if any, of an option.
        Parameters:
        opt - character name of the option
        defaultValue - is the default value to be returned if the option is not specified.
        Returns:
        Value of the argument if option is set, and has an argument, otherwise defaultValue.
        Code:
        Exact Method Body:
         return getOptionValue(String.valueOf(opt), defaultValue);
        
      • getOptionValue

        🡅  🡇     🗕  🗗  🗖
        public java.lang.String getOptionValue​(Option option)
        Gets the first argument, if any, of this option.
        Parameters:
        option - the name of the option.
        Returns:
        Value of the argument if option is set, and has an argument, otherwise null.
        Code:
        Exact Method Body:
         if (option == null) return null;
        
         final String[] values = getOptionValues(option);
        
         return values == null ? null : values[0];
        
      • getOptionValue

        🡅  🡇     🗕  🗗  🗖
        public java.lang.String getOptionValue​(Option option,
                                               java.lang.String defaultValue)
        Gets the first argument, if any, of an option.
        Parameters:
        option - name of the option.
        defaultValue - is the default value to be returned if the option is not specified.
        Returns:
        Value of the argument if option is set, and has an argument, otherwise defaultValue.
        Code:
        Exact Method Body:
         final String answer = getOptionValue(option);
         return answer != null ? answer : defaultValue;
        
      • getOptionValue

        🡅  🡇     🗕  🗗  🗖
        public java.lang.String getOptionValue​(java.lang.String opt)
        Gets the first argument, if any, of this option.
        Parameters:
        opt - the name of the option.
        Returns:
        Value of the argument if option is set, and has an argument, otherwise null.
        Code:
        Exact Method Body:
         return getOptionValue(resolveOption(opt));
        
      • getOptionValue

        🡅  🡇     🗕  🗗  🗖
        public java.lang.String getOptionValue​(java.lang.String opt,
                                               java.lang.String defaultValue)
        Gets the first argument, if any, of an option.
        Parameters:
        opt - name of the option.
        defaultValue - is the default value to be returned if the option is not specified.
        Returns:
        Value of the argument if option is set, and has an argument, otherwise defaultValue.
        Code:
        Exact Method Body:
         return getOptionValue(resolveOption(opt), defaultValue);
        
      • getOptionValues

        🡅  🡇     🗕  🗗  🗖
        public java.lang.String[] getOptionValues​(char opt)
        Gets the array of values, if any, of an option.
        Parameters:
        opt - character name of the option.
        Returns:
        Values of the argument if option is set, and has an argument, otherwise null.
        Code:
        Exact Method Body:
         return getOptionValues(String.valueOf(opt));
        
      • getOptionValues

        🡅  🡇     🗕  🗗  🗖
        public java.lang.String[] getOptionValues​(Option option)
        Gets the array of values, if any, of an option.
        Parameters:
        option - string name of the option.
        Returns:
        Values of the argument if option is set, and has an argument, otherwise null.
        Code:
        Exact Method Body:
         final List<String> values = new ArrayList<>();
        
         for (final Option processedOption : options)
             if (processedOption.equals(option))
                 values.addAll(processedOption.getValuesList());
        
         return values.isEmpty() ? null : values.toArray(EMPTY_STRING_ARRAY);
        
      • getOptionValues

        🡅  🡇     🗕  🗗  🗖
        public java.lang.String[] getOptionValues​(java.lang.String opt)
        Gets the array of values, if any, of an option.
        Parameters:
        opt - string name of the option.
        Returns:
        Values of the argument if option is set, and has an argument, otherwise null.
        Code:
        Exact Method Body:
         return getOptionValues(resolveOption(opt));
        
      • getParsedOptionValue

        🡅  🡇     🗕  🗗  🗖
        public java.lang.Object getParsedOptionValue​(char opt)
                                              throws ParseException
        Gets a version of this Option converted to a particular type.
        Parameters:
        opt - the name of the option.
        Returns:
        the value parsed into a particular object.
        Throws:
        ParseException - if there are problems turning the option value into the desired type
        See Also:
        PatternOptionBuilder
        Code:
        Exact Method Body:
         return getParsedOptionValue(String.valueOf(opt));
        
      • getParsedOptionValue

        🡅  🡇     🗕  🗗  🗖
        public java.lang.Object getParsedOptionValue​(Option option)
                                              throws ParseException
        Gets a version of this Option converted to a particular type.
        Parameters:
        option - the name of the option.
        Returns:
        the value parsed into a particular object.
        Throws:
        ParseException - if there are problems turning the option value into the desired type
        See Also:
        PatternOptionBuilder
        Code:
        Exact Method Body:
         if (option == null) return null;
        
         final String res = getOptionValue(option);
        
         if (res == null) return null;
        
         return TypeHandler.createValue(res, option.getType());
        
      • getParsedOptionValue

        🡅  🡇     🗕  🗗  🗖
        public java.lang.Object getParsedOptionValue​(java.lang.String opt)
                                              throws ParseException
        Gets a version of this Option converted to a particular type.
        Parameters:
        opt - the name of the option.
        Returns:
        the value parsed into a particular object.
        Throws:
        ParseException - if there are problems turning the option value into the desired type
        See Also:
        PatternOptionBuilder
        Code:
        Exact Method Body:
         return getParsedOptionValue(resolveOption(opt));
        
      • hasOption

        🡅  🡇     🗕  🗗  🗖
        public boolean hasOption​(char opt)
        Tests to see if an option has been set.
        Parameters:
        opt - character name of the option.
        Returns:
        true if set, false if not.
        Code:
        Exact Method Body:
         return hasOption(String.valueOf(opt));
        
      • hasOption

        🡅  🡇     🗕  🗗  🗖
        public boolean hasOption​(Option opt)
        Tests to see if an option has been set.
        Parameters:
        opt - the option to check.
        Returns:
        true if set, false if not.
        Code:
        Exact Method Body:
         return options.contains(opt);
        
      • hasOption

        🡅  🡇     🗕  🗗  🗖
        public boolean hasOption​(java.lang.String opt)
        Tests to see if an option has been set.
        Parameters:
        opt - Short name of the option.
        Returns:
        true if set, false if not.
        Code:
        Exact Method Body:
         return hasOption(resolveOption(opt));
        
      • iterator

        🡅     🗕  🗗  🗖
        public java.util.Iterator<Optioniterator()
        Returns an iterator over the Option members of CommandLine.
        Returns:
        an Iterator over the processed Option members of this CommandLine.
        Code:
        Exact Method Body:
         return options.iterator();