Cleaned up errors reporting.
This commit is contained in:
parent
96b4784fc8
commit
293a29c7a2
5 changed files with 44 additions and 47 deletions
|
@ -77,7 +77,7 @@ squeezeBlank | Same as `-s` | bool
|
||||||
showNonPrinting | Same as `-v` | bool
|
showNonPrinting | Same as `-v` | bool
|
||||||
|
|
||||||
* `paths` and `output` are required.
|
* `paths` and `output` are required.
|
||||||
* `output` should be an [IOSink](https://api.dart.dev/dart-io/IOSink-class.html) like `stdout` or a [File](https://api.dart.dev/dart-io/File/openWrite.html) stream.
|
* `output` should be an [IOSink](https://api.dart.dev/dart-io/IOSink-class.html) such as `stdout` or a [File](https://api.dart.dev/dart-io/File/openWrite.html) stream.
|
||||||
* `input` can be [stdin](https://api.dart.dev/dart-io/Stdin-class.html).
|
* `input` can be [stdin](https://api.dart.dev/dart-io/Stdin-class.html).
|
||||||
|
|
||||||
The remaining optional parameters are similar to the [GNU cat](https://www.gnu.org/software/coreutils/manual/html_node/cat-invocation.html#cat-invocation) utility.
|
The remaining optional parameters are similar to the [GNU cat](https://www.gnu.org/software/coreutils/manual/html_node/cat-invocation.html#cat-invocation) utility.
|
||||||
|
|
|
@ -22,6 +22,8 @@ const showTabsFlag = 'show-tabs';
|
||||||
const squeezeBlank = 'squeeze-blank';
|
const squeezeBlank = 'squeeze-blank';
|
||||||
const versionFlag = 'version';
|
const versionFlag = 'version';
|
||||||
|
|
||||||
|
const _homePage = 'https://github.com/ethauvin/dcat';
|
||||||
|
|
||||||
/// Concatenates files specified in [arguments].
|
/// Concatenates files specified in [arguments].
|
||||||
///
|
///
|
||||||
/// Usage: `dcat [option] [file]…`
|
/// Usage: `dcat [option] [file]…`
|
||||||
|
@ -33,8 +35,9 @@ Future<int> main(List<String> arguments) async {
|
||||||
try {
|
try {
|
||||||
argResults = parser.parse(arguments);
|
argResults = parser.parse(arguments);
|
||||||
} on FormatException catch (e) {
|
} on FormatException catch (e) {
|
||||||
return printError(
|
await printError(
|
||||||
"${e.message}\nTry '$appName --$helpFlag' for more information.");
|
"${e.message}\nTry '$appName --$helpFlag' for more information.");
|
||||||
|
return exitFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argResults[helpFlag]) {
|
if (argResults[helpFlag]) {
|
||||||
|
@ -121,20 +124,19 @@ Future<ArgParser> setupArgsParser() async {
|
||||||
return parser;
|
return parser;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prints the error [message] to [stderr].
|
/// Prints an error [message] to [stderr].
|
||||||
Future<int> printError(String message) async {
|
Future<void> printError(String message) async {
|
||||||
stderr.writeln("$appName: $message");
|
stderr.writeln("$appName: $message");
|
||||||
return exitFailure;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prints the version info.
|
/// Prints the version info.
|
||||||
Future<int> printVersion() async {
|
Future<int> printVersion() async {
|
||||||
print('''$appName (Dart cat) $appVersion
|
print('''$appName (Dart cat) $appVersion
|
||||||
Copyright (C) 2021 Erik C. Thauvin
|
Copyright (C) 2021 Erik C. Thauvin
|
||||||
License: 3-Clause BSD <https://opensource.org/licenses/BSD-3-Clause>
|
License 3-Clause BSD: <https://opensource.org/licenses/BSD-3-Clause>
|
||||||
|
|
||||||
Inspired by <https://dart.dev/tutorials/server/cmdline>
|
Written by Erik C. Thauvin <https://erik.thauvin.net/>
|
||||||
Written by Erik C. Thauvin <https://erik.thauvin.net/>''');
|
Source: $_homePage''');
|
||||||
return exitSuccess;
|
return exitSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,6 +152,6 @@ Examples:
|
||||||
$appName f - g Output f's contents, then standard input, then g's contents.
|
$appName f - g Output f's contents, then standard input, then g's contents.
|
||||||
$appName Copy standard input to standard output.
|
$appName Copy standard input to standard output.
|
||||||
|
|
||||||
Source and documentation: <https://github.com/ethauvin/dcat>''');
|
Source and documentation: <$_homePage>''');
|
||||||
return exitSuccess;
|
return exitSuccess;
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,14 +88,15 @@
|
||||||
bool squeezeBlank = false,
|
bool squeezeBlank = false,
|
||||||
bool showNonPrinting = false}) async {
|
bool showNonPrinting = false}) async {
|
||||||
final result = CatResult();
|
final result = CatResult();
|
||||||
final lastLine = _LastLine(0, _lineFeed);
|
final lastLine = _LastLine();
|
||||||
|
|
||||||
if (paths.isEmpty) {
|
if (paths.isEmpty) {
|
||||||
if (input != null) {
|
if (input != null) {
|
||||||
try {
|
try {
|
||||||
await _writeStream(input, lastLine, output, showEnds, showLineNumbers,
|
await _writeStream(input, lastLine, output, showEnds, showLineNumbers,
|
||||||
numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
|
numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
result.addMessage(exitFailure, '$e');
|
result.addMessage(exitFailure, _formatError(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -109,20 +110,8 @@
|
||||||
}
|
}
|
||||||
await _writeStream(stream, lastLine, output, showEnds, showLineNumbers,
|
await _writeStream(stream, lastLine, output, showEnds, showLineNumbers,
|
||||||
numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
|
numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
|
||||||
} on FileSystemException catch (e) {
|
|
||||||
final String? osMessage = e.osError?.message;
|
|
||||||
final String message;
|
|
||||||
if (osMessage != null && osMessage.isNotEmpty) {
|
|
||||||
message = osMessage;
|
|
||||||
} else {
|
|
||||||
message = e.message;
|
|
||||||
}
|
|
||||||
result.addMessage(exitFailure, message, path: path);
|
|
||||||
} on FormatException {
|
|
||||||
result.addMessage(exitFailure, 'Binary file not supported.',
|
|
||||||
path: path);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
result.addMessage(exitFailure, '$e', path: path);
|
result.addMessage(exitFailure, _formatError(e), path: path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,7 +97,7 @@ if (result.isFailure) {
|
||||||
<table><thead><tr><th style="text-align: left;">Parameter</th><th style="text-align: left;">Description</th><th style="text-align: left;">Type</th></tr></thead><tbody><tr><td style="text-align: left;">paths</td><td style="text-align: left;">The file paths.</td><td style="text-align: left;">String[]</td></tr><tr><td style="text-align: left;">output</td><td style="text-align: left;">The standard output or file.</td><td style="text-align: left;"><a href="https://api.dart.dev/dart-io/IOSink-class.html">IOSink</a></td></tr><tr><td style="text-align: left;">input</td><td style="text-align: left;">The standard input.</td><td style="text-align: left;"><a href="https://api.dart.dev/dart-io/Stdin-class.html">Stream</a></td></tr><tr><td style="text-align: left;">showEnds</td><td style="text-align: left;">Same as <code>-e</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">numberNonBlank</td><td style="text-align: left;">Same as <code>-b</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showLineNumbers</td><td style="text-align: left;">Same as <code>-n</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showTabs</td><td style="text-align: left;">Same as <code>-T</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">squeezeBlank</td><td style="text-align: left;">Same as <code>-s</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showNonPrinting</td><td style="text-align: left;">Same as <code>-v</code></td><td style="text-align: left;">bool</td></tr></tbody></table>
|
<table><thead><tr><th style="text-align: left;">Parameter</th><th style="text-align: left;">Description</th><th style="text-align: left;">Type</th></tr></thead><tbody><tr><td style="text-align: left;">paths</td><td style="text-align: left;">The file paths.</td><td style="text-align: left;">String[]</td></tr><tr><td style="text-align: left;">output</td><td style="text-align: left;">The standard output or file.</td><td style="text-align: left;"><a href="https://api.dart.dev/dart-io/IOSink-class.html">IOSink</a></td></tr><tr><td style="text-align: left;">input</td><td style="text-align: left;">The standard input.</td><td style="text-align: left;"><a href="https://api.dart.dev/dart-io/Stdin-class.html">Stream</a></td></tr><tr><td style="text-align: left;">showEnds</td><td style="text-align: left;">Same as <code>-e</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">numberNonBlank</td><td style="text-align: left;">Same as <code>-b</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showLineNumbers</td><td style="text-align: left;">Same as <code>-n</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showTabs</td><td style="text-align: left;">Same as <code>-T</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">squeezeBlank</td><td style="text-align: left;">Same as <code>-s</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showNonPrinting</td><td style="text-align: left;">Same as <code>-v</code></td><td style="text-align: left;">bool</td></tr></tbody></table>
|
||||||
<ul>
|
<ul>
|
||||||
<li><code>paths</code> and <code>output</code> are required.</li>
|
<li><code>paths</code> and <code>output</code> are required.</li>
|
||||||
<li><code>output</code> should be an <a href="https://api.dart.dev/dart-io/IOSink-class.html">IOSink</a> like <code>stdout</code> or a <a href="https://api.dart.dev/dart-io/File/openWrite.html">File</a> stream.</li>
|
<li><code>output</code> should be an <a href="https://api.dart.dev/dart-io/IOSink-class.html">IOSink</a> such as <code>stdout</code> or a <a href="https://api.dart.dev/dart-io/File/openWrite.html">File</a> stream.</li>
|
||||||
<li><code>input</code> can be <a href="https://api.dart.dev/dart-io/Stdin-class.html">stdin</a>.</li>
|
<li><code>input</code> can be <a href="https://api.dart.dev/dart-io/Stdin-class.html">stdin</a>.</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>The remaining optional parameters are similar to the <a href="https://www.gnu.org/software/coreutils/manual/html_node/cat-invocation.html#cat-invocation">GNU cat</a> utility.</p>
|
<p>The remaining optional parameters are similar to the <a href="https://www.gnu.org/software/coreutils/manual/html_node/cat-invocation.html#cat-invocation">GNU cat</a> utility.</p>
|
||||||
|
|
|
@ -45,10 +45,8 @@ class CatResult {
|
||||||
|
|
||||||
// Holds the current line number and last character.
|
// Holds the current line number and last character.
|
||||||
class _LastLine {
|
class _LastLine {
|
||||||
int lineNumber;
|
int lineNumber = 0;
|
||||||
int lastChar;
|
int lastChar = _lineFeed;
|
||||||
|
|
||||||
_LastLine(this.lineNumber, this.lastChar);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Concatenates files in [paths] to the standard output or a file.
|
/// Concatenates files in [paths] to the standard output or a file.
|
||||||
|
@ -66,14 +64,15 @@ Future<CatResult> cat(List<String> paths, IOSink output,
|
||||||
bool squeezeBlank = false,
|
bool squeezeBlank = false,
|
||||||
bool showNonPrinting = false}) async {
|
bool showNonPrinting = false}) async {
|
||||||
final result = CatResult();
|
final result = CatResult();
|
||||||
final lastLine = _LastLine(0, _lineFeed);
|
final lastLine = _LastLine();
|
||||||
|
|
||||||
if (paths.isEmpty) {
|
if (paths.isEmpty) {
|
||||||
if (input != null) {
|
if (input != null) {
|
||||||
try {
|
try {
|
||||||
await _writeStream(input, lastLine, output, showEnds, showLineNumbers,
|
await _writeStream(input, lastLine, output, showEnds, showLineNumbers,
|
||||||
numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
|
numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
result.addMessage(exitFailure, '$e');
|
result.addMessage(exitFailure, _formatError(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -87,26 +86,32 @@ Future<CatResult> cat(List<String> paths, IOSink output,
|
||||||
}
|
}
|
||||||
await _writeStream(stream, lastLine, output, showEnds, showLineNumbers,
|
await _writeStream(stream, lastLine, output, showEnds, showLineNumbers,
|
||||||
numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
|
numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
|
||||||
} on FileSystemException catch (e) {
|
|
||||||
final String? osMessage = e.osError?.message;
|
|
||||||
final String message;
|
|
||||||
if (osMessage != null && osMessage.isNotEmpty) {
|
|
||||||
message = osMessage;
|
|
||||||
} else {
|
|
||||||
message = e.message;
|
|
||||||
}
|
|
||||||
result.addMessage(exitFailure, message, path: path);
|
|
||||||
} on FormatException {
|
|
||||||
result.addMessage(exitFailure, 'Binary file not supported.',
|
|
||||||
path: path);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
result.addMessage(exitFailure, '$e', path: path);
|
result.addMessage(exitFailure, _formatError(e), path: path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Formats error message.
|
||||||
|
String _formatError(Object e) {
|
||||||
|
final String message;
|
||||||
|
if (e is FileSystemException) {
|
||||||
|
final String? osMessage = e.osError?.message;
|
||||||
|
if (osMessage != null && osMessage.isNotEmpty) {
|
||||||
|
message = osMessage;
|
||||||
|
} else {
|
||||||
|
message = e.message;
|
||||||
|
}
|
||||||
|
} else if (e is FormatException) {
|
||||||
|
message = 'Binary file not supported.';
|
||||||
|
} else {
|
||||||
|
message = '$e';
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
// Writes parsed data from a stream
|
// Writes parsed data from a stream
|
||||||
Future<void> _writeStream(
|
Future<void> _writeStream(
|
||||||
Stream stream,
|
Stream stream,
|
||||||
|
@ -118,9 +123,8 @@ Future<void> _writeStream(
|
||||||
bool showTabs,
|
bool showTabs,
|
||||||
bool squeezeBlank,
|
bool squeezeBlank,
|
||||||
bool showNonPrinting) async {
|
bool showNonPrinting) async {
|
||||||
const tab = 9;
|
// No flags
|
||||||
int squeeze = 0;
|
if (!showEnds &&
|
||||||
final noFlags = !showEnds &&
|
|
||||||
!showLineNumbers &&
|
!showLineNumbers &&
|
||||||
!numberNonBlank &&
|
!numberNonBlank &&
|
||||||
!showTabs &&
|
!showTabs &&
|
||||||
|
@ -162,6 +166,7 @@ Future<void> _writeStream(
|
||||||
}
|
}
|
||||||
} else if (ch == tab) {
|
} else if (ch == tab) {
|
||||||
if (showTabs) {
|
if (showTabs) {
|
||||||
|
// TAB
|
||||||
sb.write('^I');
|
sb.write('^I');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -183,6 +188,7 @@ Future<void> _writeStream(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// CTRL
|
||||||
sb
|
sb
|
||||||
..write('^')
|
..write('^')
|
||||||
..writeCharCode(ch + 64);
|
..writeCharCode(ch + 64);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue