From 8bc4cc4165d2f30d5b1255d78dbe09480d436443 Mon Sep 17 00:00:00 2001 From: simonzack Date: Fri, 16 Jan 2015 09:18:40 +1100 Subject: [PATCH] better commandline option parsing, to work with aur package managers --- pacmatic | 131 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 60 deletions(-) diff --git a/pacmatic b/pacmatic index e2f2e93..7390e22 100755 --- a/pacmatic +++ b/pacmatic @@ -180,50 +180,72 @@ To use Pacmatic's functionality in a script, source with END } +explode_args() +{ + unset OPTS + local arg=$1 + while [[ $arg ]]; do + [[ $arg = "--" ]] && OPTS+=("$@") && break; + if [[ ${arg:0:1} = "-" && ${arg:1:1} != "-" ]]; then + OPTS+=("-${arg:1:1}") + (( ${#arg} > 2 )) && arg="-${arg:2}" || { shift; arg=$1; } + else + OPTS+=("$arg"); shift; arg=$1 + fi + done +} + if [ $# -eq 0 ]; then docs exit fi -[ $1 == "--as-lib" ] && return +orig_args=$* +explode_args "$@" +set -- "${OPTS[@]}" +unset OPTS + +while [[ $1 ]]; do + case "$1" in + --as-lib) return;; + --arch|--assume-installed|--cachedir|--color|--config|--gpgdir|--ignore|--ignoregroup|\ + --logfile|--print-format|-b|--dbpath|-o|--owns|-r|--root|-s|--search) shift;; + -S|--sync) SYNC=1;; + -U|--upgrade) UPGRADE=1;; + -y|--refresh) REFRESH=1;; + -u|--sysupgrade) SYSUPGRADE=1;; + -*) ;; + *) NAME=1;; + esac + shift +done -current_time=$(date +%s) -sync_time=$(last_sync) +if [ -z $REFRESH ]; then + sync_time=$(last_sync) +else + sync_time=$(date +%s) +fi upgrade_time=$(last_upgrade) -case "$1" in - -Syu|-Su|-Suy|-Syyu|-Syuy|-Suyy|-S|-Sy|-Syy) - # do not like this threading - check_news & - mail_summary - wait - echo "Recent ML chatter: $mail_counts" - if ( ! check_checkspace ); then - echo -e "\nPlease enable CheckSpace in pacman.conf" - fi - ;; -esac - -case "$1" in - -Syu|-Su|-Suy|-Syyu|-Syuy|-Suyy) - #check_news - ;; - -S) - #check_news - if [ $(( $sync_time - $upgrade_time )) -gt $(( $warn_time )) ]; then - echo -e "\nWarning: stale installation, rerun with '-Syu'\n" - fi - ;; - -Sy|-Syy) - #check_news - if [ $(( $current_time - $upgrade_time )) -gt $(( $warn_time )) ]; then - echo -e "\nWarning: stale installation, rerun with '-Syu'\n" - fi - ;; -esac +if [ ! -z $SYNC ]; then + # do not like this threading + check_news & + mail_summary + wait + echo "Recent ML chatter: $mail_counts" + if ( ! check_checkspace ); then + echo -e "\nPlease enable CheckSpace in pacman.conf" + fi +fi + +if [ ! -z $SYNC ] && [ -z $REFRESH ]; then + if [ $(( $sync_time - $upgrade_time )) -gt $(( $warn_time )) ]; then + echo -e "\nWarning: stale installation, rerun with '-Syu'\n" + fi +fi old_pc=$(pacnew_count) -$pacman_program $* +$pacman_program $orig_args exit_code=$? exit_time=$(date "+[%F %H:%M]") if [ -w "$pacman_log" ]; then @@ -232,30 +254,19 @@ fi new_pc=$(pacnew_count) add_pc=$(( $new_pc - $old_pc )) -# this section is a little clumsy - -if [ "$new_pc" = "0" ]; then - echo "No pacnew files to update." - exit 0 -fi - -case "$1" in - -Syu|-Su|-S|-Sy|-Syyu|-Syy) - echo -en "\n$new_pc pacnew files found ($add_pc added). Update files now? (Y/n) " - read char - if [ -z "$char" ]; then - $pacdiff_program - fi - case "$char" in - Y|y) +if [ ! -z $UPGRADE ] || ([ ! -z $SYNC ] && ([ ! -z $NAME ] || [ ! -z $SYSUPGRADE ])); then + if [ "$new_pc" = "0" ]; then + echo "No pacnew files to update." + else + echo -en "\n$new_pc pacnew files found ($add_pc added). Update files now? (Y/n) " + read char + if [ -z "$char" ]; then $pacdiff_program - ;; - N|n) - ;; - *) - ;; - esac - ;; - *) - ;; -esac + fi + case "$char" in + Y|y) $pacdiff_program;; + N|n) ;; + *) ;; + esac + fi +fi