License (3-Clause BSD)
GitHub CI
codecov
pub package

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 相同 布尔值
  • pathsoutput 是必需的。
  • output 应该是 IOSink,例如 stdoutFile 流。
  • input 可以是 stdin

其余可选参数与 GNU cat 实用程序相似。

返回一个 CatResult 对象,其中包含 exitCodeexitSuccessexitFailure)和任何 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}");
  }
}

查看完整示例

GitHub

查看 Github