From 0976603694f0fb1bf078260317d78c6c8194b4ae Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 7 Jan 2017 20:47:37 +1300 Subject: Improvements to path() * Move common directory argument checking into helper function * Tolerate only one directory argument * Show subcommand in error output * Don't show help on failed commands, just suggest it * (Technicality) Fix trailing-newline lossage in subshell function --- sh/shrc.d/path.sh | 55 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 16 deletions(-) (limited to 'sh') diff --git a/sh/shrc.d/path.sh b/sh/shrc.d/path.sh index 3caabbc9..23eec75b 100644 --- a/sh/shrc.d/path.sh +++ b/sh/shrc.d/path.sh @@ -24,10 +24,28 @@ path() { done ) ;; + # Helper function checks directory argument makes sense + _argcheck) + shift + if [ "$#" -gt 2 ] ; then + printf >&2 'path(): %s: too many arguments\n' "$1" + return 2 + fi + case $2 in + *:*) + printf >&2 'path(): %s: %s contains colon\n' "$@" + return 2 + ;; + esac + return 0 + ;; + # Add a directory at the start of $PATH insert) + [ "$#" -eq 2 ] || set -- "$1" "$PWD" + path _argcheck "$@" || return if path check "$2" ; then - printf >&2 'path(): %s already in PATH\n' "$2" + printf >&2 'path(): %s: %s already in PATH\n' "$@" return 1 fi PATH=${2}${PATH:+:"$PATH"} @@ -35,8 +53,10 @@ path() { # Add a directory to the end of $PATH append) + [ "$#" -eq 2 ] || set -- "$1" "$PWD" + path _argcheck "$@" || return if path check "$2" ; then - printf >&2 'path(): %s already in PATH\n' "$2" + printf >&2 'path(): %s: %s already in PATH\n' "$@" return 1 fi PATH=${PATH:+"$PATH":}${2} @@ -44,21 +64,25 @@ path() { # Remove a directory from $PATH remove) + [ "$#" -eq 2 ] || set -- "$1" "$PWD" + path _argcheck "$@" || return if ! path check "$2" ; then - printf >&2 'path(): %s not in PATH\n' "$2" + printf >&2 'path(): %s: %s not in PATH\n' "$@" return 1 fi PATH=$( path=:$PATH: path=${path%%:"$2":*}:${path#*:"$2":} path=${path#:} - path=${path%:} - printf '%s\n' "$path" + printf '%s:' "$path" ) + PATH=${PATH%%:} ;; # Check whether a directory is in PATH check) + path _argcheck "$@" || return + [ "$#" -eq 2 ] || set -- "$1" "$PWD" case :$PATH: in *:"$2":*) return 0 ;; esac @@ -73,23 +97,22 @@ path(): Manage contents of PATH variable USAGE: path [list] Print the current directories in PATH, one per line (default command) - path insert DIR - Add a directory to the front of PATH - path append DIR - Add a directory to the end of PATH - path remove DIR - Remove directory from PATH - path check DIR - Return whether DIR is a component of PATH + path insert [DIR] + Add directory DIR (default $PWD) to the front of PATH + path append [DIR] + Add directory DIR (default $PWD) to the end of PATH + path remove [DIR] + Remove directory DIR (default $PWD) from PATH + path check [DIR] + Return whether directory DIR (default $PWD) is a component of PATH path help - Print this help message (also done if command not found) + Print this help message EOF ;; # Command not found *) - printf >&2 'path(): Unknown command\n' - path help >&2 + printf >&2 'path(): %s: Unknown command (try "help")\n' "$1" return 2 ;; esac -- cgit v1.2.3