# [PATCH] Make "tg help cmd" print cmd help

1 messages from 2008-08-04 to 2008-08-04. Participants: Russell Steicke.
Thread: https://gitlist.dev/t/14830

## Russell Steicke, 2008-08-04 12:21

Subject: [PATCH] Make "tg help cmd" print cmd help
Message-ID: <20080804122121.GD11179@maggie.localnet>
URL: https://gitlist.dev/e/20080804122121.GD11179%40maggie.localnet

```
"tg help" looked like it needed help, and stuff it needed to know
about was already in README.

Use awk to extract help information from the README file.  Store
the help files in $(PREFIX)/share/topgit.  "tg help foo" will cat
$(PREFIX)/share/topgit/tg-foo.txt.

Signed-off-by: Russell Steicke <russellsteicke@gmail.com>

---
 .gitignore     |    6 ++++++
 Makefile       |   11 +++++++++--
 create-help.sh |   17 +++++++++++++++++
 tg.sh          |   16 ++++++++++++++--
 4 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/.gitignore b/.gitignore
index 53ca141..6f0727f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,14 @@
 hooks/pre-commit
 tg-create
+tg-create.txt
 tg-delete
+tg-delete.txt
 tg-info
+tg-info.txt
 tg-patch
+tg-patch.txt
 tg-summary
+tg-summary.txt
 tg-update
+tg-update.txt
 tg
diff --git a/Makefile b/Makefile
index 3913d66..238d07d 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,7 @@
 PREFIX = $(HOME)
 bindir = $(PREFIX)/bin
 cmddir = $(PREFIX)/libexec/topgit
+sharedir = $(PREFIX)/share/topgit
 hooksdir = $(cmddir)/hooks
 
 
@@ -10,18 +11,22 @@ hooks_in = hooks/pre-commit.sh
 
 commands_out = $(patsubst %.sh,%,$(commands_in))
 hooks_out = $(patsubst %.sh,%,$(hooks_in))
+help_out = $(patsubst %.sh,%.txt,$(commands_in))
 
-all::	tg $(commands_out) $(hooks_out)
+all::	tg $(commands_out) $(hooks_out) $(help_out)
 
 tg $(commands_out) $(hooks_out): % : %.sh
 	@echo "[SED] $@"
 	@sed -e 's#@cmddir@#$(cmddir)#g;' \
 		-e 's#@hooksdir@#$(hooksdir)#g' \
 		-e 's#@bindir@#$(bindir)#g' \
+		-e 's#@sharedir@#$(sharedir)#g' \
 		$@.sh >$@+ && \
 	chmod +x $@+ && \
 	mv $@+ $@
 
+$(help_out): README
+	./create-help.sh `echo $@ | sed -e 's/tg-//' -e 's/\.txt//'`
 
 install:: all
 	install tg "$(bindir)"
@@ -29,6 +34,8 @@ install:: all
 	install $(commands_out) "$(cmddir)"
 	install -d -m 755 "$(hooksdir)"
 	install $(hooks_out) "$(hooksdir)"
+	install -d -m 755 "$(sharedir)"
+	install $(help_out) "$(sharedir)"
 
 clean::
-	rm -f tg $(commands_out) $(hooks_out)
+	rm -f tg $(commands_out) $(hooks_out) $(help_out)
diff --git a/create-help.sh b/create-help.sh
new file mode 100755
index 0000000..0c40ee2
--- /dev/null
+++ b/create-help.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+# Create the tg-foo.txt files which contain help for the tg-foo command.
+
+if [ $# -ne 1 ] ; then
+	echo "Usage: $0 tgcommand" 1>&2
+	exit 1
+fi
+
+< README awk '
+	BEGIN { incommand = 0; }
+	/^tg '"$1"'$/ { incommand = 1; next; }
+	/^tg/ { incommand = 0; next; }
+	/^~/ { next; } # Ignore the title underlines.
+	{ if (incommand) { print $0; } }
+'  > tg-"$1".txt
+
diff --git a/tg.sh b/tg.sh
index e7c42cd..03a392b 100644
--- a/tg.sh
+++ b/tg.sh
@@ -148,6 +148,19 @@ switch_to_base()
 	git symbolic-ref HEAD "$_base"
 }
 
+# Show the help messages.
+do_help()
+{
+	if [ -z "$1" ] ; then
+		echo "TopGit v0.1 - A different patch queue manager"
+		echo "Usage: tg (create|delete|info|patch|summary|update|help) ..."
+	elif [ -f "@sharedir@/tg-$1.txt" ] ; then
+		cat "@sharedir@/tg-$1.txt"
+	else
+		echo "`basename $0`: no help for $1" 1>&2
+	fi
+}
+
 
 ## Initial setup
 
@@ -171,8 +184,7 @@ shift
 
 case "$cmd" in
 help)
-	echo "TopGit v0.1 - A different patch queue manager"
-	echo "Usage: tg (create|delete|info|patch|summary|update|help) ..."
+	do_help "$1"
 	exit 1;;
 create|delete|info|patch|summary|update)
 	. "@cmddir@"/tg-$cmd;;
-- 
tg: (24367cc..) t/help (depends on: master)




-- 
Russell Steicke

-- Fortune says:
There's certainly precedent for that already too.  (Not claiming it's
*good* precedent, mind you. :-)
		-- Larry Wall in <199709021744.KAA12428@wall.org>

```
