10

And does Dart have a getopt library?

mcandre
  • 22,868
  • 20
  • 88
  • 147

5 Answers5

31

Using Options is no longer an option, use:

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

To get executable, use Platform.executable (Platform comes from dart:io)

For parsing arguments passed to main, use this cool package

Phani Rithvij
  • 4,030
  • 3
  • 25
  • 60
Tomas Kulich
  • 14,388
  • 4
  • 30
  • 35
4
// 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);
}
daftspaniel
  • 955
  • 9
  • 26
igor
  • 41
  • 1
4

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;
ustun
  • 6,941
  • 5
  • 44
  • 57
1

I use this library for defining and parsing command line args http://pub.dartlang.org/packages/args

adam-singer
  • 4,489
  • 4
  • 21
  • 25
1
#!/usr/bin/env dart

main() {
    print("Args: " + new Options().arguments);
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
mcandre
  • 22,868
  • 20
  • 88
  • 147
  • Some comments: (1) You do not need to import the core library, (2) the + operator cannot be used as String concatenation, so use "Args: ${new Options().arguments}" – rmuller Sep 04 '12 at 15:33
  • I think for the version of Dart/Mac OS X I'm using, you do actually have to import `dart:core`. Thank you for the Dart tips. Obviously `+` can be used for string concatenation, because it works in this snippet. Perhaps string interpolation is preferred in Dart, but it is not the only option. – mcandre Sep 04 '12 at 19:52