Files
gist/snippets/bash/show_help.sh
Max P. e9f8be5b2b
All checks were successful
Auto Changelog & (Release) / release (push) Successful in 6s
style(bash): remove unnecessary space stripping in help output
- Simplifies the sed command by removing redundant space trimming.
2025-07-02 20:55:37 +02:00

53 lines
1.5 KiB
Bash

#!/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