From c37792e80f73ee46d50132f3048cd42cbd32b6fe Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sun, 10 Jul 2022 23:15:44 -0300 Subject: bin/prompt: Add initial working version --- bin/prompt | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 bin/prompt (limited to 'bin/prompt') diff --git a/bin/prompt b/bin/prompt new file mode 100755 index 0000000..a191238 --- /dev/null +++ b/bin/prompt @@ -0,0 +1,75 @@ +#!/bin/sh +set -eu + +usage() { + cat <<-'EOF' + Usage: + prompt STRING|- + prompt -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -h, --help show this message + + STRING the text to be displayed in the prompt + + + Display a prompt and return a value corresponding to the + response. + EOF +} + +assert_arg() { + if [ -z "$1" ]; then + printf 'Missing %s.\n\n' "$2" >&2 + usage >&2 + exit 2 + fi +} + + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + +STRING="${1:-}" +assert_arg "$STRING" 'STRING' + +printf '%s' "$STRING" +printf ' [Y/n]: ' +read -r yesno +if [ "$yesno" != 'n' ] && [ "$yesno" != 'N' ]; then + exit 0 +else + exit 1 +fi -- cgit v1.2.3 From a0188f38d8a9702dd3d7119375173d063a5f28bc Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sat, 13 Aug 2022 10:58:10 -0300 Subject: bin/: Replace usage of assert_arg with assert-arg --- bin/email | 15 ++------------- bin/grun | 12 ++---------- bin/msg | 16 ++++------------ bin/pre | 10 +--------- bin/prompt | 10 +--------- bin/rfc | 10 +--------- bin/without-env | 13 +++---------- 7 files changed, 14 insertions(+), 72 deletions(-) (limited to 'bin/prompt') diff --git a/bin/email b/bin/email index 3c72086..c16ed62 100755 --- a/bin/email +++ b/bin/email @@ -64,19 +64,8 @@ while getopts 's:h' flag; do done shift $((OPTIND - 1)) -assert_arg() { - if [ -z "$1" ]; then - { - printf 'Missing %s.\n' "$2" - printf '\n' - usage - } >&2 - exit 2 - fi -} - -assert_arg "${SUBJECT:-}" '-s SUBJECT' -assert_arg "${1:-}" 'ADDRESS' +eval "$(assert-arg "${SUBJECT:-}" '-s SUBJECT')" +eval "$(assert-arg "${1:-}" 'ADDRESS')" printf 'Subject: %s\n\n%s' \ "$(echo "$SUBJECT" | tr -d '\n')" \ diff --git a/bin/grun b/bin/grun index cfd6b52..982420a 100755 --- a/bin/grun +++ b/bin/grun @@ -71,16 +71,8 @@ while getopts 'rh' flag; do done shift $((OPTIND - 1)) -assert_arg() { - if [ -z "$1" ]; then - printf 'Missing %s\n\n' "$2" >&2 - usage >&2 - exit 2 - fi -} - FILENAME="${1:-}" -assert_arg "$FILENAME" 'FILENAME' +eval "$(assert-arg "$FILENAME" 'FILENAME')" shift if [ "${1:-}" != '--' ]; then @@ -90,7 +82,7 @@ if [ "${1:-}" != '--' ]; then fi shift -assert_arg "${1:-}" 'COMMAND' +eval "$(assert-arg "${1:-}" 'COMMAND')" if [ ! -e "$FILENAME" ]; then diff --git a/bin/msg b/bin/msg index ad0b608..f685ffb 100755 --- a/bin/msg +++ b/bin/msg @@ -124,31 +124,23 @@ fi MESSAGE="${1:-}" -assert_arg() { - if [ -z "$1" ]; then - printf 'Missing %s.\n\n' "$2" >&2 - usage >&2 - exit 2 - fi -} - if [ "$XMPP" = true ]; then - assert_arg "$MESSAGE" '-X MESSAGE' + eval "$(assert-arg "$MESSAGE" '-X MESSAGE')" xmpp -m "$MESSAGE" eu@euandreh.xyz & fi if [ "$SOUND" = true ]; then sound & fi if [ "$SPEAK" = true ]; then - assert_arg "$MESSAGE" '-S MESSAGE' + eval "$(assert-arg "$MESSAGE" '-S MESSAGE')" echo "$MESSAGE" | speak -v pt-BR & fi if [ "$MAIL" = true ]; then - assert_arg "$MESSAGE" '-m MESSAGE' + eval "$(assert-arg "$MESSAGE" '-m MESSAGE')" echo " " | email -s "$MESSAGE" eu@euandre.org & fi if [ "$DESKTOP" = true ]; then - assert_arg "$MESSAGE" '-D MESSAGE' + eval "$(assert-arg "$MESSAGE" '-D MESSAGE')" if [ "$OK" = true ]; then notify-send -t 5000 "$MESSAGE" & else diff --git a/bin/pre b/bin/pre index 233ac1a..79c212f 100755 --- a/bin/pre +++ b/bin/pre @@ -52,16 +52,8 @@ while getopts 'c:h' flag; do done shift $((OPTIND - 1)) -assert_arg() { - if [ -z "$1" ]; then - printf 'Missing %s\n\n' "$2" >&2 - usage >&2 - exit 2 - fi -} - PREFIX="${1:-}" -assert_arg "$PREFIX" 'PREFIX' +eval "$(assert-arg "$PREFIX" 'PREFIX')" while read -r line; do if [ -z "$COLOR" ]; then diff --git a/bin/prompt b/bin/prompt index a191238..68dff61 100755 --- a/bin/prompt +++ b/bin/prompt @@ -23,14 +23,6 @@ help() { EOF } -assert_arg() { - if [ -z "$1" ]; then - printf 'Missing %s.\n\n' "$2" >&2 - usage >&2 - exit 2 - fi -} - for flag in "$@"; do case "$flag" in @@ -63,7 +55,7 @@ done shift $((OPTIND - 1)) STRING="${1:-}" -assert_arg "$STRING" 'STRING' +eval "$(assert-arg "$STRING" 'STRING')" printf '%s' "$STRING" printf ' [Y/n]: ' diff --git a/bin/rfc b/bin/rfc index e0da425..bd8fb67 100755 --- a/bin/rfc +++ b/bin/rfc @@ -86,14 +86,6 @@ check_local_db() { fi } -assert_arg() { - if [ -z "$1" ]; then - printf 'Missing %s.\n\n' "$2" >&2 - usage >&2 - exit 2 - fi -} - for flag in "$@"; do case "$flag" in @@ -141,7 +133,7 @@ if [ "${UPDATE:-}" = true ]; then exit fi -assert_arg "$RFC_NUMBER" +eval "$(assert-arg "$RFC_NUMBER")" if [ ! -e "$F" ]; then printf 'Given RFC_NUMBER "%s" does not exist at:\n%s\n' \ diff --git a/bin/without-env b/bin/without-env index 45c41ca..4f8fb14 100755 --- a/bin/without-env +++ b/bin/without-env @@ -57,16 +57,9 @@ while getopts 'h' flag; do done shift $((OPTIND - 1)) -assert_arg() { - if [ -z "$1" ]; then - printf 'Missing %s\n' "$2" >&2 - exit 2 - fi -} - -assert_arg "${1:-}" 'ENVVAR' -assert_arg "${2:-}" 'PATH' -assert_arg "${3:-}" '--' +eval "$(assert-arg "${1:-}" 'ENVVAR')" +eval "$(assert-arg "${2:-}" 'PATH')" +eval "$(assert-arg "${3:-}" '--')" eval "export $1=\"\$(echo \"\$$1\" | sed \"s|\$2:||g\")\"" shift # drop $1 -- cgit v1.2.3 From a2324725adab4fda610d39eb21d8d28bdf8f35ba Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sat, 13 Aug 2022 13:17:45 -0300 Subject: bin/: Add examples to help strings --- bin/gc | 10 ++++++++++ bin/mkdtemp | 40 ++++++++++++++++++++++++++++++++++++++++ bin/mkstemp | 40 ++++++++++++++++++++++++++++++++++++++++ bin/pre | 14 ++++++++++++++ bin/prompt | 9 +++++++++ bin/qr | 13 +++++++++++++ bin/tmp | 15 +++++++++++++++ bin/tmpname | 14 ++++++++++++++ bin/uuid | 9 +++++++++ bin/xmpp | 12 +++++++++++- 10 files changed, 175 insertions(+), 1 deletion(-) (limited to 'bin/prompt') diff --git a/bin/gc b/bin/gc index ec6a8f1..23a0436 100755 --- a/bin/gc +++ b/bin/gc @@ -14,6 +14,16 @@ help() { Options: -h, --help show this message + + + Free disk space system-wide. + + + Examples: + + Just run it: + + $ gc EOF } diff --git a/bin/mkdtemp b/bin/mkdtemp index d012175..3729dd4 100755 --- a/bin/mkdtemp +++ b/bin/mkdtemp @@ -15,10 +15,50 @@ help() { Options: -h, --help show this message + Create a new temporary file and echo its name back. + + + Examples: + + `cd` into temporary directory: + + $ cd "$(mkdtemp)" EOF } + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + + name="$(tmpname)" mkdir "$name" echo "$name" diff --git a/bin/mkstemp b/bin/mkstemp index ec92c14..4097e59 100755 --- a/bin/mkstemp +++ b/bin/mkstemp @@ -15,10 +15,50 @@ help() { Options: -h, --help show this message + Create a new temporary file and echo its name back. + + + Examples: + + Capture output into temporary file: + + $ OUT="$(mkstemp)"; cmd > "$OUT" EOF } + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + + name="$(tmpname)" touch "$name" echo "$name" diff --git a/bin/pre b/bin/pre index 79c212f..2b32f8f 100755 --- a/bin/pre +++ b/bin/pre @@ -15,6 +15,20 @@ help() { Options: -c COLOR ANSI color to be used on the prefix text -h, --help show this message + + Prefix STDIN with PREFIX. + + + Examples: + + Prefix with 'database': + + $ ./run-db.sh | pre 'database' + + + Prefix with yellow 'numbers': + + $ seq 10 | pre -c yellow numbers EOF } diff --git a/bin/prompt b/bin/prompt index 68dff61..247c81a 100755 --- a/bin/prompt +++ b/bin/prompt @@ -20,6 +20,15 @@ help() { Display a prompt and return a value corresponding to the response. + + + Examples: + + Conditionally run download command + + if prompt 'Download files?'; then + run_download; + fi EOF } diff --git a/bin/qr b/bin/qr index 4821a46..c0462e1 100755 --- a/bin/qr +++ b/bin/qr @@ -16,7 +16,20 @@ help() { -s PIXEL_SIZE size of the pixel (default 10) -h, --help show this help message + Read data from STDIN and present a QR image with said data. + + + Examples: + + Link to my homepage: + + $ printf 'https://euandre.org' | qr + + + Numbers with a smaller pixel size: + + $ seq 99 | qr -s 5 EOF } diff --git a/bin/tmp b/bin/tmp index e2d16b7..2dc0b48 100755 --- a/bin/tmp +++ b/bin/tmp @@ -16,6 +16,21 @@ help() { Options: -d delete the remote "tmp/" folder -h, --help show this message + + + Copies a file to the public server. + + + Examples: + + Copy f.txt: + + $ tmp f.txt + + + Cleanup the $REMOTE: + + $ tmp -d EOF } diff --git a/bin/tmpname b/bin/tmpname index d83fc87..89d7e4d 100755 --- a/bin/tmpname +++ b/bin/tmpname @@ -15,10 +15,24 @@ help() { Options: -h, --help show this message + Generate a temporary name. + + + Examples: + + Create a temporary file: + + $ OUT="$(tmpname)"; touch "$OUT"; cmd > "$OUT" + + + `cd` into a temporary directory: + + $ DIR="$(tmpname)"; mkdir -p "$DIR"; cd "$DIR" EOF } + for flag in "$@"; do case "$flag" in --) diff --git a/bin/uuid b/bin/uuid index 74d0fba..34b685f 100755 --- a/bin/uuid +++ b/bin/uuid @@ -15,7 +15,16 @@ help() { Options: -h, --help show this message + Generate UUID from /dev/random. + + + Examples: + + Generate a UUID: + + $ uuid + 755244c8-f955-16df-75cc-f25600c90422 EOF } diff --git a/bin/xmpp b/bin/xmpp index 472e9ca..b08a783 100755 --- a/bin/xmpp +++ b/bin/xmpp @@ -18,7 +18,17 @@ Options: -h, --help show this message FROM_JID the address used to send the message from - TO_JID the addresses where to send the message to""" + TO_JID the addresses where to send the message to + + +Send a one-off XMPP message. + + +Examples: + + Send a message to eu@euandreh.xyz: + + $ xmpp -m 'Hello, XMPP!' eu@euandreh.xyz""" class SendMsgBot(slixmpp.ClientXMPP): def __init__(self, jid, password, on_start): -- cgit v1.2.3