Add README.md generation, closes #4
This commit is contained in:
parent
33df3371f9
commit
8e5dd5ea73
101
README.md
101
README.md
|
@ -1,4 +1,6 @@
|
|||
# Bash Command Template
|
||||
# Bash Template Command
|
||||
|
||||
*This `README.md` is [autogenerated](#generate-readme-md-file).*
|
||||
|
||||
This is a GitHub template repo that will be copied instead of forked to
|
||||
create a new Bash command with a command something like this:
|
||||
|
@ -7,16 +9,13 @@ create a new Bash command with a command something like this:
|
|||
gh repo create rwxrob/mycmd -p rwxrob/template-bash-command
|
||||
```
|
||||
|
||||
This `command` inside can then be renamed and finished.
|
||||
|
||||
Obviously, not all of this is needed for many Bash scripts, but anything
|
||||
with more than two subcommands will benefit from the builtin tab
|
||||
completion, embedded Markdown help documentation support, and included
|
||||
functions (`usage`, `_filter`, `_buffer`, `_have`, etc.)
|
||||
|
||||
## Installation
|
||||
|
||||
It's usually easiest to install by cloning the repo and adding the full
|
||||
path to this repo to your `PATH`. That way you can keep up with updates.
|
||||
|
||||
## Naming Conventions
|
||||
|
||||
* Name repos containing single bash commands with `cmd-`
|
||||
|
@ -24,52 +23,6 @@ path to this repo to your `PATH`. That way you can keep up with updates.
|
|||
* Start command functions with `command_` to be completed
|
||||
* Start command functions with `command__` to not be completed
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
command
|
||||
command usage
|
||||
command help [<cmd>]
|
||||
command foo [<arg>]
|
||||
command bar [<arg>]
|
||||
```
|
||||
|
||||
## Commands
|
||||
|
||||
| Command | Summary |
|
||||
| :-: | - |
|
||||
| [usage] | Display single line summary of all command usage |
|
||||
| [help] | Display help information |
|
||||
| [foo] | Do the foo |
|
||||
| [bar] | Do the bar |
|
||||
|
||||
[usage]: #usage
|
||||
[help]: #help
|
||||
[foo]: #foo
|
||||
[bar]: #bar
|
||||
|
||||
### `usage`
|
||||
|
||||
The `usage` command displays a summary of usage.
|
||||
|
||||
### `help`
|
||||
|
||||
The help command prints help information. If no argument is passed
|
||||
displays general help information (main). Otherwise, the documentation
|
||||
for the specific argument keyword is displayed, which usually
|
||||
corresponds to a command name (but not necessarily). All documentation
|
||||
is written in CommonMark (Markdown) and will displayed as Web page if
|
||||
`pandoc` and `$HELP_BROWSER` are detected, otherwise, just the Markdown is
|
||||
sent to `$PAGER` (default: `more`).
|
||||
|
||||
### `foo`
|
||||
|
||||
The `foo` command foos.
|
||||
|
||||
### `bar`
|
||||
|
||||
The `bar` command bars.
|
||||
|
||||
## Dependencies
|
||||
|
||||
Required:
|
||||
|
@ -105,5 +58,47 @@ more powerful, safer, flexible, and performant than POSIX shell or Zsh.
|
|||
|
||||
Copyright 2021 Rob Muhlestein <rob@rwx.gg>
|
||||
Released under Apache-2.0 License
|
||||
Please mention <https://youtube.com/rwxrob>
|
||||
Please mention <https://youtube.com/rwxrob>
|
||||
|
||||
----
|
||||
|
||||
## Commands
|
||||
|
||||
### The `bar` Command
|
||||
|
||||
Bars.
|
||||
|
||||
### The `foo` Command
|
||||
|
||||
Foos.
|
||||
|
||||
### Display Help Information
|
||||
|
||||
```
|
||||
help [<command>]
|
||||
```
|
||||
|
||||
Displays specific help information. If no argument is passed displays
|
||||
general help information (main). Otherwise, the documentation for the
|
||||
specific argument keyword is displayed, which usually corresponds to
|
||||
a command name (but not necessarily). All documentation is written in
|
||||
GitHub Flavored Markdown and will displayed as a web page if `pandoc`
|
||||
and `$HELP_BROWSER` are detected, otherwise, just the Markdown is sent
|
||||
to `$PAGER` (default: more).
|
||||
|
||||
Also see `readme` and `usage` commands.
|
||||
|
||||
### Generate `README.md` File
|
||||
|
||||
```
|
||||
command readme > README.md
|
||||
```
|
||||
|
||||
The `readme` command will output the embedded help documentation in raw
|
||||
GitHub Flavored Markdown suitable for use as a `README.md` file on
|
||||
GitHub or similar hosting service.
|
||||
|
||||
### The `usage` Command
|
||||
|
||||
Displays a summary of usage.
|
||||
|
||||
|
|
149
command
149
command
|
@ -1,11 +1,7 @@
|
|||
#!/usr/bin/bash
|
||||
# shellcheck disable=SC2016
|
||||
set -e
|
||||
export PATH="/usr/bin:/usr/local/bin"
|
||||
|
||||
# Copyright 2021 Rob Muhlestein <rob@rwx.gg>
|
||||
# Released under Apache-2.0 license
|
||||
# Please mention https://youtube.com/rwxrob
|
||||
# export PATH="/usr/bin:/usr/local/bin" # safer, if you can
|
||||
|
||||
(( BASH_VERSINFO[0] < 4 )) && echo "Bash 4+ required." && exit 1
|
||||
|
||||
|
@ -18,27 +14,78 @@ EXE="${0##*/}"
|
|||
declare -A help # associative arrays *require* declaration
|
||||
|
||||
help[main]='
|
||||
# Sample Bash Template Command
|
||||
# Bash Template Command
|
||||
|
||||
This `command` contains the scaffolding for Bash tab completion using
|
||||
the `complete -C foo foo` variation which allows scripts to complete
|
||||
themselves (rather than having another script somewhere to manage). To
|
||||
use it simply add a function with the additional command and add the
|
||||
name of it to the commands array declaration at the top of the script.
|
||||
Then add `complete -C foo foo` to your bashrc. Begin functions with
|
||||
`command_` to allow useful command names to be used that would otherwise
|
||||
conflict with existing system and bash keywords. Begin functions with
|
||||
`command__` when you do not want them to appear with tab completion, but
|
||||
still want them to be available, just hidden.'
|
||||
*This `README.md` is autogenerated.*
|
||||
|
||||
help[foo]='The `foo` command foos.'
|
||||
This is a GitHub template repo that will be copied instead of forked to
|
||||
create a new Bash command with a command something like this:
|
||||
|
||||
```
|
||||
gh repo create rwxrob/mycmd -p rwxrob/template-bash-command
|
||||
```
|
||||
|
||||
This `command` inside can then be renamed and finished.
|
||||
|
||||
Obviously, not all of this is needed for many Bash scripts, but anything
|
||||
with more than two subcommands will benefit from the builtin tab
|
||||
completion, embedded Markdown help documentation support, and included
|
||||
functions (`usage`, `_filter`, `_buffer`, `_have`, etc.)
|
||||
|
||||
## Naming Conventions
|
||||
|
||||
* Name repos containing single bash commands with `cmd-`
|
||||
* Name template repos beginning with `template-`
|
||||
* Start command functions with `command_` to be completed
|
||||
* Start command functions with `command__` to not be completed
|
||||
|
||||
## Dependencies
|
||||
|
||||
Required:
|
||||
|
||||
* Bash 4+
|
||||
|
||||
Optional:
|
||||
|
||||
* `pandoc` - for rich help docs
|
||||
|
||||
## Justification
|
||||
|
||||
Bash is the dominate shell scripting language and the official default
|
||||
Linux interactive shell, which reduces cognitive overhead; every command
|
||||
line *is* a line of code that could be put into script as is. Bash
|
||||
scripts are at the core of cloud, containers, and Kubernetes. Bash 4+
|
||||
with its associative array support, powerful regular expressions, and
|
||||
multiple ways of feeding data to loops easily covers the needs
|
||||
previously requiring Python and Perl scripts. Bash scripts are also much
|
||||
more powerful, safer, flexible, and performant than POSIX shell or Zsh.
|
||||
|
||||
## Caveats
|
||||
|
||||
* Use the official bash path: `#!/usr/bin/bash`
|
||||
* Using `#!/usr/bin/env bash` introduces unnecessary risk
|
||||
* Always set the acceptable `PATH` at beginning of script
|
||||
* Always check script with [`shellcheck`] before releasing
|
||||
* Always use `bc` for *any* floating point math
|
||||
|
||||
[`shellcheck`]: <https://www.shellcheck.net>
|
||||
|
||||
## Legal
|
||||
|
||||
Copyright 2021 Rob Muhlestein <rob@rwx.gg>
|
||||
Released under Apache-2.0 License
|
||||
Please mention <https://youtube.com/rwxrob>
|
||||
|
||||
'
|
||||
|
||||
help[foo]='Foos.'
|
||||
|
||||
command_foo() {
|
||||
_filter "$@" && return $?
|
||||
echo "would foo: $*"
|
||||
}
|
||||
|
||||
help[bar]='The `bar` command bars.'
|
||||
help[bar]='Bars.'
|
||||
|
||||
command_bar() {
|
||||
_buffer "$@" && return $?
|
||||
|
@ -50,7 +97,7 @@ command__hidden() {
|
|||
echo "would run _hidden: $*"
|
||||
}
|
||||
|
||||
help[usage]='The `usage` command displays a summary of usage.'
|
||||
help[usage]='Displays a summary of usage.'
|
||||
|
||||
command_usage() {
|
||||
local -a cmds
|
||||
|
@ -63,27 +110,25 @@ command_usage() {
|
|||
}
|
||||
|
||||
help[help]='
|
||||
# The `help` Command
|
||||
# Display Help Information
|
||||
|
||||
The `help` command prints help information. If no argument is passed
|
||||
displays general help information (main). Otherwise, the documentation
|
||||
for the specific argument keyword is displayed, which usually
|
||||
corresponds to a command name (but not necessarily). All documentation
|
||||
is written in CommonMark (Markdown) and will displayed as Web page if
|
||||
`pandoc` and `$HELP_BROWSER` are detected, otherwise, just the Markdown is
|
||||
sent to `$PAGER` (default: more).'
|
||||
```
|
||||
help [<command>]
|
||||
```
|
||||
|
||||
Displays specific help information. If no argument is passed displays
|
||||
general help information (main). Otherwise, the documentation for the
|
||||
specific argument keyword is displayed, which usually corresponds to
|
||||
a command name (but not necessarily). All documentation is written in
|
||||
GitHub Flavored Markdown and will displayed as a web page if `pandoc`
|
||||
and `$HELP_BROWSER` are detected, otherwise, just the Markdown is sent
|
||||
to `$PAGER` (default: more).
|
||||
|
||||
Also see `readme` and `usage` commands.
|
||||
'
|
||||
|
||||
command_help() {
|
||||
local name="$1"
|
||||
if [[ -z "$name" ]];then
|
||||
for c in "${COMMANDS[@]}";do
|
||||
[[ ${c:0:1} = _ ]] && continue;
|
||||
command_help "$c" buildonly || true
|
||||
done
|
||||
command_help main
|
||||
return 0
|
||||
fi
|
||||
local title own body
|
||||
local name="${1:-main}" title own body
|
||||
title=$(_help_title "$name")
|
||||
if [[ -z "$title" ]]; then
|
||||
body="${help[$name]}"
|
||||
|
@ -109,6 +154,30 @@ command_help() {
|
|||
echo -e "$title\n\n$body" | "$PAGER"
|
||||
}
|
||||
|
||||
help[readme]='
|
||||
# Generate `README.md` File
|
||||
|
||||
```
|
||||
command readme > README.md
|
||||
```
|
||||
|
||||
The `readme` command will output the embedded help documentation in raw
|
||||
GitHub Flavored Markdown suitable for use as a `README.md` file on
|
||||
GitHub or similar hosting service. '
|
||||
|
||||
command_readme() {
|
||||
_trim "${help[main]}"
|
||||
local usage="$(command_usage)"
|
||||
printf "\n----\n\n## Commands\n\n"
|
||||
local -a names=("${!help[@]}")
|
||||
while IFS= read -r name; do
|
||||
[[ $name = main ]] && continue
|
||||
local body=$(_trim "${help[$name]}")
|
||||
[[ $body =~ ^\# ]] || body="# The \`$name\` Command\n\n$body"
|
||||
printf "##$body\n\n"
|
||||
done < <(printf "%s\n" "${!help[@]}" | LC_COLLATE=C sort)
|
||||
}
|
||||
|
||||
# --------------------- completion and delegation --------------------
|
||||
# (better than . <(foo bloated_completion) in .bashrc)
|
||||
|
||||
|
@ -122,6 +191,11 @@ _help_title() {
|
|||
done <<< "${help[$name]}"
|
||||
}
|
||||
|
||||
_trim() {
|
||||
local it="${1#"${1%%[![:space:]]*}"}"
|
||||
echo -e "${it%"${it##*[![:space:]]}"}"
|
||||
}
|
||||
|
||||
_have(){ type "$1" &>/dev/null; }
|
||||
|
||||
_filter(){
|
||||
|
@ -140,6 +214,7 @@ while IFS= read -r line; do
|
|||
[[ $line =~ ^declare\ -f\ command_ ]] || continue
|
||||
COMMANDS+=( "${line##declare -f command_}" )
|
||||
done < <(declare -F)
|
||||
COMMANDS=($(LC_COLLATE=C sort < <(printf "%s\n" "${COMMANDS[@]}")))
|
||||
|
||||
if [[ -n $COMP_LINE ]]; then
|
||||
line=${COMP_LINE#* }
|
||||
|
|
Loading…
Reference in New Issue