Browse Source

Manager: Improve option handling using GNU getopt

master
Riyyi 3 years ago
parent
commit
ec85c0aff6
  1. 14
      .local/completion/_dotfiles.sh
  2. 65
      dotfiles.sh

14
.local/completion/_dotfiles.sh

@ -5,20 +5,20 @@ _dotfiles.sh() {
local -a args local -a args
args+=( args+=(
'(- *)'{-a,--add}'[add file to the dotfiles directory]:file:_files' '*'{-a,--add=}'[add file to the dotfiles directory]:file:_files'
'(- *)'{-f,--files}'[display all files added to the dotfiles directory]' '(-f --files)'{-f,--files}'[display all files added to the dotfiles directory]'
'(- *)'{-h,--help}'[display usage message and exit]' '(-)'{-h,--help}'[display usage message and exit]'
'(- *)'{-p,--packages}'[instal, list or store packages]:packages:(( '(-p --packages)'{-p,--packages=}'[instal, list or store packages]:package functions:((
install\:"install all core packages of the stored list" install\:"install all core packages of the stored list"
install-aur\:"install all AUR packages of the stored list" install-aur\:"install all AUR packages of the stored list"
list\:"display all packages installed on the system (default)" list\:"display all packages installed on the system (default)"
store\:"stores a list of all installed packages" store\:"stores a list of all installed packages"
))' ))'
'(- *)'{-l,--pull}'[pull each added file from system to dotfiles directory]' '(-l --pull)'{-l,--pull}'[pull each added file from system to dotfiles directory]'
'(- *)'{-s,--push}'[push each added file to its location on the system]' '(-s --push)'{-s,--push}'[push each added file to its location on the system]'
) )
_arguments $args[@] && ret=0 _arguments -s -S $args[@] && ret=0
return ret return ret
} }

65
dotfiles.sh

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# Manages dotfiles and packages # Manages dotfiles and packages
# Depends: pacman-contrib # Depends: GNU getopt, pacman-contrib
# User-config--------------------------- # User-config---------------------------
@ -20,7 +20,7 @@ AUR_HELPER="trizen"
# -------------------------------------- # --------------------------------------
if [ "$(dirname "$0")" != "." ]; then if [ "$(dirname "$0")" != "." ]; then
echo "Please run this script from the directory it resides." echo "Please run this script from the directory it resides." >&2
exit 1 exit 1
fi fi
@ -37,7 +37,7 @@ ${B}SYNOPSIS${N}
${B}./dotfiles.sh${N} ${U}OPTION${N} [${U}ARG${N}] ${B}./dotfiles.sh${N} ${U}OPTION${N} [${U}ARG${N}]
${B}OPTIONS${N} ${B}OPTIONS${N}
${B}-a${N} ${U}FILE${N}, ${B}--add${N} ${U}FILE${N} ${B}-a${N} ${U}FILE${N}, ${B}--add${N}=${U}FILE${N}
Add file to the dotfiles directory. Add file to the dotfiles directory.
${B}-f, --files${N} ${B}-f, --files${N}
@ -46,7 +46,7 @@ ${B}OPTIONS${N}
${B}-h, --help${N} ${B}-h, --help${N}
Display usage message and exit. Display usage message and exit.
${B}-p${N} [${U}FUNCTION${N}], ${B}--packages${N} [${U}FUNCTION${N}] ${B}-p${N} [${U}FUNCTION${N}], ${B}--packages${N}=[${U}FUNCTION${N}]
Apply ${U}FUNCTION${N} to the package manager packages. Apply ${U}FUNCTION${N} to the package manager packages.
${U}install${N} Install all core packages of the stored list. ${U}install${N} Install all core packages of the stored list.
@ -68,7 +68,7 @@ EOF
} }
# Exit if no option is provided # Exit if no option is provided
[ "$#" -eq 0 ] && help && exit 1 [ "$#" -eq 0 ] && help >&2 && exit 1
set_files() { set_files() {
FILES="$(find . -type f -o -type l \ FILES="$(find . -type f -o -type l \
@ -148,7 +148,7 @@ push() {
packages() { packages() {
if ! pacman -Qqs pacman-contrib > /dev/null; then \ if ! pacman -Qqs pacman-contrib > /dev/null; then \
echo 'Please install the "pacman-contrib" dependency before running this option.' echo 'Please install the "pacman-contrib" dependency before running this option.' >&2
exit 1 exit 1
fi fi
@ -182,60 +182,57 @@ packages() {
fi fi
} }
# Option handling
# -------------------------------------- # --------------------------------------
SCRIPT="$(basename "$0")" script="$(basename "$0")"
options="$(getopt --options "ha:fp:ls" --longoptions "help,add:,files,packages:,pull,push" -n "$script" -- "$@" 2>&1)"
result="$?"
# $1 = -option, $2 message # Exit if invalid option is provided
option_wrong() { if [ "$result" -ne 0 ]; then
[ -z "$1" ] || [ -z "$2" ] && return 1 echo "$options" | head -n 1 >&2
echo "Try './$script --help' for more information." >&2
echo "$SCRIPT: $2 '$1'" >&2
echo "Try '$SCRIPT -h' or '$SCRIPT --help' for more information." >&2
exit 1 exit 1
}
# Get all the -options
OPTIONS=$(echo "$@" | awk '{
for(i=1; i<=NF; i++) { if ($i ~ /^-/) printf "%s ", $i }
}' | wc -w)
# Check if -options are valid
if [ "$OPTIONS" -gt 1 ]; then
LAST="$(echo "$@" | cut -d ' ' -f $#)"
option_wrong "$LAST" "option too many"
elif [ "$#" -gt 2 ]; then
LAST="$(echo "$@" | cut -d ' ' -f $#)"
option_wrong "$LAST" "argument too many"
fi fi
OPT="$(echo "$* " | cut -d ' ' -f 1)" eval set -- "$options"
ARG="$(echo "$* " | cut -d ' ' -f 2)"
# Parse -options and call functions while true; do
case $OPT in case "$1" in
-a | --add) -a | --add)
add "$ARG" || option_wrong "$OPT" 'option requires an argument' add "$2"
shift 2
;; ;;
-f | --files) -f | --files)
list_files list_files
shift
;; ;;
-h | --help) -h | --help)
help help
exit
;; ;;
-p | --packages) -p | --packages)
packages "$ARG" packages "$2"
shift 2
;; ;;
-l | --pull) -l | --pull)
pull pull
shift
;; ;;
-s | --push) -s | --push)
push push
shift
;;
--)
shift
break
;; ;;
*) *)
option_wrong "$OPT" 'invalid option' break
;; ;;
esac esac
done
# @Todo: # @Todo:
# push function to push just one file # push function to push just one file

Loading…
Cancel
Save