|
| 1 | +[](https://github.com/dart-lang/tools/actions/workflows/cli_util.yaml) |
| 2 | +[](https://pub.dev/packages/cli_util) |
| 3 | +[](https://pub.dev/packages/cli_util/publisher) |
| 4 | + |
| 5 | +A package to help in building Dart command-line apps. |
| 6 | + |
| 7 | +## What's this? |
| 8 | + |
| 9 | +`package:cli_util` provides: |
| 10 | +- utilities to find the Dart SDK directory (`sdkPath`) |
| 11 | +- utilities to find the settings directory for a tool (`applicationConfigHome()`) |
| 12 | +- utilities to aid in showing rich CLI output and progress information (`cli_logging.dart`) |
| 13 | + |
| 14 | +## Locating the Dart SDK |
| 15 | + |
| 16 | +```dart |
| 17 | +import 'dart:io'; |
| 18 | +
|
| 19 | +import 'package:cli_util/cli_util.dart'; |
| 20 | +import 'package:path/path.dart' as path; |
| 21 | +
|
| 22 | +main(args) { |
| 23 | + // Get SDK directory from cli_util. |
| 24 | + var sdkDir = sdkPath; |
| 25 | + |
| 26 | + // Do stuff... For example, print version string |
| 27 | + var versionFile = File(path.join(sdkDir, 'version')); |
| 28 | + print(versionFile.readAsStringSync()); |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +## Displaying output and progress |
| 33 | + |
| 34 | +`package:cli_util` can also be used to help CLI tools display output and progress. |
| 35 | +It has a logging mechanism which can help differentiate between regular tool |
| 36 | +output and error messages, and can facilitate having a more verbose (`-v`) mode for |
| 37 | +output. |
| 38 | + |
| 39 | +In addition, it can display an indeterminate progress spinner for longer running |
| 40 | +tasks, and optionally display the elapsed time when finished: |
| 41 | + |
| 42 | +```dart |
| 43 | +import 'package:cli_util/cli_logging.dart'; |
| 44 | +
|
| 45 | +void main(List<String> args) async { |
| 46 | + var verbose = args.contains('-v'); |
| 47 | + var logger = verbose ? Logger.verbose() : Logger.standard(); |
| 48 | +
|
| 49 | + logger.stdout('Hello world!'); |
| 50 | + logger.trace('message 1'); |
| 51 | + await Future.delayed(Duration(milliseconds: 200)); |
| 52 | + logger.trace('message 2'); |
| 53 | + logger.trace('message 3'); |
| 54 | +
|
| 55 | + var progress = logger.progress('doing some work'); |
| 56 | + await Future.delayed(Duration(seconds: 2)); |
| 57 | + progress.finish(showTiming: true); |
| 58 | +
|
| 59 | + logger.stdout('All ${logger.ansi.emphasized('done')}.'); |
| 60 | + logger.flush(); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Features and bugs |
| 65 | + |
| 66 | +Please file feature requests and bugs at the [issue tracker][tracker]. |
| 67 | + |
| 68 | +[tracker]: https://github.com/dart-lang/tools/issues |
0 commit comments