Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/tools-help
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ for f in "${tools[@]}"; do
|| fail "$f --help doesn't follow its header when it grows by a line"
done
echo "ok - ${#tools[@]} tools print their header comment as --help, however long it grows, and no code"

# The same --help, output and exit code, run by a relative path from the
# repository's parent and from tools/: a tool that cds to the repository
# first must not look for its header at "$0" from the directory it left
# (#559).
help_from() { (cd -- "$1" && "$2" --help 2>&1; echo "exit $?"); }
repo=$PWD
for f in "${tools[@]}"; do
out=$(help_from "$repo" "$f")
[[ $(help_from "$repo/.." "${repo##*/}/$f") == "$out" ]] || fail "$f --help differs run from the repository's parent"
[[ $(help_from "$repo/tools" "./${f#tools/}") == "$out" ]] || fail "$f --help differs run from tools/"
done
echo "ok - ${#tools[@]} tools print the same --help by a relative path from another directory"
12 changes: 10 additions & 2 deletions tools/lib/usage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@
#
# source tools/lib/usage.sh
# usage_from_header [--raw] FILE [PARAGRAPHS]
# prints FILE's header with each line's "#" and one space after it taken
# prints the header of the tool named FILE, in the tools directory
# above this file, with each line's "#" and one space after it taken
# off (--raw keeps them). With PARAGRAPHS, only that many paragraphs of
# it (a bare "#" line ends one), for a tool whose help is the first part
# of a longer header.
#
# Only FILE's name is used. A tool that cds to the repository first and
# passes "$0" passes a path relative to the directory it left
# (`cd ..; omarchy-mobile/tools/build-packages --help`, #559), while this
# file's own path is still good: the tool has just sourced it from where it
# is now.

usage_from_header() {
local raw=0
[[ ${1:-} == --raw ]] && { raw=1; shift; }
local name=${1:?usage_from_header FILE [PARAGRAPHS]}
awk -v raw="$raw" -v paras="${2:-0}" '
NR <= 2 { next }
!/^#/ { exit }
/^#[ \t]*$/ && ++blank == paras { exit }
{ if (!raw) sub(/^# ?/, ""); print }' "${1:?usage_from_header FILE [PARAGRAPHS]}"
{ if (!raw) sub(/^# ?/, ""); print }' "$(dirname -- "${BASH_SOURCE[0]}")/../${name##*/}"
}