Write command-line apps

What's the point?

  • Command-line applications need to do input and output.
  • The dart:io library provides I/O functionality.
  • The args package helps define and parse command-line arguments.
  • A Future object represents a value that will be available at some time in the future.
  • Streams provide a series of asynchronous data events.
  • Most input and output requires the use of streams.

Note: This tutorial uses the async and await language features, which rely on the Future and Stream classes for asynchronous support. To learn more about these features, see the asynchronous programming codelab and the streams tutorial.

This tutorial teaches you how to build command-line appsand shows you a few small command-line applications.These programs use resources that most command-line applications need,including the standard output, error, and input streams,command-line arguments, files and directories, and more.

Running an app with the standalone Dart VM

To run a command-line app, you need the Dart VM (dart),which comes when you install the Dart SDK.

Important: The location of the SDK installation directory (we’ll call it <sdk-install-dir>) depends on your platform and how you installed the SDK. You can find dart in <sdk-install-dir>/bin. By putting this directory in your PATH you can refer to the dart command and other commands, such as dartanalyzer, by name.

Let’s run a small program.

  • Create a file called helloworld.dart that contains this code:
  1. void main() {
  2. print('Hello, World!');
  3. }
  • In the directory that contains the file you just created, run the program:
  1. $ dart helloworld.dart
  2. Hello, World!

The Dart VM supports many options.Use dart —help to see commonly used options.Use dart —verbose to see all options.

Overview of the dcat app code

This tutorial covers the details of a small sample app called dcat, whichdisplays the contents of any files listed on the command line. This app usesvarious classes, functions, and properties available to command-line apps. For abrief description of key app features, click the highlighted code below.

  1. import 'dart:convert';
  2. import 'dart:io';
  3.  
  4. import 'package:args/args.dart';
  5.  
  6. const lineNumber = 'line-number';
  7.  
  8. ArgResults argResults;
  9.  
  10. void main(List<String> arguments) {
  11. exitCode = 0; // presume success
  12. final parser = ArgParser()
  13. ..addFlag(lineNumber, negatable: false, abbr: 'n');
  14.  
  15. argResults = parser.parse(arguments);
  16. final paths = argResults.rest;
  17.  
  18. dcat(paths, argResults[lineNumber] as bool);
  19. }
  20.  
  21. Future dcat(List<String> paths, bool showLineNumbers) async {
  22. if (paths.isEmpty) {
  23. // No files provided as arguments. Read from stdin and print each line.
  24. await stdin.pipe(stdout);
  25. } else {
  26. for (var path in paths) {
  27. var lineNumber = 1;
  28. final lines = utf8.decoder
  29. .bind(File(path).openRead())
  30. .transform(const LineSplitter());
  31. try {
  32. await for (var line in lines) {
  33. if (showLineNumbers) {
  34. stdout.write('${lineNumber++} ');
  35. }
  36. stdout.writeln(line);
  37. }
  38. } catch (_) {
  39. await _handleError(path);
  40. }
  41. }
  42. }
  43. }
  44.  
  45. Future _handleError(String path) async {
  46. if (await FileSystemEntity.isDirectory(path)) {
  47. stderr.writeln('error: $path is a directory');
  48. } else {
  49. exitCode = 2;
  50. }
  51. }

Getting dependencies

You might notice that dcat depends on a package named args.To get the args package, use thepub package manager.

A real app has tests, license files, and so on, in a file hierarchysuch as the one in the Dart command-line app template.But for this first app, let’s do the minimum necessary to get the code to run:

  • Create a directory named dcat, and change to that directory.
  • Inside dcat, create a file named dcat.dartand copy the preceding code into it.
  • Inside dcat, create a file named pubspec.yamlwith the following code:
  1. name: dcat
  2. environment:
  3. sdk: '>=2.6.0 <3.0.0'
  4. dependencies:
  5. args: ^1.5.0
  • Still in the dcat directory, run pub get to get the args package:
  1. $ pub get
  2. Resolving dependencies...
  3. + args 1.5.2
  4. Changed 1 dependency!

Note: To learn more about using packages and organizing your code, see the package documentation and layout conventions.

Running dcat

Once you have your app’s dependencies,you can run the app from the command line over any text file,like pubspec.yaml or quote.txt(downloadable file):

  1. $ dart dcat.dart -n quote.txt
  2. 1 Be yourself. Everyone else is taken. -Oscar Wilde
  3. 2 Don't cry because it's over, smile because it happened. -Dr. Seuss
  4. 3 You only live once, but if you do it right, once is enough. -Mae West
  5. ...

This command displays each line of the specified file. Because the -n argumentis present, a line number is displayed before each line.

Parsing command-line arguments

The args package providesparser support for transforming command-line argumentsinto a set of options, flags, and additional values.Import the package’sargs libraryas follows:

  1. import 'package:args/args.dart';

