From a77e638c773d8a492f8c48e8c15474030a7e3d71 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Mon, 12 Dec 2011 22:36:57 +0100 Subject: contrib/*: Support the "--help" and "--version" options Add "--help"/"-h" and "--version"/"-V" support to all contrib scripts. Also, update scripts that used "-v" as a short option for "--version" and use "-V" for the sake of consistency. Additionally: * Move version and usage messages to separate convenience functions in all scripts. * Add a workaround to paccache to support "--help" and "--version". This should be replaced by a proper POSIX-compliant command line parser that supports long options in a future patch. * Add a "$myver" variable to all scripts and use it whenever we refer to the program version (e.g. in version messages). Also, use the pacman version number everywhere instead of using a different versioning scheme for each contrib script. This is achieved by adding a "PACKAGE_VERSION" placeholder that is replaced by sed(1) when the script is built. * Ensure we always return with exit status 0 if "--help" is used and return with exit status 1 if we display the usage message due to invalid arguments. * Add "AUTOMAKE_OPTIONS = std-options" and add all scripts to "bin_SCRIPTS" to make `make installcheck` check that installed scripts actually support the "--help" and "--version" options. Signed-off-by: Lukas Fleischer Signed-off-by: Dan McGee --- contrib/Makefile.am | 14 ++++++++++++++ contrib/bacman.in | 18 ++++++++++-------- contrib/paccache.in | 20 +++++++++++++++++--- contrib/pacdiff.in | 12 +++++++++++- contrib/paclist.in | 21 ++++++++++++++++++++- contrib/paclog-pkglist.in | 19 ++++++++++++++++--- contrib/pacscripts.in | 10 ++++++++-- contrib/pacsearch.in | 20 +++++++++++++++----- contrib/pacsysclean.in | 13 +++++++++++-- 9 files changed, 122 insertions(+), 25 deletions(-) diff --git a/contrib/Makefile.am b/contrib/Makefile.am index c9f63654..8751fd9b 100644 --- a/contrib/Makefile.am +++ b/contrib/Makefile.am @@ -1,3 +1,9 @@ +# enforce that all scripts have a --help and --version option +AUTOMAKE_OPTIONS = std-options + +bin_SCRIPTS = \ + $(OURSCRIPTS) + OURSCRIPTS = \ bacman \ paccache \ @@ -30,9 +36,17 @@ EXTRA_DIST = \ # Files that should be removed, but which Automake does not know. MOSTLYCLEANFILES = $(OURSCRIPTS) $(OURFILES) *.tmp +if USE_GIT_VERSION +GIT_VERSION := $(shell sh -c 'git describe --abbrev=4 --dirty | sed s/^v//') +REAL_PACKAGE_VERSION = $(GIT_VERSION) +else +REAL_PACKAGE_VERSION = $(PACKAGE_VERSION) +endif + edit = sed \ -e 's|@sysconfdir[@]|$(sysconfdir)|g' \ -e 's|@localstatedir[@]|$(localstatedir)|g' \ + -e 's|@PACKAGE_VERSION[@]|$(REAL_PACKAGE_VERSION)|g' \ -e 's|@SIZECMD[@]|$(SIZECMD)|g' \ -e '1s|!/bin/bash|!$(BASH_SHELL)|g' diff --git a/contrib/bacman.in b/contrib/bacman.in index 00d0691b..93623565 100755 --- a/contrib/bacman.in +++ b/contrib/bacman.in @@ -24,7 +24,7 @@ shopt -s extglob shopt -s nullglob declare -r myname='bacman' -readonly progver="0.2.1" +declare -r myver='@PACKAGE_VERSION@' # # User Friendliness @@ -35,19 +35,21 @@ usage() { echo "Example: $myname kernel26" } +version() { + printf "%s %s\n" "$myname" "$myver" + echo 'Copyright (C) 2008 locci ' +} + if (( $# != 1 )); then usage exit 1 fi -if [[ $1 == "--help" || $1 == "-h" ]]; then +if [[ $1 = -@(h|-help) ]]; then usage exit 0 -fi - -if [[ $1 == "--version" || $1 == "-v" ]]; then - echo "$myname version $progver" - echo "Copyright (C) 2008 locci" +elif [[ $1 = -@(V|-version) ]]; then + version exit 0 fi @@ -177,7 +179,7 @@ pkg_size=$(du -sk | awk '{print $1 * 1024}') # TODO adopt makepkg's write_pkginfo() into this or scripts/library # echo Generating .PKGINFO metadata... -echo "# Generated by $myname $progver" > .PKGINFO +echo "# Generated by $myname $myver" > .PKGINFO if [[ $INFAKEROOT == "1" ]]; then echo "# Using $(fakeroot -v)" >> .PKGINFO fi diff --git a/contrib/paccache.in b/contrib/paccache.in index b95b9646..da65f476 100755 --- a/contrib/paccache.in +++ b/contrib/paccache.in @@ -21,6 +21,7 @@ shopt -s extglob declare -r myname='paccache' +declare -r myver='@PACKAGE_VERSION@' declare -a candidates=() cmdopts=() whitelist=() blacklist=() declare -i delete=0 dryrun=0 filecount=0 move=0 needsroot=0 totalsaved=0 verbose=0 @@ -202,19 +203,32 @@ containing pacman package tarballs. EOF } +version() { + printf "%s %s\n" "$myname" "$myver" + echo 'Copyright (C) 2011 Dave Reisner ' +} + if (( ! UID )); then error "Do not run this script as root. You will be prompted for privilege escalation." exit 42 fi -while getopts ':a:c:dfhi:k:m:rsuvz' opt; do +# TODO: remove this workaround and use a sane command line parser (like the +# parse_options library from scripts/) here +if [[ $1 = -@(h|-help) ]]; then + usage + exit 0 +elif [[ $1 = -@(V|-version) ]]; then + version + exit 0 +fi + +while getopts ':a:c:dfi:k:m:rsuvz' opt; do case $opt in a) scanarch=$OPTARG ;; c) cachedir=$OPTARG ;; d) dryrun=1 ;; f) cmdopts=(-f) ;; - h) usage - exit 0 ;; i) if [[ $OPTARG = '-' ]]; then [[ ! -t 0 ]] && IFS=$'\n' read -r -d '' -a ign else diff --git a/contrib/pacdiff.in b/contrib/pacdiff.in index 79cf809d..bfafda26 100755 --- a/contrib/pacdiff.in +++ b/contrib/pacdiff.in @@ -18,6 +18,7 @@ # declare -r myname='pacdiff' +declare -r myver='@PACKAGE_VERSION@' diffprog=${DIFFPROG:-vimdiff} diffsearchpath=${DIFFSEARCHPATH:-/etc} @@ -32,6 +33,11 @@ usage() { echo "Example : DIFFPROG=meld DIFFSEARCHPATH=\"/boot /etc /usr\" $myname" } +version() { + printf "%s %s\n" "$myname" "$myver" + echo 'Copyright (C) 2007 Aaron Griffin ' +} + cmd() { if [ $locate -eq 1 ]; then locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave @@ -44,8 +50,12 @@ if [ $# -gt 0 ]; then case $1 in -l|--locate) locate=1;; - *) + -V|--version) + version; exit 0;; + -h|--help) usage; exit 0;; + *) + usage; exit 1;; esac fi diff --git a/contrib/paclist.in b/contrib/paclist.in index 8623049f..7883e21b 100755 --- a/contrib/paclist.in +++ b/contrib/paclist.in @@ -18,6 +18,7 @@ # along with this program. If not, see . declare -r myname='paclist' +declare -r myver='@PACKAGE_VERSION@' export TEXTDOMAIN='pacman' export TEXTDOMAINDIR='/usr/share/locale' @@ -29,13 +30,31 @@ if ! type gettext &>/dev/null; then } fi -if [[ -z $1 || $1 = -@(h|-help) ]]; then +usage() { printf '%s - List all packages installed from a given repo\n' "$myname" printf 'Usage: %s \n' "$myname" printf 'Example: %s testing\n' "$myname" +} + +version() { + printf "%s %s\n" "$myname" "$myver" + echo 'Copyright (C) 2008 Dan McGee ' + echo 'Copyright (C) 2011 Dave Reisner ' +} + +if [[ -z $1 ]]; then + usage exit 1 fi +if [[ $1 = -@(h|-help) ]]; then + usage + exit 0 +elif [[ $1 = -@(V|-version) ]]; then + version + exit 0 +fi + printf -v installed '[%s]' "$(gettext installed)" pacman -Sl $1 | awk -v i="$installed" '$NF == i { print $2,$3 }' diff --git a/contrib/paclog-pkglist.in b/contrib/paclog-pkglist.in index 0b27bf36..222bbc4c 100755 --- a/contrib/paclog-pkglist.in +++ b/contrib/paclog-pkglist.in @@ -18,16 +18,29 @@ # along with this program. If not, see . declare -r myname='paclog-pkglist' +declare -r myver='@PACKAGE_VERSION@' export TEXTDOMAIN='pacman' export TEXTDOMAINDIR='/usr/share/locale' declare logfile=${1:-@localstatedir@/log/pacman.log} +usage() { + printf 'usage: %s [pacman log]\n' "$myname" + printf 'example: %s @localstatedir@/log/pacman.log\n' "$myname" + printf '\ndefaults to: @localstatedir@/log/pacman.log\n' +} + +version() { + printf "%s %s\n" "$myname" "$myver" + echo 'Copyright (C) 2011 Dave Reisner ' +} + if [[ $1 ]]; then if [[ $1 = -@(h|-help) ]]; then - printf 'usage: %s [pacman log]\n' "$myname" - printf 'example: %s @localstatedir@/log/pacman.log\n' "$myname" - printf '\ndefaults to: @localstatedir@/log/pacman.log\n' + usage + exit 0 + elif [[ $1 = -@(V|-version) ]]; then + version exit 0 elif [[ ! -e $logfile ]]; then printf $"target not found: %s\n" "$1" diff --git a/contrib/pacscripts.in b/contrib/pacscripts.in index 967f7826..84687145 100755 --- a/contrib/pacscripts.in +++ b/contrib/pacscripts.in @@ -25,7 +25,7 @@ set -o nounset set -o errexit declare -r myname='pacscripts' -progver="0.4" +declare -r myver='@PACKAGE_VERSION@' conf="@sysconfdir@/pacman.conf" @@ -57,6 +57,12 @@ usage() { echo "Example: $myname gconf-editor-2.24.1-1-x86_64.pkg.tar.gz" } +version() { + printf "%s %s\n" "$myname" "$myver" + echo 'Copyright (c) 2009 Giulio "giulivo" Fidente ' + echo 'Copyright (c) 2009 Xavier Chantry ' +} + spacman() { if [ $EUID -eq 0 ]; then pacman "$@" @@ -127,6 +133,6 @@ fi case "$1" in --help|-h) usage; exit 0 ;; - --version|-v) echo "$myname version $progver"; exit 0 ;; + --version|-V) version; exit 0 ;; *) print_scriptlet $1 ;; esac diff --git a/contrib/pacsearch.in b/contrib/pacsearch.in index 794110eb..b1db8abe 100755 --- a/contrib/pacsearch.in +++ b/contrib/pacsearch.in @@ -25,21 +25,31 @@ use strict; use warnings; my $myname = 'pacsearch'; -my $version = "2.0"; +my $myver = '@PACKAGE_VERSION@'; -if ($#ARGV lt 0 || $ARGV[0] eq "--help" || $ARGV[0] eq "-h") { +sub usage { print "$myname - Add color and install information to a pacman -Ss search\n"; print "Usage: $myname \n"; print "Example: $myname ^gnome\n"; +} + +sub version { + printf "%s %s\n", $myname, $myver; + print "Copyright (C) 2008-2011 Dan McGee \n\n"; + print "Based off original shell script version:\n"; + print "Copyright (C) 2006-2007 Dan McGee \n"; +} + +if ($#ARGV lt 0 || $ARGV[0] eq "--help" || $ARGV[0] eq "-h") { + usage; if ($#ARGV lt 0) { exit 1; } exit 0; } -if ($ARGV[0] eq "--version" || $ARGV[0] eq "-v") { - print "$myname version $version\n"; - print "Copyright (C) 2006-2011 Dan McGee\n"; +if ($ARGV[0] eq "--version" || $ARGV[0] eq "-V") { + version; exit 0; } diff --git a/contrib/pacsysclean.in b/contrib/pacsysclean.in index c87082e0..17f7681b 100755 --- a/contrib/pacsysclean.in +++ b/contrib/pacsysclean.in @@ -2,23 +2,32 @@ # pacsysclean - Sort installed packages by decreasing installed size. Useful for system clean-up. +declare -r myname='pacsysclean' +declare -r myver='@PACKAGE_VERSION@' + PACMAN_OPTS= usage() { - echo "pacsysclean - Sort installed packages by decreasing installed size." + echo "$myname - Sort installed packages by decreasing installed size." echo - echo "Usage: pacsysclean [options]" + echo "Usage: $myname [options]" echo echo "Options:" echo " -o Specify custom pacman query options (e.g., dt)" echo " -h, --help Show this help message and exit" } +version() { + printf "%s %s\n" "$myname" "$myver" + echo 'Copyright (C) 2011 Eric BĂ©langer ' +} + if [ -n "$1" ]; then case "$1" in -o) PACMAN_OPTS="${2}" ;; -h|--help) usage; exit 0 ;; + -V|--version) version; exit 0 ;; *) usage; exit 1 ;; esac fi -- cgit v1.2.3-70-g09d2