chiark / gitweb /
getopt option parsing; usage message
authorColin Watson <cjwatson@chiark.greenend.org.uk>
Fri, 27 Mar 2009 15:51:39 +0000 (15:51 +0000)
committerColin Watson <cjwatson@chiark.greenend.org.uk>
Fri, 27 Mar 2009 15:51:39 +0000 (15:51 +0000)
rosetta-merge
rosetta-merge-all

index b34676c..a1b5019 100755 (executable)
@@ -1,22 +1,44 @@
 #! /bin/bash
 
-APPLY=false
-if [ "$1" = --apply ]; then
-       APPLY=:
-       shift
-fi
+usage () {
+       [ "$1" = 0 ] || exec >&2
+       echo "Usage: $0 [--apply] [--keep-old] [--prefix PREFIX] SOURCE LANG"
+       exit "$1"
+}
 
-KEEP_OLD=false
-if [ "$1" = --keep-old ]; then
-       KEEP_OLD=:
-       shift
-fi
+die_getopt () {
+       echo "getopt failed" >&2
+       exit 1
+}
 
+APPLY=false
+KEEP_OLD=false
 PREFIX=
-if [ "$1" = --prefix ]; then
-       PREFIX="$2-"
-       shift 2
-fi
+eval set -- "$(getopt -o '' -l apply,keep-old,prefix:,help -- "$@")" || die_getopt
+while :; do
+       case $1 in
+               --apply)
+                       APPLY=:
+                       shift
+                       ;;
+               --keep-old)
+                       KEEP_OLD=:
+                       shift
+                       ;;
+               --prefix)
+                       PREFIX="${2%-}-"
+                       shift 2
+                       ;;
+               --help)
+                       usage 0
+                       shift
+                       ;;
+               --)     shift; break ;;
+               *)      die_getopt ;;
+       esac
+done
+
+[ "$1" ] || usage 1
 
 SOURCE="$1"
 shift
index a0d0a8a..6740bbc 100755 (executable)
@@ -1,5 +1,47 @@
 #! /bin/sh
-if [ "$1" = --apply ]; then
+
+APPLY=false
+
+usage () {
+       [ "$1" = 0 ] || exec >&2
+       echo "Usage: $0 [--apply] [--keep-old] [--prefix PREFIX] SOURCE"
+       exit "$1"
+}
+
+die_getopt () {
+       echo "getopt failed" >&2
+       exit 1
+}
+
+parse_opts () {
+       eval set -- "$(getopt -o '' -l apply,keep-old,prefix:,help -- "$@")"
+       while :; do
+               case $1 in
+                       --apply)
+                               APPLY=:
+                               shift
+                               ;;
+                       --keep-old)
+                               shift
+                               ;;
+                       --prefix)
+                               shift 2
+                               ;;
+                       --help)
+                               usage 0
+                               shift
+                               ;;
+                       --)     shift; break ;;
+                       *)      die_getopt ;;
+               esac
+       done
+}
+
+[ "$1" ] || usage 1
+
+parse_opts "$@"
+
+if $APPLY; then
        for x in *.po; do rosetta-merge "$@" ${x%.*}; done
 else
        for x in *.po; do rosetta-merge "$@" ${x%.*} | view -; done