6

I have a fairly lengthy command-line program that requires user input of parameters and then processes using those parameters. What I would like to do is split the program into interactive and non-interactive. I attempted to do that, and intended to have the non-interactive program "call" the interactive program and using the results (parameters), process based on those parameters. The non-interactive part of the program displays results on the console as it processes. I have looked at Process.run and Process.start, but apparently they don't function that way. There is another similar question to this that is about 12-months old, so I thought it worthwhile asking again.

1 Answer 1

8

I have looked at Process.run and Process.start, but apparently they don't function that way.

Process.start is what you want here. It can do what you want, but you'll have to become a bit more comfortable with async programming if you aren't already. You'll spawn the process and then asynchronously read and write to the spawned processes stdout and stdin streams.

Your interactive program can do something like this:

// interactive.dart
import 'dart:io';

main() {
  var input = stdin.readLineSync();
  print(input.toUpperCase());
}

It's using stdin to read input from the command line. Then it outputs the processed result using regular print().

The non-interactive script can spawn and drive that using something like:

import 'dart:convert';
import 'dart:io';

main() {
  Process.start("dart", ["interactive.dart"]).then((process) {
    process.stdin.writeln("this is the input");

    UTF8.decoder.fuse(new LineSplitter()).bind(process.stdout).listen((line) {
      print(line);
    });
  });
}

It uses Process.start to spawn the interactive script. It writes to it using process.stdin. To read the resulting output it has to jump through some hoops to convert the raw byte output to strings for each line, but this is the basic idea.

1
  • Thanks, I'll look at that and report back - looks interesting.
    – Brian Oh
    Sep 11, 2013 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.