Package Apache.CLI



Class PatternOptionBuilder


  • public class PatternOptionBuilder
    extends java.lang.Object
    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)..
    Allows Options to be created from a single String. The pattern contains various single character flags and via an optional punctuation character, their expected type.

    Here is an Overview of PatternOptionBuilder patterns

    a -a flag
    b@ -b [class name]
    c> -c [file name]
    d+ -d [class name] (creates object via empty constructor)
    e% -e [number] (creates Double/Long instance depending on existing of a '.')
    f/ -f [URL]
    g: -g [string]

    For example, the following allows command line flags of '-v -p string-value -f /dir/file'.

    The exclamation mark precede a mandatory option.


     Options options = PatternOptionBuilder.parsePattern("vp:!f/");
    


    Apache Commons To-Do:
    TO-DO These need to break out to OptionType and also to be pluggable.


    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Object getValueClass​(char ch)
      Retrieve the class that ch represents.
      static boolean isValueCode​(char ch)
      Returns whether ch is a value code, i.e.
      static Options parsePattern​(String pattern)
      Returns the Options instance represented by pattern.
      • Methods inherited from class java.lang.Object

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

      • getValueClass

        🡅  🡇     🗕  🗗  🗖
        public static java.lang.Object getValueClass​(char ch)
        Retrieve the class that ch represents.
        Parameters:
        ch - the specified character
        Returns:
        The class that ch represents
        Code:
        Exact Method Body:
         switch (ch)
         {
             case '@': return PatternOptionBuilder.OBJECT_VALUE;
             case ':': return PatternOptionBuilder.STRING_VALUE;
             case '%': return PatternOptionBuilder.NUMBER_VALUE;
             case '+': return PatternOptionBuilder.CLASS_VALUE;
             case '#': return PatternOptionBuilder.DATE_VALUE;
             case '<': return PatternOptionBuilder.EXISTING_FILE_VALUE;
             case '>': return PatternOptionBuilder.FILE_VALUE;
             case '*': return PatternOptionBuilder.FILES_VALUE;
             case '/': return PatternOptionBuilder.URL_VALUE;
         }
        
         return null;
        
      • isValueCode

        🡅  🡇     🗕  🗗  🗖
        public static boolean isValueCode​(char ch)
        Returns whether ch is a value code, i.e. whether it represents a class in a pattern.
        Parameters:
        ch - the specified character
        Returns:
        true if ch is a value code, otherwise false.
        Code:
        Exact Method Body:
         return ch == '@' || ch == ':' || ch == '%' || ch == '+' || ch == '#' || ch == '<' ||
             ch == '>' || ch == '*' || ch == '/' || ch == '!';
        
      • parsePattern

        🡅     🗕  🗗  🗖
        public static Options parsePattern​(java.lang.String pattern)
        Returns the Options instance represented by pattern.
        Parameters:
        pattern - the pattern string
        Returns:
        The Options instance
        Code:
        Exact Method Body:
         char        opt         = ' ';
         boolean     required    = false;
         Class<?>    type        = null;
        
         final Options options = new Options();
        
         for (int i = 0; i < pattern.length(); i++)
         {
             final char ch = pattern.charAt(i);
        
             // a value code comes after an option and specifies
             // details about it
             if (!isValueCode(ch))
             {
                 if (opt != ' ')
                 {
                     final Option option = Option
                         .builder(String.valueOf(opt))
                         .hasArg(type != null)
                         .required(required)
                         .type(type)
                         .build();
        
                     // we have a previous one to deal with
                     options.addOption(option);
        
                     required    = false;
                     type        = null;
                 }
        
                 opt = ch;
             }
        
             else if (ch == '!') required    = true;
             else                type        = (Class<?>) getValueClass(ch);
         }
        
         if (opt != ' ')
         {
             final Option option = Option
                 .builder(String.valueOf(opt))
                 .hasArg(type != null)
                 .required(required)
                 .type(type)
                 .build();
        
             // we have a final one to deal with
             options.addOption(option);
         }
        
         return options;