Files
gist/snippets/bash/show_help.sh
Max P. e94fdbf837
All checks were successful
Auto Changelog & (Release) / release (push) Successful in 7s
feat(bash): add self-contained help extraction script
- Introduces a Bash script showcasing embedded help text extraction
- Implements a `show_help` function utilizing `sed` for parsing comments
- Supports `--help` and `--verbose` options for enhanced usability
2025-07-02 20:55:05 +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