The args library contains these classes, among others:

ClassDescription
ArgParserA command-line argument parser.
ArgResultsThe result of parsing command-line arguments using ArgParser.

Here is the dcat code that uses these classes to parse and store command-linearguments:

  1. ArgResults argResults;
  2.  
  3. void main(List<String> arguments) {
  4. exitCode = 0; // presume success
  5. final parser = ArgParser()
  6. ..addFlag(lineNumber, negatable: false, abbr: 'n');
  7.  
  8.  
  9. argResults = parser.parse(arguments);
  10. final paths = argResults.rest;
  11.  
  12. dcat(paths, argResults[lineNumber] as bool);
  13. }

The runtime passes command-line arguments to the app’s main() function as alist of strings. The ArgParser is configured to parse the -n flag. Theresult of parsing command-line arguments is stored in argResults.

The following diagram shows how the dcat command line used aboveis parsed into an ArgResults object.

Run dcat from the command-line

You can access flags and options by name,treating an ArgResults like a Map.You can access other values using the rest property.

The API referencefor the args library provides detailed information to help you usethe ArgParser and ArgResults classes.

Reading and writing with stdin, stdout, and stderr

Like other languages,Dart has standard output, standard error, and standard input streams.The standard I/O streams are defined at the top level of the dart:io library:

StreamDescription
stdoutThe standard output
stderrThe standard error
stdinThe standard input

Import the dart:io library as follows:

  1. import 'dart:io';

Note: Web apps (apps that depend on dart:html) can’t use the dart:io library.

stdout

Here’s the code from the dcat program that writes the line number tothe stdout (if the -n flag is set) followed by the line from the file.

  1. if (showLineNumbers) {
  2. stdout.write('${lineNumber++} ');
  3. }
  4. stdout.writeln(line);

The write() and writeln() methods take an object of any type,convert it to a string, and print it. The writeln() methodalso prints a newline character.dcat uses the write() method to print the line number so theline number and the text appear on the same line.

You can also use the writeAll() method to print a list of objects,or use addStream() to asynchronously print all of the elements from a stream.

stdout provides more functionality than the print() function.For example, you can display the contents of a stream with stdout.However, you must use print() instead of stdoutfor programs that are converted to and run in JavaScript.

stderr

Use stderr to write error messages to the console.The standard error stream has the same methods as stdout,and you use it in the same way.Although both stdout and stderr print to the console,their output is separateand can be redirected or piped at the command lineor programmatically to different destinations.

This code from dcat prints an error message if the usertries to list a directory.

  1. if (await FileSystemEntity.isDirectory(path)) {
  2. stderr.writeln('error: $path is a directory');
  3. } else {
  4. exitCode = 2;
  5. }

stdin

The standard input stream typicallyreads data synchronously from the keyboard,although it can read asynchronouslyand it can get input piped in from the standardoutput of another program.

Here’s a small program that reads a single line from stdin:

  1. import 'dart:io';
  2. void main() {
  3. stdout.writeln('Type something');
  4. String input = stdin.readLineSync();
  5. stdout.writeln('You typed: $input');
  6. }

The readLineSync() method reads text from the standard input stream,blocking until the user types in text and presses return.This little program prints out the typed text.

In the dcat program,if the user does not provide a filename on the command line,the program instead reads from stdinusing the pipe() method.Because pipe() is asynchronous(returning a future, even though this code doesn’t use that return value),the code that calls it uses await.

  1. await stdin.pipe(stdout);

In this case,the user types in lines of text and the program copies them to stdout.The user signals the end of input by pressing Control+D.

  1. $ dart dcat.dart
  2. The quick brown fox jumps over the lazy dog.
  3. The quick brown fox jumps over the lazy dog.

Getting info about a file

TheFileSystemEntityclass in the dart:io library provides properties and static methodsthat help you inspect and manipulate the file system.

For example, if you have a path,you can determine whether the path is a file, a directory, a link, or not foundby using the type() method from the FileSystemEntity class.Because the type() method accesses the file system,it performs the check asynchronously.

The following code from the dcat example uses FileSystemEntityto determine if the path provided on the command line is a directory.The future returns a boolean that indicates if the path is a directory or not.Because the check is asynchronous, the code calls isDirectory()using await.

  1. if (await FileSystemEntity.isDirectory(path)) {
  2. stderr.writeln('error: $path is a directory');
  3. } else {
  4. exitCode = 2;
  5. }

Other interesting methods in the FileSystemEntity classinclude isFile(), exists(), stat(), delete(),and rename(), all of which also use a future to return a value.

FileSystemEntity is the superclass for the File, Directory, and Link classes.

Reading a file

