summaryrefslogtreecommitdiff
path: root/scripts/library
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/library')
-rw-r--r--scripts/library/README15
-rw-r--r--scripts/library/output_format.sh21
-rw-r--r--scripts/library/parse_options.sh78
3 files changed, 114 insertions, 0 deletions
diff --git a/scripts/library/README b/scripts/library/README
new file mode 100644
index 00000000..1e9c962b
--- /dev/null
+++ b/scripts/library/README
@@ -0,0 +1,15 @@
+This folder contains code snippets that can be reused by multiple
+scripts. A brief description of each file follows.
+
+output_format.sh:
+Provides basic output formatting functions with levels 'msg', 'msg2',
+'warning' and 'error'. The 'msg' amd 'msg2' functions print to stdout
+and can be silenced by defining 'QUIET'. The 'warning' and 'error'
+functions print to stderr with the appropriate prefix added to the
+message.
+
+parse_options.sh:
+A getopt replacement to avoids portability issues, in particular the
+lack of long option name support in the default getopt provided by some
+platforms.
+Usage: parse_option $SHORT_OPTS $LONG_OPTS "$@"
diff --git a/scripts/library/output_format.sh b/scripts/library/output_format.sh
new file mode 100644
index 00000000..9e890e76
--- /dev/null
+++ b/scripts/library/output_format.sh
@@ -0,0 +1,21 @@
+msg() {
+ (( QUIET )) && return
+ local mesg=$1; shift
+ printf "==> ${mesg}\n" "$@" >&1
+}
+
+msg2() {
+ (( QUIET )) && return
+ local mesg=$1; shift
+ printf " -> ${mesg}\n" "$@" >&1
+}
+
+warning() {
+ local mesg=$1; shift
+ printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2
+}
+
+error() {
+ local mesg=$1; shift
+ printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
+} \ No newline at end of file
diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh
new file mode 100644
index 00000000..5ced2606
--- /dev/null
+++ b/scripts/library/parse_options.sh
@@ -0,0 +1,78 @@
+# getopt like parser
+parse_options() {
+ local short_options=$1; shift;
+ local long_options=$1; shift;
+ local ret=0;
+ local unused_options=""
+ local i
+
+ while [[ -n $1 ]]; do
+ if [[ ${1:0:2} = '--' ]]; then
+ if [[ -n ${1:2} ]]; then
+ local match=""
+ for i in ${long_options//,/ }; do
+ if [[ ${1:2} = ${i//:} ]]; then
+ match=$i
+ break
+ fi
+ done
+ if [[ -n $match ]]; then
+ if [[ ${1:2} = $match ]]; then
+ printf ' %s' "$1"
+ else
+ if [[ -n $2 ]]; then
+ printf ' %s' "$1"
+ shift
+ printf " '%s'" "$1"
+ else
+ printf "@SCRIPTNAME@: $(gettext "option %s requires an argument")" "'$1'" >&2
+ ret=1
+ fi
+ fi
+ else
+ echo "@SCRIPTNAME@: $(gettext "unrecognized option") '$1'" >&2
+ ret=1
+ fi
+ else
+ shift
+ break
+ fi
+ elif [[ ${1:0:1} = '-' ]]; then
+ for ((i=1; i<${#1}; i++)); do
+ if [[ $short_options =~ ${1:i:1} ]]; then
+ if [[ $short_options =~ ${1:i:1}: ]]; then
+ if [[ -n ${1:$i+1} ]]; then
+ printf ' -%s' "${1:i:1}"
+ printf " '%s'" "${1:$i+1}"
+ else
+ if [[ -n $2 ]]; then
+ printf ' -%s' "${1:i:1}"
+ shift
+ printf " '%s'" "${1}"
+ else
+ printf "@SCRIPTNAME@: $(gettext "option %s requires an argument")" "'-${1:i:1}'" >&2
+ ret=1
+ fi
+ fi
+ break
+ else
+ printf ' -%s' "${1:i:1}"
+ fi
+ else
+ echo "@SCRIPTNAME@: $(gettext "unrecognized option") '-${1:i:1}'" >&2
+ ret=1
+ fi
+ done
+ else
+ unused_options="${unused_options} '$1'"
+ fi
+ shift
+ done
+
+ printf " --"
+ [[ $unused_options ]] && printf ' %s' "${unused_options[@]}"
+ [[ $1 ]] && printf " '%s'" "$@"
+ printf "\n"
+
+ return $ret
+} \ No newline at end of file