dcat: 将文件连接到标准输出或文件
一个 Dart 命令列和库实现的标准 cat Unix 实用程序,灵感来自 编写命令行应用程序示例代码。
概要
dcat 依次读取文件或标准输入(如果没有给定),并将它们写入标准输出或文件。
命令行用法
dcat --help
Usage: dcat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines, overrides -n
-e, --show-nonprinting-ends equivalent to -vE
-E, --show-ends display $ at end of each line
-h, --help display this help and exit
-n, --number number all output lines
-t, --show-nonprinting-tabs equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-s, --squeeze-blank suppress repeated empty output lines
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--version output version information and exit
Examples:
dcat f - g Output f's contents, then standard input, then g's contents.
dcat Copy standard input to standard output.
编译独立应用程序
Linux / MacOS
dart compile exe -o bin/dcat bin/dcat.dart
Windows
dart compile exe bin/dcat.dart
库用法
dart pub add dcat
import 'package:dcat/dcat.dart';
final result = await cat(['path/to/file', 'path/to/otherfile]'],
File('path/to/outfile').openWrite());
if (result.isFailure) {
for (final error in result.errors) {
print('Error: ${error.message}');
}
}
cat 函数支持以下参数
| 参数 | 描述 | 类型 |
|---|---|---|
| paths | 文件路径。 | String[] |
| 输出 | 标准输出或文件。 | IOSink |
| input | 标准输入。 | Stream |
| showEnds | 与 -e 相同 |
布尔值 |
| numberNonBlank | 与 -b 相同 |
布尔值 |
| showLineNumbers | 与 -n 相同 |
布尔值 |
| showTabs | 与 -T 相同 |
布尔值 |
| squeezeBlank | 与 -s 相同 |
布尔值 |
| showNonPrinting | 与 -v 相同 |
布尔值 |
其余可选参数与 GNU cat 实用程序相似。
返回一个 CatResult 对象,其中包含 exitCode(exitSuccess 或 exitFailure)和任何 errors。
final result =
await cat(['path/to/file'], stdout, showLineNumbers: true);
if (result.exitCode == exitSuccess) { // or result.isSuccess
// ...
} else {
for (final error in result.errors) {
stderr.writeln("Error with '${error.path}': ${error.message}");
}
}