Compare commits
2 Commits
e2c0294a5b
...
e94fdbf837
Author | SHA1 | Date | |
---|---|---|---|
e94fdbf837
|
|||
65e4092af2
|
52
snippets/bash/show_help.sh
Normal file
52
snippets/bash/show_help.sh
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# show_help.sh – Example Bash script with embedded help section
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
# Use this snippet as a pattern for Bash scripts that include their own help text
|
||||||
|
# inside comment blocks. The `show_help` function will extract the lines between
|
||||||
|
# `#=== HELP START ===` and `#=== HELP END ===`, strip the leading `#` and output
|
||||||
|
# a clean help message.
|
||||||
|
|
||||||
|
# ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
show_help() {
|
||||||
|
sed -n '/^#=== HELP START ===/,/^#=== HELP END ===/ {
|
||||||
|
/^#=== HELP START ===/d
|
||||||
|
/^#=== HELP END ===/d
|
||||||
|
s/^# *//
|
||||||
|
p
|
||||||
|
}' "$0"
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- Example usage of the show_help function ---
|
||||||
|
|
||||||
|
#=== HELP START ===
|
||||||
|
# Example Script
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# ./show_help.sh [options]
|
||||||
|
#
|
||||||
|
# Options:
|
||||||
|
# -h, --help Show this help message and exit
|
||||||
|
# -v, --verbose Enable verbose output
|
||||||
|
#
|
||||||
|
# Description:
|
||||||
|
# This is an example of how to embed a self-contained help
|
||||||
|
# section in a Bash script using a simple sed-based extractor.
|
||||||
|
#=== HELP END ===
|
||||||
|
|
||||||
|
# ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
# Main entrypoint
|
||||||
|
case "$1" in
|
||||||
|
-h|--help)
|
||||||
|
show_help
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-v|--verbose)
|
||||||
|
echo "Verbose mode enabled"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Run with --help for usage."
|
||||||
|
;;
|
||||||
|
esac
|
Reference in New Issue
Block a user