And does Dart have a getopt library?
How do I access argv / command line options in Dart?
Asked Answered
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
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;
Ah, Options has a
script
accessor. rosettacode.org/wiki/Program_name#Dart –
Overdress Small additional comment: as of March 2013 the Options class is in dart:io. –
Backbreaking
The
Options
class does no longer exist in dart:io. Use package:args instead. –
Sporades // 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);
}
#!/usr/bin/env dart
main() {
print("Args: " + new Options().arguments);
}
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}" –
Wicketkeeper
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. –
Overdress I use this library for defining and parsing command line args http://pub.dartlang.org/packages/args
© 2022 - 2024 — McMap. All rights reserved.
$0
. – Overdress