ApplicationArguments class
The arguments the program was started with, split into --options
and plain arguments.
main(args) receives the arguments as a plain list. A bean that needs
them later, such as an import job that takes a file name and a
--dry-run flag, would otherwise have to parse that list itself.
SpringApplication parses it once and registers the result as a bean,
so any bean can inject it, and every ApplicationRunner receives it.
Options are --name=value or a bare --name; an option may be given
more than once. Everything else (-v included) is a plain argument,
and so is everything after a lone --. Options are also properties:
--server.port=8081 sets server.port, so most settings are read with
@Value or @ConfigurationProperties rather than from here.
// dart run bin/main.dart --dry-run books.csv
@Component()
class Importer implements ApplicationRunner {
Importer(this.books);
final BookStore books;
@override
Future<void> run(ApplicationArguments args) async {
final dryRun = args.containsOption('dry-run');
for (final file in args.nonOptionArgs) {
await books.importCsv(file, save: !dryRun);
}
}
}
Spring (Java):
@Component
class Importer implements ApplicationRunner {
public void run(ApplicationArguments args) {
boolean dryRun = args.containsOption("dry-run");
for (String file : args.getNonOptionArgs()) { ... }
}
}
Constructors
-
ApplicationArguments(List<
String> sourceArgs) -
Parses
sourceArgs.SpringApplicationdoes this for every run; create one yourself only in a test of a runner.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
-
nonOptionArgs
→ List<
String> -
The plain arguments, in order: those that do not start with
--, and everything after a lone--(the--itself is left out).no setter -
optionNames
→ Set<
String> -
The names of the options given (
dry-runfor--dry-run), each once.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
sourceArgs
→ List<
String> -
The arguments exactly as
mainreceived them, in order. A CommandLineRunner receives this list.final
Methods
-
containsOption(
String name) → bool -
Whether the option
namewas given, with or without a value. Pass the name without the dashes. -
getOptionValues(
String name) → List< String> ? -
The values of the option
name, in the order given:['a', 'b']for--tag=a --tag=b. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited