And does Dart have a getopt library?
Asked
Active
Viewed 7,193 times
5 Answers
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
-
I see that Dart omits the script name from the arguments. Do you know how to retrieve it? Ruby and Perl do it with `$0`. – mcandre Feb 14 '12 at 16:10
-
Ah, Options has a `script` accessor. http://rosettacode.org/wiki/Program_name#Dart – mcandre Feb 14 '12 at 16:22
-
Small additional comment: as of March 2013 the Options class is in dart:io. – Florian Loitsch Mar 22 '13 at 09:25
-
The `Options` class does no longer exist in dart:io. Use package:args instead. – Steven Roose Nov 14 '14 at 11:42
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