目录

Contents

dart run 命令

dart run 命令支持运行文件、当前 package 或者当前 package 的依赖中运行Dart程序。该命令提供了以前在 pub run 和 Dart VM 工具里提供的功能。如果想从任意位置运行程序,可以使用 pub global 命令。

The dart run command supports running a Dart program — located in a file, in the current package, or in one of the dependencies of the current package — from the command line. This command provides functionality that was previously in pub run and the Dart VM tool. To run a program from an arbitrary location, use the pub global command.

dart run [options] [<DART_FILE> | <PACKAGE_TARGET>] [args]

下面的例子演示了如何创建并且运行一个 Dart 程序:

Here’s an example of creating a new app and running it:

$ dart create myapp
$ cd myapp
$ dart run

运行 Dart 文件

Running a Dart file

可以通过传入 Dart 文件的相对路径来运行一个 Dart 程序:

You can run a Dart file by passing its relative path:

$ dart run tool/debug.dart

运行 package 中的 Dart 程序

Running a program that’s in a package

本节说明的前提是你在 package 的根目录执行 dart run 命令。更多关于 package 目录结构的介绍,请查阅 package 布局约定

The instructions in this section assume that you’re executing the dart run command from the directory that’s at the top of a Dart package (the current package). For information on the directory structure of Dart packages, see package layout conventions.

运行依赖 package 中的 Dart 程序

In a depended-on package

你可以运行当前 package 所依赖的任何 package 的 Dart 程序,只要它在 bin 目录下。运行时请指定依赖的 package 名和程序名。如果程序名与 package 名相同,则可以省略。

You can run programs that are distributed in the bin directory of any package that the current package depends on. To run such a program, specify the depended-on package name and the program name. You can omit the program name if it’s the same as the package name.

例如,在 package 的根目录,想运行一个名为 bar 的依赖 package 中的 main 程序(bin/bar.dart),使用如下命令:

For example, say you’re in the top directory of a package that depends on the bar package. To run the main program that’s in the bar package (bin/bar.dart), you can use this command:

$ dart run bar

如果程序名和 package 名不同,则需要使用 <package 名>:<程序名> 的形式来运行程序。比如 bin/baz.dart 程序在名为 bar 的 package 中, 我们就可以这样运行:

If the program name doesn’t match the package name, use the form <package name>:<program name>. For example, to run the program bin/baz.dart that’s in the bar package, use this command:

$ dart run bar:baz

放在 bin 目录下的 Dart 文件是可以被 Dart 命令行工具直接访问的,依赖 package 中的其他目录都是私有的。

The bin directory is the only place with visible programs. All other directories in the depended-on package are private.

在当前 package 中运行 Dart 程序

In the current package

当当前的路径与 package 名匹配时(即文件夹名称与 pubspec 中的 name 属性相同),可以省略 package 名。如果程序名与 package 名匹配(也就是它是 main 程序),也可以省略程序名。

When the current directory matches the package name (that is, you’re in the directory that matches the name property in the pubspec), then you can omit the package name. If the program name matches the package name (that is, it’s the main program), then you can also omit the program name.

dart run 是运行 Dart 程序最短的形式,它会运行当前 package 的主程序。例如,如果你在名为 foo 的 package 根目录执行下面命令,实际上会运行 bin/foo.dart 文件:

Here’s the shortest form of dart run, which runs the main program for the current package. For example, if you’re in the top directory of the foo package, this command runs bin/foo.dart:

$ dart run

如果程序名和 package 名不同,需要使用 :<程序名> 的形式来运行程序,比如运行当前 package 中的 bin/baz.dart 文件:

If the program name doesn’t match the package name, then add a colon and the program name. For example, this command runs bin/baz.dart in the current package:

$ dart run :baz

要运行在当前 package 但不在 bin 目录的程序时,通过传递相对路径运行(如前文所示):

To run a program that’s in the current package but not in the bin directory, pass a relative path (as shown before):

$ dart run tool/debug.dart

给 main() 传递参数

Supplying arguments to main()

若你需要 main() 函数传递参数,在命令行后面添加参数即可:

To supply arguments to the main() function, put them at the end of the command:

$ dart run tool/debug.dart arg1 arg2

在当前 package 中运行 main 程序需要添加 package 名,比如要传递参数并运行位于 foo package 根目录中的 bin/foo.dart 程序:

When you’re running the main program for the current package, add the package name. Here’s an example of running bin/foo.dart with arguments while you’re in the top directory of the foo package:

$ dart run foo arg1 arg2

调试

Debugging

可以通过传递一个或者多个调试选项来开启调试功能,下面是一个开启 断言 功能的例子:

To enable debugging, pass one or more debugging options. Here’s an example of enabling assert statements:

$ dart run --enable-asserts tool/debug.dart

运行 dart run --help 获取更多信息。

See dart run --help for details.

开启实验性功能

Enabling experimental features

如果在开发中想尝试实验性功能,可以使用 实验性功能开关

To enable new features and enhancements that are currently in development, use experiment flags.