FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > ArchLinux > ArchLinux Pacman Development

 
 
LinkBack Thread Tools
 
Old 07-05-2011, 11:51 AM
Allan McRae
 
Default parse_options: implement optional arguments

This allows options specified with a trailing "::" to optionally
take arguments.

Signed-off-by: Allan McRae <allan@archlinux.org>
---
scripts/library/parse_options.sh | 19 +++++++++++++++----
1 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh
index 9622230..49cbb60 100644
--- a/scripts/library/parse_options.sh
+++ b/scripts/library/parse_options.sh
@@ -17,7 +17,12 @@ parse_options() {
fi
done
if [[ -n $match ]]; then
- if [[ ${1:2} = $match ]]; then
+ local needsargument=0
+
+ [[ ${match} = ${1:2}: ]] && needsargument=1
+ [[ ${match} = ${1:2}:: && -n $2 && ${2:0:1} != "-" ]] && needsargument=1
+
+ if (( ! needsargument )); then
printf ' %s' "$1"
else
if [[ -n $2 ]]; then
@@ -40,7 +45,15 @@ parse_options() {
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
+ local needsargument=0
+
+ [[ $short_options =~ ${1:i:1}: && ! $short_options =~ ${1:i:1}:: ]] && needsargument=1
+ [[ $short_options =~ ${1:i:1}:: &&
+ ( -n ${1:$i+1} || ( -n $2 && ${2:0:1} != "-" ) ) ]] && needsargument=1
+
+ if (( ! needsargument )); then
+ printf ' -%s' "${1:i:1}"
+ else
if [[ -n ${1:$i+1} ]]; then
printf ' -%s' "${1:i:1}"
printf " '%s'" "${1:$i+1}"
@@ -55,8 +68,6 @@ parse_options() {
fi
fi
break
- else
- printf ' -%s' "${1:i:1}"
fi
else
echo "@SCRIPTNAME@: $(gettext "unrecognized option") '-${1:i:1}'" >&2
--
1.7.6
 

Thread Tools




All times are GMT. The time now is 04:34 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org