O'Reilly logo

Dart: Scalable Application Development by Ivo Balbaert, Sergey Akopkokhyants, Davy Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

Script arguments

What if your parameters are in the key:value ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required