| OLD | NEW |
| 1 # cli_util | 1 # cli_util |
| 2 | 2 |
| 3 A library to help in building Dart command-line apps. | 3 A library to help in building Dart command-line apps. |
| 4 | 4 |
| 5 In particular, `cli_util` provides a simple, standardized way to get the current | 5 In particular, `cli_util` provides a simple, standardized way to get the current |
| 6 SDK directory. Useful, especially, when building client applications that | 6 SDK directory. Useful, especially, when building client applications that |
| 7 interact with the Dart SDK (such as the [analyzer][analyzer]). | 7 interact with the Dart SDK (such as the [analyzer][analyzer]). |
| 8 | 8 |
| 9 [](https://travis-c
i.org/dart-lang/cli_util) | 9 [](https://travis-c
i.org/dart-lang/cli_util) |
| 10 [](https://pub.dartlang.org/pac
kages/cli_util) |
| 10 | 11 |
| 11 ## Usage | 12 ## Locating the Dart SDK |
| 12 | 13 |
| 13 ```dart | 14 ```dart |
| 14 import 'dart:io'; | 15 import 'dart:io'; |
| 15 | 16 |
| 16 import 'package:cli_util/cli_util.dart'; | 17 import 'package:cli_util/cli_util.dart'; |
| 17 import 'package:path/path.dart' as path; | 18 import 'package:path/path.dart' as path; |
| 18 | 19 |
| 19 main(args) { | 20 main(args) { |
| 20 // Get sdk dir from cli_util | 21 // Get sdk dir from cli_util. |
| 21 Directory sdkDir = getSdkDir(args); | 22 String sdkPath = getSdkPath(); |
| 22 | 23 |
| 23 // Do stuff... For example, print version string | 24 // Do stuff... For example, print version string |
| 24 File versionFile = new File(path.join(sdkDir.path, 'version')); | 25 File versionFile = new File(path.join(sdkPath, 'version')); |
| 25 print(versionFile.readAsStringSync()); | 26 print(versionFile.readAsStringSync()); |
| 26 } | 27 } |
| 27 ``` | 28 ``` |
| 28 | 29 |
| 30 ## Displaying output and progress |
| 31 |
| 32 `package:cli_util` can also be used to help CLI tools display output and progres
s. |
| 33 It has a logging mechanism which can help differentiate between regular tool |
| 34 output and error messages, and can facilitate having a more verbose (`-v`) mode
for |
| 35 output. |
| 36 |
| 37 In addition, it can display an indeterminate progress spinner for longer running |
| 38 tasks, and optionally display the elapsed time when finished: |
| 39 |
| 40 ```dart |
| 41 import 'package:cli_util/cli_logging.dart'; |
| 42 |
| 43 main(List<String> args) async { |
| 44 bool verbose = args.contains('-v'); |
| 45 Logger logger = verbose ? new Logger.verbose() : new Logger.standard(); |
| 46 |
| 47 logger.stdout('Hello world!'); |
| 48 logger.trace('message 1'); |
| 49 await new Future.delayed(new Duration(milliseconds: 200)); |
| 50 logger.trace('message 2'); |
| 51 logger.trace('message 3'); |
| 52 |
| 53 Progress progress = logger.progress('doing some work'); |
| 54 await new Future.delayed(new Duration(seconds: 2)); |
| 55 progress.finish(showTiming: true); |
| 56 |
| 57 logger.stdout('All ${logger.ansi.emphasized('done')}.'); |
| 58 logger.flush(); |
| 59 } |
| 60 ``` |
| 61 |
| 29 ## Features and bugs | 62 ## Features and bugs |
| 30 | 63 |
| 31 Please file feature requests and bugs at the [issue tracker][tracker]. | 64 Please file feature requests and bugs at the [issue tracker][tracker]. |
| 32 | 65 |
| 33 [analyzer]: https://pub.dartlang.org/packages/analyzer | 66 [analyzer]: https://pub.dartlang.org/packages/analyzer |
| 34 [tracker]: https://github.com/dart-lang/cli_util/issues | 67 [tracker]: https://github.com/dart-lang/cli_util/issues |
| OLD | NEW |