Package Apache.CLI



Class Options

  • All Implemented Interfaces:
    java.io.Serializable

    public class Options
    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)..
    Main entry-point into the library.

    Options represents a collection of Option objects, which describe the possible options for a command-line.

    It may flexibly parse long and short options, with or without values. Additionally, it may parse only a portion of a commandline, allowing for flexible multi-stage parsing.
    See Also:
    CommandLine, Serialized Form


    • Constructor Summary

      Constructors 
      Constructor Description
      Options()  
    • Method Summary

       
      Create a New Option, and Add it to 'this' Options-Instance
      Modifier and Type Method
      Options addOption​(String opt, boolean hasArg, String description)
      Add an option that only contains a short-name.
      Options addOption​(String opt, String description)
      Add an option that only contains a short name.
      Options addOption​(String opt, String longOpt, boolean hasArg, String description)
      Add an option that contains a short-name and a long-name.
      Options addRequiredOption​(String opt, String longOpt, boolean hasArg, String description)
      Add an option that contains a short-name and a long-name.
       
      Add an Already-Constructed Option, or Group, to 'this' Options-Instance
      Modifier and Type Method
      Options addOption​(Option opt)
      Adds an option instance
      Options addOptionGroup​(OptionGroup group)
      Add the specified option group.
       
      Check (by Name) Whether an Option has been Inserted
      Modifier and Type Method
      boolean hasLongOption​(String opt)
      Returns whether the named Option is a member of this Options.
      boolean hasOption​(String opt)
      Returns whether the named Option is a member of this Options.
      boolean hasShortOption​(String opt)
      Returns whether the named Option is a member of this Options.
       
      Check & Retrieve 'this' Options-Instance Configurations / Insertions
      Modifier and Type Method
      List<String> getMatchingOptions​(String opt)
      Gets the options with a long name starting with the name specified.
      Option getOption​(String opt)
      Gets the Option matching the long or short name specified.
      OptionGroup getOptionGroup​(Option opt)
      Gets the OptionGroup the opt belongs to.
      Collection<Option> getOptions()
      Gets a read-only list of options in this set
      List getRequiredOptions()
      Gets the required options.
       
      Methods: class java.lang.Object
      Modifier and Type Method
      String toString()
      Dump state, suitable for debugging.
      • Methods inherited from class java.lang.Object

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

      • addOption

        🡅  🡇     🗕  🗗  🗖
        public Options addOption​(Option opt)
        Adds an option instance
        Parameters:
        opt - the option that is to be added
        Returns:
        the resulting Options instance
        Code:
        Exact Method Body:
         final String key = opt.getKey();
        
         // add it to the long option list
         if (opt.hasLongOpt()) longOpts.put(opt.getLongOpt(), opt);
        
         // if the option is required add it to the required list
         if (opt.isRequired())
         {
             if (requiredOpts.contains(key)) requiredOpts.remove(requiredOpts.indexOf(key));
             requiredOpts.add(key);
         }
        
         shortOpts.put(key, opt);
        
         return this;
        
      • addOption

        🡅  🡇     🗕  🗗  🗖
        public Options addOption​(java.lang.String opt,
                                 boolean hasArg,
                                 java.lang.String description)
        Add an option that only contains a short-name.

        It may be specified as requiring an argument.
        Parameters:
        opt - Short single-character name of the option.
        hasArg - flag signalling if an argument is required after this option
        description - Self-documenting description
        Returns:
        the resulting Options instance
        Code:
        Exact Method Body:
         addOption(opt, null, hasArg, description);
         return this;
        
      • addOption

        🡅  🡇     🗕  🗗  🗖
        public Options addOption​(java.lang.String opt,
                                 java.lang.String description)
        Add an option that only contains a short name.

        The option does not take an argument.
        Parameters:
        opt - Short single-character name of the option.
        description - Self-documenting description
        Returns:
        the resulting Options instance
        Code:
        Exact Method Body:
         addOption(opt, null, false, description);
         return this;
        
      • addOption

        🡅  🡇     🗕  🗗  🗖
        public Options addOption​(java.lang.String opt,
                                 java.lang.String longOpt,
                                 boolean hasArg,
                                 java.lang.String description)
        Add an option that contains a short-name and a long-name.

        It may be specified as requiring an argument.
        Parameters:
        opt - Short single-character name of the option.
        longOpt - Long multi-character name of the option.
        hasArg - flag signalling if an argument is required after this option
        description - Self-documenting description
        Returns:
        the resulting Options instance
        Code:
        Exact Method Body:
         addOption(new Option(opt, longOpt, hasArg, description));
         return this;
        
      • addOptionGroup

        🡅  🡇     🗕  🗗  🗖
        public Options addOptionGroup​(OptionGroup group)
        Add the specified option group.
        Parameters:
        group - the OptionGroup that is to be added
        Returns:
        the resulting Options instance
        Code:
        Exact Method Body:
         if (group.isRequired()) requiredOpts.add(group);
        
         for (final Option option : group.getOptions())
         {
             // an Option cannot be required if it is in an OptionGroup, either the group is
             // required or nothing is required
        
             option.setRequired(false);
             addOption(option);
        
             optionGroups.put(option.getKey(), group);
         }
        
         return this;
        
      • addRequiredOption

        🡅  🡇     🗕  🗗  🗖
        public Options addRequiredOption​(java.lang.String opt,
                                         java.lang.String longOpt,
                                         boolean hasArg,
                                         java.lang.String description)
        Add an option that contains a short-name and a long-name.

        The added option is set as required. It may be specified as requiring an argument. This method is a shortcut for:

        Example:
         Options option = new Option(opt, longOpt, hasArg, description);
         option.setRequired(true);
         options.add(option);
        
        Parameters:
        opt - Short single-character name of the option.
        longOpt - Long multi-character name of the option.
        hasArg - flag signalling if an argument is required after this option
        description - Self-documenting description
        Returns:
        the resulting Options instance
        Code:
        Exact Method Body:
         final Option option = new Option(opt, longOpt, hasArg, description);
         option.setRequired(true);
         addOption(option);
         return this;
        
      • getMatchingOptions

        🡅  🡇     🗕  🗗  🗖
        public java.util.List<java.lang.String> getMatchingOptions​
                    (java.lang.String opt)
        
        Gets the options with a long name starting with the name specified.
        Parameters:
        opt - the partial name of the option
        Returns:
        the options matching the partial name specified, or an empty list if none matches
        Code:
        Exact Method Body:
         opt = Util.stripLeadingHyphens(opt);
        
         final List<String> matchingOpts = new ArrayList<>();
        
         // for a perfect match return the single option only
         if (longOpts.containsKey(opt)) return Collections.singletonList(opt);
        
         for (final String longOpt : longOpts.keySet())
             if (longOpt.startsWith(opt))
                 matchingOpts.add(longOpt);
        
         return matchingOpts;
        
      • getOption

        🡅  🡇     🗕  🗗  🗖
        public Option getOption​(java.lang.String opt)
        Gets the Option matching the long or short name specified.

        The leading hyphens in the name are ignored (up to 2).
        Parameters:
        opt - short or long name of the Option
        Returns:
        the option represented by opt
        Code:
        Exact Method Body:
         opt = Util.stripLeadingHyphens(opt);
        
         final Option option = shortOpts.get(opt);
        
         return option != null ? option : longOpts.get(opt);
        
      • getOptionGroup

        🡅  🡇     🗕  🗗  🗖
        public OptionGroup getOptionGroup​(Option opt)
        Gets the OptionGroup the opt belongs to.
        Parameters:
        opt - the option whose OptionGroup is being queried.
        Returns:
        the OptionGroup if opt is part of an OptionGroup, otherwise return null
        Code:
        Exact Method Body:
         return optionGroups.get(opt.getKey());
        
      • getOptions

        🡅  🡇     🗕  🗗  🗖
        public java.util.Collection<OptiongetOptions()
        Gets a read-only list of options in this set
        Returns:
        read-only Collection of Option objects in this descriptor
        Code:
        Exact Method Body:
         return Collections.unmodifiableCollection(helpOptions());
        
      • getRequiredOptions

        🡅  🡇     🗕  🗗  🗖
        public java.util.List getRequiredOptions()
        Gets the required options.
        Returns:
        read-only List of required options
        Code:
        Exact Method Body:
         return Collections.unmodifiableList(requiredOpts);
        
      • hasLongOption

        🡅  🡇     🗕  🗗  🗖
        public boolean hasLongOption​(java.lang.String opt)
        Returns whether the named Option is a member of this Options.
        Parameters:
        opt - long name of the Option
        Returns:
        true if the named Option is a member of this Options
        Code:
        Exact Method Body:
         opt = Util.stripLeadingHyphens(opt);
         return longOpts.containsKey(opt);
        
      • hasOption

        🡅  🡇     🗕  🗗  🗖
        public boolean hasOption​(java.lang.String opt)
        Returns whether the named Option is a member of this Options.
        Parameters:
        opt - short or long name of the Option
        Returns:
        true if the named Option is a member of this Options
        Code:
        Exact Method Body:
         opt = Util.stripLeadingHyphens(opt);
         return shortOpts.containsKey(opt) || longOpts.containsKey(opt);
        
      • hasShortOption

        🡅  🡇     🗕  🗗  🗖
        public boolean hasShortOption​(java.lang.String opt)
        Returns whether the named Option is a member of this Options.
        Parameters:
        opt - short name of the Option
        Returns:
        true if the named Option is a member of this Options
        Code:
        Exact Method Body:
         opt = Util.stripLeadingHyphens(opt);
         return shortOpts.containsKey(opt);
        
      • toString

        🡅     🗕  🗗  🗖
        public java.lang.String toString()
        Dump state, suitable for debugging.
        Overrides:
        toString in class java.lang.Object
        Returns:
        Stringified form of this object
        Code:
        Exact Method Body:
         final StringBuilder buf = new StringBuilder();
        
         buf.append("[ Options: [ short ");
         buf.append(shortOpts.toString());
         buf.append(" ] [ long ");
         buf.append(longOpts);
         buf.append(" ]");
        
         return buf.toString();