[Intel-gfx] [maintainer-tools PATCH 02/30] qf: Introduce subcommand and aliases

Rodrigo Vivi rodrigo.vivi at intel.com
Mon Aug 21 20:10:52 UTC 2017


Let's start refreshing qf a bit by introducing
subcommand and aliases like dim.

The goal is to have an standardized qf and dim
where both have same style, documentation and
also that is in a format that we can easily
introduce to make check.

Actually all new code here is a simple copy
from dim directly with s/dim/qf.

This patch also introduce qf_usage,
qf_list_commands qf_list_aliases and already
 move qf_help from the case parse $1 to a
subcommand.

Cc: Daniel Vetter <daniel.vetter at ffwll.ch>
Cc: Jani Nikula <jani.nikula at intel.com>
Cc: Joonas Lahtinen <joonas.lahtinen at linux.intel.com>
Signed-off-by: Rodrigo Vivi <rodrigo.vivi at intel.com>
---
 Makefile |   5 ++++
 qf       | 102 +++++++++++++++++++++++++++++++++++++++++++++++----------------
 qf.rst   |  14 +++++++++
 3 files changed, 96 insertions(+), 25 deletions(-)

diff --git a/Makefile b/Makefile
index adf26126e27c..7cb22d2b1e06 100644
--- a/Makefile
+++ b/Makefile
@@ -51,6 +51,11 @@ mancheck:
 			echo "$@: $$cmd not documented"; \
 		fi \
 	done
+	@for cmd in $$(./qf list-commands); do \
+		if ! grep -q "^$$cmd" qf.rst; then \
+			echo "$@: $$cmd not documented"; \
+		fi \
+	done
 	rst2man --strict --no-raw dim.rst >/dev/null
 	rst2man --strict --no-raw qf.rst >/dev/null
 
diff --git a/qf b/qf
index d1c331023230..c02fb75a4e3a 100755
--- a/qf
+++ b/qf
@@ -169,28 +169,6 @@ function quilt_clean_check
 	fi
 }
 
-function qf_help
-{
-	manpage=$DIM_PREFIX/maintainer-tools/qf.rst
-	if [ ! -e "$manpage" ]; then
-		manpage=$(dirname $(readlink -f $0))/qf.rst
-		if [ ! -e "$manpage" ]; then
-			echo "Can't find the man page."
-			exit 1
-		fi
-	fi
-
-	if hash rst2man 2>/dev/null; then
-		renderer=rst2man
-		pager="man -l -"
-	else
-		renderer=cat
-		pager=${PAGER:-cat}
-	fi
-
-	$renderer < $manpage | $pager
-}
-
 case "$1" in
 	setup)
 		cd `git rev-parse --show-toplevel`
@@ -480,7 +458,81 @@ case "$1" in
 		shift
 		gitk "$@"
 		;;
-	*)
-		qf_help
-		;;
 esac
+
+qf=$(basename $0)
+
+# first positional argument is the subcommand
+if [ -n "$HELP" ] || [ "$#" = "0" ]; then
+    subcommand="usage"
+else
+    subcommand="$1"
+    shift
+fi
+
+function qf_help
+{
+	manpage=$DIM_PREFIX/maintainer-tools/qf.rst
+	if [ ! -e "$manpage" ]; then
+		manpage=$(dirname $(readlink -f $0))/qf.rst
+		if [ ! -e "$manpage" ]; then
+			echo "Can't find the man page."
+			exit 1
+		fi
+	fi
+
+	if hash rst2man 2>/dev/null; then
+		renderer=rst2man
+		pager="man -l -"
+	else
+		renderer=cat
+		pager=${PAGER:-cat}
+	fi
+
+	$renderer < $manpage | $pager
+}
+
+function qf_list_commands
+{
+	declare -F | grep -o " qf_[a-zA-Z_]*" | sed 's/^ qf_//;s/_/-/g'
+}
+
+function qf_list_aliases
+{
+	# use posix mode to omit functions in set output
+	( set -o posix; set ) | grep "^qf_alias_[a-zA-Z0-9_]*=" |\
+		sed 's/^qf_alias_//;s/=/\t/;s/_/-/g'
+}
+
+function qf_usage
+{
+	echo "usage: $qf SUBCOMMAND [ARGUMENTS]"
+	echo
+	echo "The available subcommands are:"
+	if hash column 2>/dev/null; then
+		qf_list_commands | column -c 72 | sed 's/^/\t/'
+	else
+		qf_list_commands | sed 's/^/\t/'
+	fi
+	echo
+	echo "See '$qf help' for more information."
+}
+
+# qf subcommand aliases (with bash 4.3+)
+if ! declare -n subcmd=qf_alias_${subcommand//-/_} &> /dev/null || \
+		test -z "${subcmd:-}"; then
+	subcmd="$subcommand"
+fi
+
+# look up the function by the subcommand name
+subcmd_func=qf_${subcmd//-/_}
+if ! declare -f $subcmd_func >/dev/null; then
+	echo "'$subcommand' is not a qf command."
+	qf_usage
+	exit 1
+fi
+
+# throw away to not confuse list-aliases
+unset subcmd
+
+$subcmd_func "$@"
diff --git a/qf.rst b/qf.rst
index f0019d76c53d..ed5c199baff4 100644
--- a/qf.rst
+++ b/qf.rst
@@ -95,6 +95,16 @@ $ qf git bisect new|old
 COMMANDS
 ========
 
+list-aliases
+------------
+List all aliases for the subcommand names.
+
+See \$qf_alias_<alias> under ENVIRONMENT below on how to define aliases.
+
+list-commands
+-------------
+List all subcommand names, including aliases.
+
 setup [*branch-name*]
 ---------------------
 Sets up a git repository for this quilt worflow script by
@@ -233,6 +243,10 @@ help
 ----
 This help text here
 
+usage
+-----
+Short form usage help listing all subcommands.
+
 CONTRIBUTING
 ============
 
-- 
2.13.2



More information about the Intel-gfx mailing list