Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running an interactive Dart program from another Dart program

Tags:

dart

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.

like image 694
Brian Oh Avatar asked Sep 09 '13 15:09

Brian Oh


People also ask

How do I run the DART program from the command line?

Run the app To run the app from the command line, use the Dart VM by running the dart run command in the app's top directory: $ cd cli $ dart run Hello world: 42!

What is Stdin Dart?

Stdin allows both synchronous and asynchronous reads from the standard input stream. Mixing synchronous and asynchronous reads is undefined.

What is .dart file?

A DART file is a source code file that contains Dart programming language. It is used for building web, server, and mobile applications and IoT devices. Dart programming language an object-oriented language with C-style syntax that supports mixins, abstract classes, optional typing, and interfaces.

How do you play a single dart file?

To run the Dart file on Android Studio you need to add a configuration. We will create a Configuration to run the Dart file based on an available Template. Select: Template > Dart Command Line App > Create configuration.


1 Answers

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.

like image 67
munificent Avatar answered Sep 25 '22 19:09

munificent