#!/bin/bash | |
### | |
### my-script — does one thing well | |
### | |
### Usage: | |
### my-script <input> <output> | |
### | |
### Options: | |
### <input> Input file to read. | |
### <output> Output file to write. Use '-' for stdout. | |
### -h Show this message. | |
help() { | |
sed -rn 's/^### ?//;T;p' "$0" | |
} | |
if [[ $# == 0 ]] || [[ "$1" == "-h" ]]; then | |
help | |
exit 1 | |
fi | |
echo Hello World |
This comment has been minimized.
This comment has been minimized.
This doesn't actually work cross-platform, the
Complete script: #!/bin/bash
###
### my-script — does one thing well
###
### Usage:
### my-script <input> <output>
###
### Options:
### <input> Input file to read.
### <output> Output file to write. Use '-' for stdout.
### -h Show this message.
help() {
awk '/^###/' "$0"
}
if [[ $# == 0 ]] || [[ "$1" == "-h" ]]; then
help
exit 1
fi
echo Hello World |
This comment has been minimized.
This comment has been minimized.
Install GNU |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
That's cool but I think I'll stick with here documents. #!/bin/bash
Help=$(cat <<-"HELP"
my-script — does one thing well
Usage:
my-script <input> <output>
Options:
<input> Input file to read.
<output> Output file to write. Use '-' for stdout.
-h Show this message.
HELP
)
help() {
echo "$Help"
}
if [[ $# == 0 ]] || [[ "$1" == "-h" ]]; then
help
exit 1
fi
echo Hello World |
This comment has been minimized.
This comment has been minimized.
This is a really nifty approach, thanks for sharing. It's fairly obvious in retrospect but it hadn't occurred to me before! Slight tweak, this awk solution will only print the help text, not the leading
|
This comment has been minimized.
This comment has been minimized.
Compacted: |
This comment has been minimized.
This comment has been minimized.
or you can use |
This comment has been minimized.
This comment has been minimized.
A #!/bin/sh
###
### my-script — does one thing well
###
### Usage:
### my-script <input> <output>
###
### Options:
### <input> Input file to read.
### <output> Output file to write. Use '-' for stdout.
### -h Show this message.
help() {
sed -n 's/^### \?//p' "$0"
}
if [ $# -eq 0 ] || [ "$1" = "-h" ]; then
help
exit 1
fi
echo Hello World |
This comment has been minimized.
This comment has been minimized.
Complete awk version:
I personally use macros to add help messages to my zsh functions. It's as easy as:
|
This comment has been minimized.
This comment has been minimized.
Awesome idea, thanks very much! |
This comment has been minimized.
This comment has been minimized.
Very cool. |
This comment has been minimized.
This comment has been minimized.
That's a good idea. Awesome! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Glad someone proved this is a good strategy to write helper message, I also did it in my commonly-used script: what I did is simply print the comment at the top(except the first line to
|
This comment has been minimized.
This comment has been minimized.
666 |
This comment has been minimized.
This comment has been minimized.
Why even use the extended version?
and |
This comment has been minimized.
This. Is. Perfect.😍