Class OptionsUtils


  • public class OptionsUtils
    extends Object
    • Constructor Summary

      Constructors 
      Constructor Description
      OptionsUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addAutoOption​(org.apache.commons.cli.Options options, String optionName, String longOptionName, String description, String noOptionName, String longNoOptionName, String noDescription)
      Add an option with three possible values: true, false, or null.
      static void addDateOption​(String name, String longOpt, String desc, org.apache.commons.cli.Options options)
      Add a date option with support for fuzzy dates (i.e. one month ago).
      static <T extends Enum<T>>
      void
      addEnumOption​(org.apache.commons.cli.Options options, String optionName, String longOption, String description, Class<? extends Enum<T>> enumClass)  
      static Predicate<org.apache.commons.cli.CommandLine> allOf​(Predicate<org.apache.commons.cli.CommandLine>... optionNames)
      Make sure that all the given predicates are true.
      static Predicate<org.apache.commons.cli.CommandLine> anyOf​(Predicate<org.apache.commons.cli.CommandLine>... optionNames)
      Make sure that at least one of the given predicate is true.
      static Boolean getAutoOptionValue​(org.apache.commons.cli.CommandLine commandLine, String optionName, String noOptionName)  
      static <T extends Enum<T>>
      T
      getEnumOptionValue​(org.apache.commons.cli.CommandLine commandLine, String optionName, Class<T> enumClass)
      Obtain the value of an enumerated option.
      static <T extends Enum<T>>
      T
      getEnumOptionValue​(org.apache.commons.cli.CommandLine commandLine, String optionName, Class<T> enumClass, Predicate<org.apache.commons.cli.CommandLine> predicate)
      Obtain the value of an enumerated option.
      static String getOptionValue​(org.apache.commons.cli.CommandLine commandLine, String optionName, Predicate<org.apache.commons.cli.CommandLine> predicate)
      Obtain the value of an option and if present, make sure that it satisfies a predicate.
      static <T> T getParsedOptionValue​(org.apache.commons.cli.CommandLine commandLine, String optionName, Predicate<org.apache.commons.cli.CommandLine> predicate)  
      static boolean hasOption​(org.apache.commons.cli.CommandLine commandLine, String optionName, Predicate<org.apache.commons.cli.CommandLine> predicate)
      Check if an option is present, and if so make sure that the predicate is satisfied.
      static Predicate<org.apache.commons.cli.CommandLine> noneOf​(Predicate<org.apache.commons.cli.CommandLine>... optionNames)
      Make sure that none of the given predicates are true.
      static Predicate<org.apache.commons.cli.CommandLine> requires​(Predicate<org.apache.commons.cli.CommandLine> predicate)
      Make sure that the given predicate is satisfied.
      static Predicate<org.apache.commons.cli.CommandLine> toBeSet​(String optionName)
      Make sure that the given option is present.
      static Predicate<org.apache.commons.cli.CommandLine> toBeUnset​(String optionName)
      Make sure that the given option is missing.
    • Constructor Detail

      • OptionsUtils

        public OptionsUtils()
    • Method Detail

      • getAutoOptionValue

        @Nullable
        public static Boolean getAutoOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                                 String optionName,
                                                 String noOptionName)
                                          throws org.apache.commons.cli.ParseException
        Throws:
        org.apache.commons.cli.ParseException
      • addEnumOption

        public static <T extends Enum<T>> void addEnumOption​(org.apache.commons.cli.Options options,
                                                             String optionName,
                                                             String longOption,
                                                             String description,
                                                             Class<? extends Enum<T>> enumClass)
      • getEnumOptionValue

        @Nullable
        public static <T extends Enum<T>> T getEnumOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                                               String optionName,
                                                               Class<T> enumClass)
                                                        throws org.apache.commons.cli.ParseException
        Obtain the value of an enumerated option.
        Throws:
        org.apache.commons.cli.ParseException
      • getEnumOptionValue

        @Nullable
        public static <T extends Enum<T>> T getEnumOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                                               String optionName,
                                                               Class<T> enumClass,
                                                               Predicate<org.apache.commons.cli.CommandLine> predicate)
                                                        throws org.apache.commons.cli.ParseException
        Obtain the value of an enumerated option.
        Throws:
        org.apache.commons.cli.ParseException
      • getOptionValue

        @Nullable
        public static String getOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                            String optionName,
                                            Predicate<org.apache.commons.cli.CommandLine> predicate)
                                     throws org.apache.commons.cli.ParseException
        Obtain the value of an option and if present, make sure that it satisfies a predicate.
        Throws:
        org.apache.commons.cli.ParseException
      • getParsedOptionValue

        @Nullable
        public static <T> T getParsedOptionValue​(org.apache.commons.cli.CommandLine commandLine,
                                                 String optionName,
                                                 Predicate<org.apache.commons.cli.CommandLine> predicate)
                                          throws org.apache.commons.cli.ParseException
        Throws:
        org.apache.commons.cli.ParseException
        See Also:
        getOptionValue(CommandLine, String, Predicate)
      • hasOption

        public static boolean hasOption​(org.apache.commons.cli.CommandLine commandLine,
                                        String optionName,
                                        Predicate<org.apache.commons.cli.CommandLine> predicate)
                                 throws org.apache.commons.cli.ParseException
        Check if an option is present, and if so make sure that the predicate is satisfied.

        The predicate can be any Predicate, but using the ones defined in this class will produce more informative error messages.

        Throws:
        org.apache.commons.cli.ParseException
      • requires

        public static Predicate<org.apache.commons.cli.CommandLine> requires​(Predicate<org.apache.commons.cli.CommandLine> predicate)
        Make sure that the given predicate is satisfied.

        This is useful as a top-level predicate as it prepents "requires " to the description.

      • anyOf

        @SafeVarargs
        public static Predicate<org.apache.commons.cli.CommandLine> anyOf​(Predicate<org.apache.commons.cli.CommandLine>... optionNames)
        Make sure that at least one of the given predicate is true.
      • allOf

        @SafeVarargs
        public static Predicate<org.apache.commons.cli.CommandLine> allOf​(Predicate<org.apache.commons.cli.CommandLine>... optionNames)
        Make sure that all the given predicates are true.
      • noneOf

        @SafeVarargs
        public static Predicate<org.apache.commons.cli.CommandLine> noneOf​(Predicate<org.apache.commons.cli.CommandLine>... optionNames)
        Make sure that none of the given predicates are true.
      • toBeSet

        public static Predicate<org.apache.commons.cli.CommandLine> toBeSet​(String optionName)
        Make sure that the given option is present.
      • toBeUnset

        public static Predicate<org.apache.commons.cli.CommandLine> toBeUnset​(String optionName)
        Make sure that the given option is missing.