diff --git a/profile.d/processes.sh b/profile.d/processes.sh index 75ec278..1e89085 100644 --- a/profile.d/processes.sh +++ b/profile.d/processes.sh @@ -39,13 +39,36 @@ # Usage: ppg ppg() { - if [[ "$1" == "-h" || "$1" == "--help" ]]; then - printf "ppg: Search processes matching the given string.\n\n" - printf "Usage: ppg \n\n" - printf "Options:\n" - printf "\t-h, --help\t\tDisplay this help screen\n" - return 0 + local PARSED + + PARSED=$(getopt -o h --long help -n 'ppg' -- "$@") + # shellcheck disable=SC2181 # getopt return code is checked immediately after + if [[ $? -ne 0 ]]; then + disp E "Invalid options, use \"ppg --help\" to display usage." + return 1 fi + + eval set -- "$PARSED" + while true; do + case "$1" in + -h|--help) + printf "ppg: Search processes matching the given string.\n\n" + printf "Usage: ppg \n\n" + printf "Options:\n" + printf "\t-h, --help\t\tDisplay this help screen\n" + return 0 + ;; + --) + shift + break + ;; + *) + disp E "Invalid options, use \"ppg --help\" to display usage." + return 1 + ;; + esac + done + if [[ -z "$1" ]]; then disp E "Usage: ppg " return 1 @@ -81,13 +104,36 @@ export -f ppg # Usage: ppu ppu() { - if [[ "$1" == "-h" || "$1" == "--help" ]]; then - printf "ppu: List processes owned by a specific user.\n\n" - printf "Usage: ppu \n\n" - printf "Options:\n" - printf "\t-h, --help\t\tDisplay this help screen\n" - return 0 + local PARSED + + PARSED=$(getopt -o h --long help -n 'ppu' -- "$@") + # shellcheck disable=SC2181 # getopt return code is checked immediately after + if [[ $? -ne 0 ]]; then + disp E "Invalid options, use \"ppu --help\" to display usage." + return 1 fi + + eval set -- "$PARSED" + while true; do + case "$1" in + -h|--help) + printf "ppu: List processes owned by a specific user.\n\n" + printf "Usage: ppu \n\n" + printf "Options:\n" + printf "\t-h, --help\t\tDisplay this help screen\n" + return 0 + ;; + --) + shift + break + ;; + *) + disp E "Invalid options, use \"ppu --help\" to display usage." + return 1 + ;; + esac + done + if [[ -z "$1" ]]; then disp E "Usage: ppu " return 1 @@ -106,13 +152,36 @@ export -f ppu # Usage: ppn ppn() { - if [[ "$1" == "-h" || "$1" == "--help" ]]; then - printf "ppn: List processes by exact command name (no path/parameters).\n\n" - printf "Usage: ppn \n\n" - printf "Options:\n" - printf "\t-h, --help\t\tDisplay this help screen\n" - return 0 + local PARSED + + PARSED=$(getopt -o h --long help -n 'ppn' -- "$@") + # shellcheck disable=SC2181 # getopt return code is checked immediately after + if [[ $? -ne 0 ]]; then + disp E "Invalid options, use \"ppn --help\" to display usage." + return 1 fi + + eval set -- "$PARSED" + while true; do + case "$1" in + -h|--help) + printf "ppn: List processes by exact command name (no path/parameters).\n\n" + printf "Usage: ppn \n\n" + printf "Options:\n" + printf "\t-h, --help\t\tDisplay this help screen\n" + return 0 + ;; + --) + shift + break + ;; + *) + disp E "Invalid options, use \"ppn --help\" to display usage." + return 1 + ;; + esac + done + if [[ -z "$1" ]]; then disp E "Usage: ppn " return 1 @@ -133,13 +202,36 @@ export -f ppn # Usage: ppid gpid() { - if [[ "$1" == "-h" || "$1" == "--help" ]]; then - printf "gpid: Get PID list of the given process name.\n\n" - printf "Usage: gpid \n\n" - printf "Options:\n" - printf "\t-h, --help\t\tDisplay this help screen\n" - return 0 + local PARSED + + PARSED=$(getopt -o h --long help -n 'gpid' -- "$@") + # shellcheck disable=SC2181 # getopt return code is checked immediately after + if [[ $? -ne 0 ]]; then + disp E "Invalid options, use \"gpid --help\" to display usage." + return 1 fi + + eval set -- "$PARSED" + while true; do + case "$1" in + -h|--help) + printf "gpid: Get PID list of the given process name.\n\n" + printf "Usage: gpid \n\n" + printf "Options:\n" + printf "\t-h, --help\t\tDisplay this help screen\n" + return 0 + ;; + --) + shift + break + ;; + *) + disp E "Invalid options, use \"gpid --help\" to display usage." + return 1 + ;; + esac + done + if [[ -z "$1" ]]; then disp E "Usage: gpid " return 1 @@ -190,10 +282,19 @@ export -f gpid # Usage: ku ku() { + local PARSED local dry_run=0 local -a signal_opt=() - while [[ $# -gt 0 ]]; do + PARSED=$(getopt -o hns: --long help,dry-run,signal: -n 'ku' -- "$@") + # shellcheck disable=SC2181 # getopt return code is checked immediately after + if [[ $? -ne 0 ]]; then + disp E "Invalid options, use \"ku --help\" to display usage." + return 1 + fi + + eval set -- "$PARSED" + while true; do case "$1" in -h|--help) printf "ku: Kill all processes owned by the given users.\n\n" @@ -203,7 +304,7 @@ ku() printf "\t-n, --dry-run\t\tDisplay commands without executing them\n" printf "\t-s, --signal SIG\tSignal to send (overrides KU_DEFAULT_SIGNAL)\n" printf "\t --signal=SIG\tSame as above\n" - printf "\t -SIG / -NUM\tSignal format compatible with kill\n" + printf "\t -SIG / -NUM\t\tSignal format compatible with kill\n" return 0 ;; -n|--dry-run) @@ -211,21 +312,26 @@ ku() shift ;; -s|--signal) - if [[ -z "${2:-}" || "$2" == -* ]]; then - disp E "--signal requires a value." - return 1 - fi signal_opt=(-s "$2") shift 2 ;; - --signal=*) - if [[ -z "${1#*=}" ]]; then - disp E "--signal requires a value." - return 1 - fi - signal_opt=(-s "${1#*=}") + --) + shift + break + ;; + -*) + signal_opt=("$1") shift ;; + *) + break + ;; + esac + done + + # Accept kill-style signal forms not handled by getopt, before usernames. + while [[ $# -gt 0 ]]; do + case "$1" in -[0-9]*|-SIG*|-[[:alpha:]]*) signal_opt=("$1") shift @@ -284,10 +390,19 @@ export -f ku # Usage: kt [kill_options] kt() { + local PARSED local dry_run=0 local -a pre_kill_opts=() - while [[ $# -gt 0 ]]; do + PARSED=$(getopt -o hns: --long help,dry-run,signal: -n 'kt' -- "$@") + # shellcheck disable=SC2181 # getopt return code is checked immediately after + if [[ $? -ne 0 ]]; then + disp E "Invalid options, use \"kt --help\" to display usage." + return 1 + fi + + eval set -- "$PARSED" + while true; do case "$1" in -h|--help) printf "kt: Kill all children of a process then the process (kill tree).\n\n" @@ -297,7 +412,7 @@ kt() printf "\t-n, --dry-run\t\tDisplay kill commands without executing them\n" printf "\t-s, --signal SIG\tSignal to send to process tree\n" printf "\t --signal=SIG\tSame as above\n" - printf "\t -SIG / -NUM\tSignal format compatible with kill\n" + printf "\t -SIG / -NUM\t\tSignal format compatible with kill\n" return 0 ;; -n|--dry-run) @@ -305,21 +420,26 @@ kt() shift ;; -s|--signal) - if [[ -z "${2:-}" || "$2" == -* ]]; then - disp E "--signal requires a value." - return 1 - fi pre_kill_opts+=(-s "$2") shift 2 ;; - --signal=*) - if [[ -z "${1#*=}" ]]; then - disp E "--signal requires a value." - return 1 - fi - pre_kill_opts+=(-s "${1#*=}") + --) + shift + break + ;; + -*) + pre_kill_opts+=("$1") shift ;; + *) + break + ;; + esac + done + + # Accept kill-style signal forms not handled by getopt, before the PID. + while [[ $# -gt 0 ]]; do + case "$1" in -[0-9]*|-SIG*|-[[:alpha:]]*) pre_kill_opts+=("$1") shift @@ -328,6 +448,10 @@ kt() shift break ;; + -*) + disp E "Unknown option: $1, use \"kt --help\" to display usage." + return 1 + ;; *) break ;;