Parsing command-line arguments
A server app that runs a batch job often takes parameter values from the command line. How can we get these values in our program?
How to do it...
The obvious way to parse command-line arguments is as follows (see command_line_arguments.dart
):
void main(List<String> args) { print("script arguments:"); for(String arg in args) print(arg); }
Now, the command dart command_line_arguments.dart param1 param2 param3
gives you the following output:
script arguments:
param1
param2
param3
However, you can also test this from within Dart Editor, open the menu Run, and select Manage Launches (Ctrl + Shift + M). Fill in the parameters in the Script arguments window:
What if your parameters are in the key:value
form, as shown in the following code?
par1:value1 par2:value2 par3:value3
In this case, use the following code snippet:
for(String arg in args) { List<String> par = arg.split(':'); var key = par[0]; var...