Class Parser


  • public final class Parser
    extends java.lang.Object
    Defines the CLI argument parser of a Command.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Parser.Argument
      A command line argument.
      static class  Parser.Arity
      Defines the arity of an argument.
      static class  Parser.Builder
      Builds an Parser.
    • Method Summary

      Modifier and Type Method Description
      static Parser.Builder builder()  
      java.util.List<Parser.Argument> getArgs()  
      java.util.Optional<java.lang.String> getPositionalHelp()  
      java.util.Optional<java.lang.String> getPositionalName()  
      Arguments parse​(java.lang.String[] args)
      Parses command line arguments in an Arguments object.
      Arguments parse​(java.lang.String[] args, int offset)
      Parses command line arguments in an Arguments object.
      • Methods inherited from class java.lang.Object

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

      • getPositionalName

        public java.util.Optional<java.lang.String> getPositionalName()
      • getPositionalHelp

        public java.util.Optional<java.lang.String> getPositionalHelp()
      • parse

        public Arguments parse​(java.lang.String[] args)
        Parses command line arguments in an Arguments object.

        All arguments after the first argument that does not start with "-" or after "--" are treated as positional options. Arguments that come before this must be present in the argument spec returned by the command. Any encountered unknown argument will throw an exception. Arguments that expect a value and do not find one will throw an exception.

        Parameters:
        args - Arguments to parse.
        Returns:
        Returns the parsed arguments.
        Throws:
        CliError - if the arguments are invalid.
      • parse

        public Arguments parse​(java.lang.String[] args,
                               int offset)
        Parses command line arguments in an Arguments object.
        Parameters:
        args - Arguments to parse.
        offset - Number of arguments to skip before parsing.
        Returns:
        Returns the parsed arguments.
        Throws:
        CliError - if the arguments are invalid.
        See Also:
        parse(String[])