-3

In C# langauge exists global system variable for this purpose.

Environment.CommandLine

This property provides access to the program name and any arguments specified on the command line when the current process was started.

Dart is asynchronous langauge. It allows self starting processes.

void main() {
  task("foo", callback1);
  task("baz", callback2);
}

Package tasks.

void task(String name, action()) {
  schedule();
  addTask(name. action);
}

void schedule() {
  // Here we start timer if it not started.
  // This timer will get cmd line arguments
  // And executes task specified in cmd line arguments 
}

P.S.

An official answer from Dart Team: "Not planned".

I cannot understand: "Why this is possible in other platforms through their libraries but in Dart platform this is not possible?".

Why only through "main" parameters which even not ensures that other isolates not substitute these arguments on the surrogate parameters that are not a real OS process command line arguments)?

Here examples:

Go language

func main() {
    fmt.Println(len(os.Args), os.Args)
}

Rust language

fn main() {
  let args = os::args();
  println!("The first argument is {}", args[1]);
}

C# language

class Sample {
  public static void Main() {
    Console.WriteLine();
    String[] arguments = Environment.GetCommandLineArgs();
    Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
  }
}

Ruby language

ARGV.each do|a|
  puts "Argument: #{a}"
end

Python language

import sys

print(sys.argv)

PHP language

foreach($argv as $value)
{
  echo "$value\n";
}

Node.js language

process.argv.forEach(function (val, index, array) {
  console.log(index + ': ' + val);
});

P.S.

Again, I am convinced that that Dart platform is not like everyone else.

This is only my opinion. It does not change anything.

Thanks, Günter Zöchbauer, for your concern, but do not need to edit this.

mezoni
  • 10,684
  • 4
  • 32
  • 54
  • 2
    pass arguments to foo or save args pointer and arglen into some global variable. – Mohit Jain Apr 22 '14 at 13:52
  • You may find [this](http://stackoverflow.com/questions/9279541/how-do-i-access-argv-command-line-options-in-dart) page interesting. It talks about new Options().arguments. – Mohit Jain Apr 22 '14 at 14:01
  • 1
    @Mohit Jain it also says that Options is not available anymore. – Fox32 Apr 22 '14 at 15:26
  • Related issue in the bug tracker: https://code.google.com/p/dart/issues/detail?id=18369 – mezoni Apr 22 '14 at 16:25

1 Answers1

6

If you want to use command-line arguments outside main you have to pass it around. If you want, you can use a global for that.

This behavior is similar to Java, C and C++ (which you forgot to mention).

One big advantage of this approach is, that it is now easy to launch other programs by simply importing them (as a library) and invoking their main. It also makes argument handling more consistent with respect to isolates.

Florian Loitsch
  • 7,698
  • 25
  • 30