How to access argv / command line options in Dart?

And does Dart have a getopt library?

+4
source share
5 answers

Using parameters is no longer an option, use:

void main(List<String> args) {
   print(args);
}

To get the executable, use Platform.executable (the platform is taken from dart: io)

To parse the arguments passed to main, use this cool package

+15
source

Edit: this is no longer valid, see accepted answer above.

See Options.

http://api.dartlang.org/dart_io/Options.html

List<String> argv = (new Options()).arguments;
+4
source
// dart 1.0 
import 'dart:io';

void main(List<String> args) {
  String exec = Platform.executable;
  List<String> flags = Platform.executableArguments;
  Uri    script = Platform.script;

  print("exec=$exec");
  print("flags=$flags");
  print("script=$script");

  print("script arguments:");
  for(String arg in args)
    print(arg);
}
+3
#!/usr/bin/env dart

main() {
    print("Args: " + new Options().arguments);
}
+1

I use this library to identify and analyze command line commands http://pub.dartlang.org/packages/args

0
source

All Articles