Skip to content

Commit

Permalink
New option --file-line.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfuzz committed Oct 4, 2023
1 parent 8ffd253 commit bfe71b3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

### ??? -- ???

Notable changes:

* `bashy-core`:
* `stderr-msg`: New option `--file-line`.

### v2.5 -- 2023-10-04

Notable changes:
Expand Down
17 changes: 17 additions & 0 deletions scripts/lib/bashy-core/stderr-messages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# when "manually" turning on or off messages.
# --exec -- Execute the arguments as a command, instead of treating them as
# arguments to print. The command's output gets redirected to `stderr`.
# --file-line -- Include the caller-of-the-caller's file and line number. That
# is, a user of the command can use this to talk about what called it.
# --set=0|1 -- Enable or disable printing of this kind of message.
# --status -- Prints `1` or `0` to stdout, to indicate enabled status. (This is
# to make it easy to propagate the enabled state down into another command.)
Expand Down Expand Up @@ -104,6 +106,7 @@ function _stderr_print-handler {
shift 2

local doExec=0
local doFileLine=0
local printName=1
local wasCmd=0

Expand All @@ -120,6 +123,9 @@ function _stderr_print-handler {
--exec)
doExec=1
;;
--file-line)
doFileLine=1
;;
--no-name)
if [[ ${anyMessagesVarName} == '' ]]; then
error-msg "Unrecognized option: $1"
Expand Down Expand Up @@ -147,13 +153,24 @@ function _stderr_print-handler {
return
fi

local didPrintName=0
if [[ ${anyMessagesVarName} != '' ]] && (( !${!anyMessagesVarName} )); then
if (( printName )); then
didPrintName=1
printf 1>&2 '%s: ' "$(this-cmd-name)"
fi
eval "${anyMessagesVarName}=1"
fi

if (( doFileLine )); then
if (( didPrintName )); then
printf 1>&2 '\n'
fi
# `3` (and not `2`) because there's an internal caller layer to elide.
[[ "$(caller 3)" =~ ^([0-9]+).*/(.*)$ ]]
printf 1>&2 '%s:%s: ' "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
fi

if (( doExec )); then
"$@" 1>&2
else
Expand Down

0 comments on commit bfe71b3

Please sign in to comment.