Skip to content

CLI changes

Bob Nystrom edited this page May 8, 2023 · 1 revision

The Dart SDK ships with a number of command line tools: dart, dartanalyzer, dartfmt. To reduce the proliferation of names cluttering up your PATH, we are consolidating all of that functionality under a single dart command. The new way to run the formatter is:

$ dart format <args...>

Since we're making a change to move to a new command anyway, we took the opportunity to revamp the CLI options and output. The old interface was ad hoc and verbose. If you disliked having to remember to pass -w or were annoyed by hundreds of lines of output about unchanged files, we think you'll like the new interface.

Here's the new way to accomplish some common tasks:

Reformat everything in the current directory:

$ dart format .

Reformats all .dart files in the current directory and any subdirectories, recursively and overwrites the files with the result. It prints only the names of files whose formatting changed. If you want to see all processed files, even unchanged ones, run:

$ dart format --show=all .

Reformat specific directories or files:

You can pass one or more directory and file names to reformat just those:

$ dart format some_dir a_file.dart

This reformats and overwrites everything in some_dir and a_file.dart.

Show which files need formatting:

$ dart format --output=none --set-exit-if-changed some_dir

This prints the names of all files in some_dir whose formatting would change, but doesn't actually modify any files. The --output=none (or just -o none) is equivalent to the old --dry-run flag.

The --set-exit-if-changed flag also tells the formatter to exit with a non-zero exit code if there are files that need formatting. This can be useful if you want to write a presubmit script or something that checks to see if there are unformatted files.

Print the formatted result to stdout:

$ dart format --output=show a_file.dart

This formats the contents of a_file.dart and prints the result to stdout. It does not overwrite the file.

Learn more

You can see docs of the most common options by running:

$ dart help format

To learn about every single flag and option, run:

$ dart help format --verbose

If you have questions or feedback, please file an issue.