Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

write the docs once

13 views
Skip to first unread message

Eli the Bearded

unread,
Dec 27, 2023, 10:20:33 PM12/27/23
to
Just a hack, no other content.

I have a habit, in small helper shell scripts / perl scripts, to
document what it is and how it is used right after the #! line. And then
sometimes I don't want to code up a usage() shell function with the same
document as in the comments.

For those cases, here's a fix.

Use the comments at the top of the script as the error usage help
message.

if usage_error_condition; then
echo "${0##*/}: usage error"
sed -e 's/^#!.*/ /;s/^# *$/ /; s/^# //; /^$/ q' < $0
exit 2
fi

Explained:
${0##*/} the script name, with the leading path component removed

sed < $0 read from the script making changes:
s/^#!.*/ / blank out the #! line
s/^# *$/ / blank out blank comments
s/^# // remove leading "# " from comments
/^$/ q stop printing upon reaching first blank line


Elijah
------
Buzz McCool shouldn't be the only one keeping the group alive
0 new messages