Skip to contentSkip to conversation

Improve option parsing for cg-log

PATCH Applied Marcel Holtmann 3 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improve option parsing for cg-log

Hi Petr,

the attached patch changes the option parsing, because otherwise we are stuck to a specific order.

Regards

Marcel

cg-log (mode:100755) +27 -22
Index: cg-log
===================================================================
@@ -27,29 +27,34 @@
 # at least somewhere it does. Bash is broken.
 trap exit SIGPIPE
 
-if [ "$1" = "-c" ]; then
-	shift
-	# See terminfo(5), "Color Handling"
-	colheader="$(tput setaf 2)"    # Green
-	colauthor="$(tput setaf 6)"    # Cyan
-	colcommitter="$(tput setaf 5)" # Magenta
-	colfiles="$(tput setaf 4)"     # Blue
-	colsignoff="$(tput setaf 3)"   # Yellow
-	coldefault="$(tput op)"        # Restore default
-else
-	colheader=
-	colauthor=
-	colcommitter=
-	colfiles=
-	colsignoff=
-	coldefault=
-fi
-
+colheader=
+colauthor=
+colcommitter=
+colfiles=
+colsignoff=
+coldefault=
 list_files=
-if [ "$1" = "-f" ]; then
-	shift
-	list_files=1
-fi
+while [ "$1" ]; do
+	case "$1" in
+	-c)
+		# See terminfo(5), "Color Handling"
+		colheader="$(tput setaf 2)"    # Green
+		colauthor="$(tput setaf 6)"    # Cyan
+		colcommitter="$(tput setaf 5)" # Magenta
+		colfiles="$(tput setaf 4)"     # Blue
+		colsignoff="$(tput setaf 3)"   # Yellow
+		coldefault="$(tput op)"        # Restore default
+		shift
+		;;
+	-f)
+		list_files=1
+		shift
+		;;
+	*)
+		break
+		;;
+	esac
+done
 
 list_commit_files()
 {
Applied
#1 lore raw

Dear diary, on Thu, May 12, 2005 at 10:51:54PM CEST, I got a letter where Marcel Holtmann <[email protected]> told me that...

Quoted 1 line

Hi Petr,

Hi,

Quoted 2 lines

the attached patch changes the option parsing, because otherwise we are stuck to a specific order.

thanks, applied. However, you didn't include the -r options parsing in there yet.

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
#2 lore raw

Hi Petr,

Quoted 5 lines
Quoted 2 lines

the attached patch changes the option parsing, because otherwise we are stuck to a specific order.

thanks, applied. However, you didn't include the -r options parsing in there yet.

what do you mean by that?

Regards

Marcel

#3 lore raw

Dear diary, on Thu, May 12, 2005 at 11:49:46PM CEST, I got a letter where Marcel Holtmann <[email protected]> told me that...

Quoted 9 lines

Hi Petr,

Quoted 5 lines
Quoted 2 lines

the attached patch changes the option parsing, because otherwise we are stuck to a specific order.

thanks, applied. However, you didn't include the -r options parsing in there yet.

what do you mean by that?

The -r option still must be after all the other options.

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
#4 lore raw

Hi Petr,

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

the attached patch changes the option parsing, because otherwise we are stuck to a specific order.

thanks, applied. However, you didn't include the -r options parsing in there yet.

what do you mean by that?

The -r option still must be after all the other options.

I see what you mean and it seems that I missed that option. Must be because you put the list_commit_files() between them and I assumed that there is no further option parsing.

Do you really wanna keep the double meaning of -r. Depending on a previous -r it is $log_start or $log_end.

Regards

Marcel

#5 lore raw

Dear diary, on Fri, May 13, 2005 at 11:05:34AM CEST, I got a letter where Marcel Holtmann <[email protected]> told me that...

Quoted 1 line

Hi Petr,

Hi,

Quoted 5 lines
Quoted 1 line

The -r option still must be after all the other options.

I see what you mean and it seems that I missed that option. Must be because you put the list_commit_files() between them and I assumed that there is no further option parsing.

Uhm, I know. :-) It sorta evolved like that. We didn't yet settle down on where to actually put the functions. I'd say right after . cg-Xlib.

Quoted 2 lines

Do you really wanna keep the double meaning of -r. Depending on a previous -r it is $log_start or $log_end.

Yes, by all means. On one side I like the colon notation, on the other side this was always my biggest usability problem with SVN. And it costs us nothing and does what the user would after all expect, I think.

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
#6 lore raw
In reply to Petr Baudis
Quoted 2 lines · Petr Baudis

The -r option still must be after all the other options.

I've been thinking about option parsing as well, and I think cogito could use a more "getopt-like" handling of options. "cg-log -cf" or "cg-diff -rorigin" is just so much easier to type...

The attached patch implements that for cg-log and cg-diff, by means of two new helper functions in cg-Xlib. It also improves error-handling a bit.

How about it?

/dan

Better option parsing in cg-diff and cg-log. Uses a new function, optparse, in cg-Xlib, that allows for getopt-style option parsing.

This means that "cg-log -cf" is equivalent to "cg-log -c -f", as is "cg-log -fc" and "cg-log -fc -rHEAD --" and even
"cg-log --file-list --color --revision=HEAD".

---

 cg-Xlib |   33 ++++++++++++++++++++++++++++++
 cg-diff |   53 ++++++++++++++++++++++---------------------------
 cg-log  |   69 ++++++++++++++++++++++++----------------------------------------
 3 files changed, 83 insertions(+), 72 deletions(-)
cg-Xlib (mode:100755) +33 -0
Index: cg-Xlib
===================================================================
@@ -62,6 +62,39 @@
 	fi
 done
 
+# option parsing
+
+opts=($@)
+
+optshift() {
+	unset opts[0]
+	opts=("${opts[@]}")
+	[ -z "$1" -o -n "$opts" ] || die "option \`$1' requires an argument"
+}
+
+optparse() {
+	[ -z "$1" ] && case $opts in
+		--) optshift; return 1 ;;
+		-*) return 0 ;;
+		*)  return 1 ;;
+	esac
+
+	local match=${1%=} minmatch=${2:-1} o=$opts val
+	[[ $1 == *= ]] && val=$match
+	case $match in
+	--*)	[ "$val" ] && o=${opts%%=*}
+		[ ${#o} -ge $((2 + $minmatch)) -a \
+			"${match:0:${#o}}" = "$o" ] || return 1
+		[[ -n "$val" && "$opts" == *=* ]] && opts[0]=${opts#*=} \
+			|| optshift $val ;;
+	-?)	[[ $o == $match* ]] || return 1
+		[[ $o != -?-* || -n "$val" ]] || die "unrecognized option \`$o'"
+		opts[0]=${o#$match}
+		[ "$opts" ] && { [ "$val" ] || opts[0]=-${opts}; } \
+			|| optshift $val ;;
+	*)	die "optparse cannot handle $1" ;;
+	esac
+}
 
 # Compatibility hacks:
 # 2005-04-26
cg-diff (mode:100755) +24 -29

70 lines not on this page · raw message

cg-log (mode:100755) +26 -43

89 lines not on this page · raw message