DartPad run

You can use DartPad to simply try out the Dart programming language and API without having to download anything.

Install the Dart

With Homebrew, installing Dart is easy.

brew tap dart-lang/dart
brew install dart
Copy the code

Create a small app

Use the dart create command to create a command-line application using the console-full template:

dart create -t console-full cli
Copy the code

This command creates a Dart application that contains the following information:

  • A primary Dart source file,bin/cli.dartThe file contains a top layermain()Function. This function is the entry point to your application.
  • An additional Dart file,lib/cli.dartContains some functional function methods that will be imported tocli.dartFile.
  • A PubSpec file,pubspec.yamlContains the metadata of the application, including those on which the application dependspackageInformation and required versions, etc.

Run the application

To run the application from the command line, run the DART VM in the root directory of the application using the dart run command:

cd cli
dart run
Copy the code

Compile into a formal product

In the example steps above, we used the Dart VM (i.e., the Dart command) to run the application. The Dart VM is optimized for rapid incremental compilation to provide immediate response during development. Now that your applet is complete, it’s time for AOT optimization to compile your Dart code into native machine code.

Compile the program AOT into machine code using the Dart Compile tool:

dart compile exe bin/cli.dart
Copy the code

See how quickly the compiled program starts:

time bin/cli.exe
Copy the code

Access to rely on

To obtain dependent packages, such as args packages, use the Pub package manager.

However, for the first CLI, we can easily add what we need using the Dart pub add command.

dart pub add args
Copy the code

Dcat application code example

Standard input and print implementation reference examples, using the arGS dependency library above.

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

import 'package:args/args.dart';

const lineNumber = 'line-number';

void main(List<String> arguments) {
  exitCode = 0; // presume success
  final parser = ArgParser()..addFlag(lineNumber, negatable: false, abbr: 'n');

  ArgResults argResults = parser.parse(arguments);
  final paths = argResults.rest;

  dcat(paths, showLineNumbers: argResults[lineNumber] as bool);
}

Future<void> dcat(List<String> paths, {bool showLineNumbers = false}) async {
  if (paths.isEmpty) {
    // No files provided as arguments. Read from stdin and print each line.
    await stdin.pipe(stdout);
  } else {
    for (final path in paths) {
      var lineNumber = 1;
      final lines = utf8.decoder
          .bind(File(path).openRead())
          .transform(const LineSplitter());
      try {
        await for (final line in lines) {
          if (showLineNumbers) {
            stdout.write('${lineNumber++} ');
          }
          stdout.writeln(line);
        }
      } catch (_) {
        await _handleError(path);
      }
    }
  }
}

Future<void> _handleError(String path) async {
  if (await FileSystemEntity.isDirectory(path)) {
    stderr.writeln('error: $path is a directory');
  } else {
    exitCode = 2;
  }
}
Copy the code

release

Similar to the Flutter library package publishing process, you can choose to submit to the official Dart Pub or your own private server.