Class Arguments
- java.lang.Object
-
- software.amazon.smithy.cli.Arguments
-
public final class Arguments extends java.lang.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 beforefinishParsing()
is called, you may need to delay configuring parts of the CLI or commands by adding subscribers viaonComplete(BiConsumer)
. These subscribers are invoked when all arguments have been parsed.
-
-
Constructor Summary
Constructors Constructor Description Arguments(java.lang.String[] args)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends ArgumentReceiver>
voidaddReceiver(T receiver)
Adds an argument receiver to the argument list.java.util.List<java.lang.String>
finishParsing()
Expects that all remaining arguments are positional, and returns them.<T extends ArgumentReceiver>
TgetReceiver(java.lang.Class<T> type)
Get a receiver by class.java.lang.Iterable<ArgumentReceiver>
getReceivers()
Get the argument receivers registered with the Arguments list.boolean
hasNext()
Checks if any arguments are remaining.void
onComplete(java.util.function.BiConsumer<Arguments,java.util.List<java.lang.String>> consumer)
Adds a subscriber to the arguments that is invoked when arguments have finished parsing.java.lang.String
peek()
Peeks at the next value in the arguments list without shifting it.java.lang.String
shift()
Shifts off the next value in the arguments list or returns null.java.lang.String
shiftFor(java.lang.String parameter)
Expects an argument value for a parameter by name.
-
-
-
Method Detail
-
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(java.lang.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:
java.lang.NullPointerException
- if not found.
-
getReceivers
public java.lang.Iterable<ArgumentReceiver> getReceivers()
Get the argument receivers registered with the Arguments list.- Returns:
- Returns the receivers.
-
onComplete
public void onComplete(java.util.function.BiConsumer<Arguments,java.util.List<java.lang.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 java.lang.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 java.lang.String shift()
Shifts off the next value in the arguments list or returns null.- Returns:
- Returns the next value or null.
-
shiftFor
public java.lang.String shiftFor(java.lang.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 java.util.List<java.lang.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 "--".
-
-