From 8fdd6048459bcc2c2d05e21b0ba7dd14420b5f29 Mon Sep 17 00:00:00 2001 From: Salt Date: Wed, 20 May 2020 12:18:02 -0500 Subject: [PATCH] te: Add flag to open editor after creation --- te | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/te b/te index f8ddd5b..2fc6cce 100755 --- a/te +++ b/te @@ -11,6 +11,7 @@ set -e # Read-only set-once variables declare -r _name="$(basename -- "$0")" # Options +declare -i _optedit declare -i _optforce declare -i _opthelp declare -i _optverbose @@ -140,6 +141,8 @@ printhelp() { Usage: $_name [OPTION]... [FILENAME]... Template out a file based on its extension + -e Open \$EDITOR on file after creation. Only works with + exactly one argument. -f Use force, clobber when necessary -h Print this help text -v Print more status messages. Stacks @@ -186,6 +189,13 @@ createtemplates() { local perms="$(stat -c '%a' $_return)" log "Applying perms: $perms" 2 chmod "$perms" "$arg" + # If we're supposed to open the editor, do it now + if (( _optedit > 0 )); then + if ! has "$EDITOR"; then + error "Could not find editor: $EDITOR" 3 + fi + exec "$EDITOR" "$arg" + fi else case $_return in fail) @@ -204,8 +214,11 @@ main() { # Parse out arguments while [ -n "$1" ]; do # Parse out flags - while getopts ":fhv" opt; do + while getopts ":efhv" opt; do case $opt in + e) + _optedit=1 + ;; f) _optforce+=1 ;; @@ -233,10 +246,17 @@ main() { done # Early hook for help [ -n "$_opthelp" ] && printhelp && exit 0 + # Validate critical options + if (( _optedit > 0 )) && (( ${#_args[@]} > 1 )); then + error "Option -e only works with one argument" 1 + fi + if [ "${#_args[@]}" == "0" ]; then + error "Missing required operand, see \"$_name -h\"" 1 + fi # Validate core program dependencies log "Validating dependencies" 2 if ! has basename chmod cp find readlink; then - error "Failed to find program: $_return" 1 + error "Failed to find program: $_return" 2 fi # Do the do