Class Arguments

java.lang.Object
software.amazon.smithy.cli.Arguments

public final class Arguments extends Object
Command line arguments list to evaluate.

Arguments are parsed on demand. To finalize parsing arguments and force validation of remaining arguments, call finishParsing(). Note that because arguments are parsed on demand and whole set of arguments isn't known before finishParsing() is called, you may need to delay configuring parts of the CLI or commands by adding subscribers via onComplete(BiConsumer). These subscribers are invoked when all arguments have been parsed.

  • Constructor Details

    • Arguments

      public Arguments(String[] args)
  • Method Details

    • addReceiver

      public <T extends ArgumentReceiver> void addReceiver(T receiver)
      Adds an argument receiver to the argument list.
      Parameters:
      receiver - Receiver to add.
    • getReceiver

      public <T extends ArgumentReceiver> T getReceiver(Class<T> type)
      Get a receiver by class.
      Type Parameters:
      T - Type of receiver to get.
      Parameters:
      type - Type of receiver to get.
      Returns:
      Returns the found receiver.
      Throws:
      NullPointerException - if not found.
    • getReceivers

      public Iterable<ArgumentReceiver> getReceivers()
      Get the argument receivers registered with the Arguments list.
      Returns:
      Returns the receivers.
    • onComplete

      public void onComplete(BiConsumer<Arguments,List<String>> consumer)
      Adds a subscriber to the arguments that is invoked when arguments have finished parsing.
      Parameters:
      consumer - Subscriber consumer to add.
    • hasNext

      public boolean hasNext()
      Checks if any arguments are remaining.
      Returns:
      Returns true if there are more arguments.
    • peek

      public String peek()
      Peeks at the next value in the arguments list without shifting it.

      Note that arguments consumed by a ArgumentReceiver are never peeked.

      Returns:
      Returns the next argument or null if no further arguments are present.
    • shift

      public String shift()
      Shifts off the next value in the arguments list or returns null.
      Returns:
      Returns the next value or null.
    • shiftFor

      public String shiftFor(String parameter)
      Expects an argument value for a parameter by name.
      Parameters:
      parameter - Name of the parameter to get the value of.
      Returns:
      Returns the value of the parameter.
      Throws:
      CliError - if the parameter is not present.
    • finishParsing

      public List<String> finishParsing()
      Expects that all remaining arguments are positional, and returns them.

      If an argument is "--", then it's skipped and remaining arguments are considered positional. If any argument is encountered that isn't valid for a registered Receiver, then an error is raised. Otherwise, all remaining arguments are returned in a list.

      Subscribers for different receivers are called when this method is called.

      Returns:
      Returns remaining arguments.
      Throws:
      CliError - if the next argument starts with "--" but isn't "--".