mirror of
https://github.com/status-im/nim-confutils.git
synced 2025-02-01 23:05:33 +00:00
ignore exceptions while writing to stdout/stderr (#79)
`writeLine` can fail with `IOError`, and `styledWrite` when using colors also with `ValueError`. To ensure that the control flow is unaffected, simply ignore those errors while writing human-readable output to fds.
This commit is contained in:
parent
8522393cec
commit
4dbd23af3b
@ -121,10 +121,16 @@ when useBufferedOutput:
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
template errorOutput(args: varargs[untyped]) =
|
template errorOutput(args: varargs[untyped]) =
|
||||||
|
try:
|
||||||
styledWrite stderr, args
|
styledWrite stderr, args
|
||||||
|
except IOError, ValueError:
|
||||||
|
discard
|
||||||
|
|
||||||
template helpOutput(args: varargs[untyped]) =
|
template helpOutput(args: varargs[untyped]) =
|
||||||
|
try:
|
||||||
styledWrite stdout, args
|
styledWrite stdout, args
|
||||||
|
except IOError, ValueError:
|
||||||
|
discard
|
||||||
|
|
||||||
template flushOutput =
|
template flushOutput =
|
||||||
discard
|
discard
|
||||||
@ -969,9 +975,15 @@ proc loadImpl[C, SecondarySources](
|
|||||||
let trailing = if opt.typename != "bool": "=" else: ""
|
let trailing = if opt.typename != "bool": "=" else: ""
|
||||||
|
|
||||||
if argName in filterKind and len(opt.name) > 0:
|
if argName in filterKind and len(opt.name) > 0:
|
||||||
|
try:
|
||||||
stdout.writeLine("--", opt.name, trailing)
|
stdout.writeLine("--", opt.name, trailing)
|
||||||
|
except IOError:
|
||||||
|
discard
|
||||||
if argAbbr in filterKind and len(opt.abbr) > 0:
|
if argAbbr in filterKind and len(opt.abbr) > 0:
|
||||||
|
try:
|
||||||
stdout.writeLine('-', opt.abbr, trailing)
|
stdout.writeLine('-', opt.abbr, trailing)
|
||||||
|
except IOError:
|
||||||
|
discard
|
||||||
|
|
||||||
let completion = splitCompletionLine()
|
let completion = splitCompletionLine()
|
||||||
# If we're not asked to complete a command line the result is an empty list
|
# If we're not asked to complete a command line the result is an empty list
|
||||||
@ -1017,12 +1029,18 @@ proc loadImpl[C, SecondarySources](
|
|||||||
let opt = findOpt(cmdStack, option_word)
|
let opt = findOpt(cmdStack, option_word)
|
||||||
if opt != nil:
|
if opt != nil:
|
||||||
for arg in getArgCompletions(opt, cur_word):
|
for arg in getArgCompletions(opt, cur_word):
|
||||||
|
try:
|
||||||
stdout.writeLine(arg)
|
stdout.writeLine(arg)
|
||||||
|
except IOError:
|
||||||
|
discard
|
||||||
elif cmdStack[^1].hasSubCommands:
|
elif cmdStack[^1].hasSubCommands:
|
||||||
# Show all the available subcommands
|
# Show all the available subcommands
|
||||||
for subCmd in subCmds(cmdStack[^1]):
|
for subCmd in subCmds(cmdStack[^1]):
|
||||||
if startsWithIgnoreStyle(subCmd.name, cur_word):
|
if startsWithIgnoreStyle(subCmd.name, cur_word):
|
||||||
|
try:
|
||||||
stdout.writeLine(subCmd.name)
|
stdout.writeLine(subCmd.name)
|
||||||
|
except IOError:
|
||||||
|
discard
|
||||||
else:
|
else:
|
||||||
# Full options listing
|
# Full options listing
|
||||||
for i in countdown(cmdStack.len - 1, 0):
|
for i in countdown(cmdStack.len - 1, 0):
|
||||||
@ -1234,4 +1252,3 @@ func load*(f: TypedInputFile): f.ContentType =
|
|||||||
else:
|
else:
|
||||||
mixin loadFile
|
mixin loadFile
|
||||||
loadFile(f.Format, f.string, f.ContentType)
|
loadFile(f.Format, f.string, f.ContentType)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user