dcat opens each file listed on the command linewith the openRead() method, which returns a stream.The await for block waits for the file to be readasynchronously. The data prints to stdout when itbecomes available on the stream.

  1. for (var path in paths) {
  2. var lineNumber = 1;
  3. final lines = utf8.decoder
  4. .bind(File(path).openRead())
  5. .transform(const LineSplitter());
  6. try {
  7. await for (var line in lines) {
  8. if (showLineNumbers) {
  9. stdout.write('${lineNumber++} ');
  10. }
  11. stdout.writeln(line);
  12. }
  13. } catch (_) {
  14. await _handleError(path);
  15. }
  16. }

The following shows the rest of the code, which uses two decoders thattransform the data before making it available in the await for block.The UTF8 decoder converts the data into Dart strings.LineSplitter splits the data at newlines.

  1. for (var path in paths) {
  2. var lineNumber = 1;
  3. final lines = utf8.decoder
  4. .bind(File(path).openRead())
  5. .transform(const LineSplitter());
  6. try {
  7. await for (var line in lines) {
  8. if (showLineNumbers) {
  9. stdout.write('${lineNumber++} ');
  10. }
  11. stdout.writeln(line);
  12. }
  13. } catch (_) {
  14. await _handleError(path);
  15. }
  16. }

The dart:convert library contains these and other data converters,including one for JSON.To use these converters you need to import the dart:convert library:

  1. import 'dart:convert';

Writing a file

The easiest way to write text to a file is to create aFileobject and use the writeAsString() method:

  1. final quotes = File('quotes.txt');
  2. const stronger = 'That which does not kill us makes us stronger. -Nietzsche';
  3. await quotes.writeAsString(stronger, mode: FileMode.append);

The writeAsString() method writes the data asynchronously.It opens the file before writing and closes the file when done.To append data to an existing file, you can use the optionalparameter mode and set its value to FileMode.append.Otherwise, the mode is FileMode.write and the previous contents of the file,if any, are overwritten.

If you want to write more data, you can open the file for writing.The openWrite() method returns an IOSink (the same type as stdin and stderr).You can continue to write to the file until done,at which time, you must close the file.The close() method is asynchronous and returns a future.

  1. final quotes = File('quotes.txt').openWrite(mode: FileMode.append);
  2. quotes.write("Don't cry because it's over, ");
  3. quotes.writeln("smile because it happened. -Dr. Seuss");
  4. await quotes.close();

Getting environment information

Use the Platform classto get information about the machine and OS that the program is running on.

Platform.environmentprovides a copy of the environmentvariables in an immutable map. If you need a mutable map (modifiable copy) youcan use Map.from(Platform.environment).

  1. final envVarMap = Platform.environment;
  2. print('PWD = ${envVarMap["PWD"]}');
  3. print('LOGNAME = ${envVarMap["LOGNAME"]}');
  4. print('PATH = ${envVarMap["PATH"]}');

Platform provides other useful properties that giveinformation about the machine, OS, and currentlyrunning program. For example:

Setting exit codes

The dart:io library defines a top-level property,exitCode, that you can change to set the exit code forthe current invocation of the Dart VM.An exit code is a number passed fromthe Dart program to the parent processto indicate the success, failure, or other state of theexecution of the program.

The dcat program sets the exit codein the _handleError() function to indicate that an errorocccurred during execution.

  1. Future _handleError(String path) async {
  2. if (await FileSystemEntity.isDirectory(path)) {
  3. stderr.writeln('error: $path is a directory');
  4. } else {
  5. exitCode = 2;
  6. }
  7. }

An exit code of 2 indicates that the program encountered an error.

An alternative to using exitCode is to use the top-level exit() function,which sets the exit code and quits the program immediately.For example, the _handleError() function could call exit(2)instead of setting exitCode to 2,but exit() would quit the programand it might not process all of the files on the command line.

Generally speaking, you’re better off using the exitCode property, which sets the exit code but allows the program to continue through to its natural completion.

Although you can use any number for an exit code,by convention, the codes in the table below have the following meanings:

CodeMeaning
0Success
1Warnings
2Errors

Summary

This tutorial described some basic API found in these classes from the dart:io library:

APIDescription
IOSinkHelper class for objects that consume data from streams.
FileRepresents a file on the native file system
DirectoryRepresents a directory on the native file system
FileSystemEntitySuperclass for File and Directory
PlatformProvides information about the machine and operating system
stdoutThe standard output
stderrThe standard error
stdinThe standard input
exitCodeSets the exit code
exit()Sets the exit code and quits

In addition, this tutorial covers two classes that help with command-line arguments:ArgParser andArgResults.

For more classes, functions, and properties,consult to the API reference fordart:io,dart:convert,and the args package.

What next?

If you’re interested in server-side programming,check out the next tutorial, which coversHTTP clients and servers.