# [PATCH] Allow help/--help/-h to be run outside of a TopGit repo

5 messages from 2008-11-20 to 2008-11-20. Participants: martin f. krafft, Petr Baudis, martin f krafft.
Thread: https://gitlist.dev/t/16406

## martin f. krafft, 2008-11-20 11:46

Subject: [PATCH] Allow help/--help/-h to be run outside of a TopGit repo
Message-ID: <1227181594-15123-1-git-send-email-madduck@debian.org>
URL: https://gitlist.dev/e/1227181594-15123-1-git-send-email-madduck%40debian.org

```
The user ought to be able to call `tg help` from anywhere in the filesystem,
not just Git repositories, so the help parsing has to happen before the calls
to git git binary.

Debian bug: #501982

Signed-off-by: martin f. krafft <madduck@debian.org>

---
 tg.sh |   28 +++++++++++++++++++++++++---
 1 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/tg.sh b/tg.sh
index 4dcc15e..258f8ce 100644
--- a/tg.sh
+++ b/tg.sh
@@ -235,12 +235,37 @@ do_help()
 	fi
 }
 
+# Check whether we are supposed to output the help message
+should_do_help()
+{
+	# we are being sourced for utility functions, never run help
+	[ -z "$tg__include" ] || return 1
+
+	local prev
+	while [ -n "$1" ]; do
+		case "$1" in
+		help|--help|-h)
+			shift
+			echo "${1:-$prev}"
+			return 0
+		esac
+		prev="$1"
+		shift
+	done
+
+	# run help when there was no previous topic, meaning that there where
+	# no arguments at all
+	test -z "$prev"
+}
 
 ## Startup
 
 [ -d "@cmddir@" ] ||
 	die "No command directory: '@cmddir@'"
 
+# check if we should run help and get the topic while we're at it
+help_topic="$(should_do_help "$@")" && { do_help "$help_topic"; exit 0; }
+
 ## Initial setup
 
 set -e
@@ -270,9 +295,6 @@ cmd="$1"
 shift
 
 case "$cmd" in
-help|--help|-h)
-	do_help "$1"
-	exit 0;;
 --hooks-path)
 	# Internal command
 	echo "@hooksdir@";;
-- 
tg: (f17218e..) fixes/independent-help (depends on: upstream)

```

## Petr Baudis, 2008-11-20 14:27

Subject: Re: [PATCH] Allow help/--help/-h to be run outside of a TopGit repo
Message-ID: <20081120142702.GG10491@machine.or.cz>
URL: https://gitlist.dev/e/20081120142702.GG10491%40machine.or.cz
In-Reply-To: <1227181594-15123-1-git-send-email-madduck@debian.org>

```
On Thu, Nov 20, 2008 at 12:46:34PM +0100, martin f. krafft wrote:
> The user ought to be able to call `tg help` from anywhere in the filesystem,
> not just Git repositories, so the help parsing has to happen before the calls
> to git git binary.
> 
> Debian bug: #501982
> 
> Signed-off-by: martin f. krafft <madduck@debian.org>
> 
> ---
>  tg.sh |   28 +++++++++++++++++++++++++---
>  1 files changed, 25 insertions(+), 3 deletions(-)
> 
> diff --git a/tg.sh b/tg.sh
> index 4dcc15e..258f8ce 100644
> --- a/tg.sh
> +++ b/tg.sh
> @@ -235,12 +235,37 @@ do_help()
>  	fi
>  }
>  
> +# Check whether we are supposed to output the help message
> +should_do_help()
> +{
> +	# we are being sourced for utility functions, never run help
> +	[ -z "$tg__include" ] || return 1
> +
> +	local prev
> +	while [ -n "$1" ]; do
> +		case "$1" in
> +		help|--help|-h)
> +			shift
> +			echo "${1:-$prev}"
> +			return 0
> +		esac
> +		prev="$1"
> +		shift
> +	done
> +
> +	# run help when there was no previous topic, meaning that there where
> +	# no arguments at all
> +	test -z "$prev"
> +}
>  
>  ## Startup
>  
>  [ -d "@cmddir@" ] ||
>  	die "No command directory: '@cmddir@'"
>  
> +# check if we should run help and get the topic while we're at it
> +help_topic="$(should_do_help "$@")" && { do_help "$help_topic"; exit 0; }
> +

Why is this so complicated? Can't you just do_help from
should_do_help()? The overall semantics seems strange anyway, though -
it seems that 'tg mail --help -r' will try to show help for '-r' instead
of 'mail'.

-- 
				Petr "Pasky" Baudis
People who take cold baths never have rheumatism, but they have
cold baths.

```

## martin f krafft, 2008-11-20 14:55

Subject: Re: [PATCH] Allow help/--help/-h to be run outside of a TopGit repo
Message-ID: <20081120145511.GD29789@piper.oerlikon.madduck.net>
URL: https://gitlist.dev/e/20081120145511.GD29789%40piper.oerlikon.madduck.net
In-Reply-To: <20081120142702.GG10491@machine.or.cz>

```
also sprach Petr Baudis <pasky@suse.cz> [2008.11.20.1527 +0100]:
> Why is this so complicated? Can't you just do_help from
> should_do_help()?

Yes, I could, but that would be in conflict with my understanding of
the Law of Demeter.

> The overall semantics seems strange anyway, though - it seems that
> 'tg mail --help -r' will try to show help for '-r' instead of
> 'mail'.

True, but then we are parsing command lines. Would you be opposed to
a getopt (POSIX) approach to normalising/parsing the command line?

Cheers,

-- 
 .''`.   martin f. krafft <madduck@debian.org>
: :'  :  proud Debian developer, author, administrator, and user
`. `'`   http://people.debian.org/~madduck - http://debiansystem.info
  `-  Debian - when you have better things to do than fixing systems
 
this space intentionally left occupied.

```

## Petr Baudis, 2008-11-20 15:08

Subject: Re: [PATCH] Allow help/--help/-h to be run outside of a TopGit repo
Message-ID: <20081120150832.GI10491@machine.or.cz>
URL: https://gitlist.dev/e/20081120150832.GI10491%40machine.or.cz
In-Reply-To: <20081120145511.GD29789@piper.oerlikon.madduck.net>

```
On Thu, Nov 20, 2008 at 03:55:11PM +0100, martin f krafft wrote:
> also sprach Petr Baudis <pasky@suse.cz> [2008.11.20.1527 +0100]:
> > Why is this so complicated? Can't you just do_help from
> > should_do_help()?
> 
> Yes, I could, but that would be in conflict with my understanding of
> the Law of Demeter.

What is that?

> > The overall semantics seems strange anyway, though - it seems that
> > 'tg mail --help -r' will try to show help for '-r' instead of
> > 'mail'.
> 
> True, but then we are parsing command lines. Would you be opposed to
> a getopt (POSIX) approach to normalising/parsing the command line?

No. :-)

				Petr "Pasky" Baudis

```

## martin f krafft, 2008-11-20 15:11

Subject: Re: [PATCH] Allow help/--help/-h to be run outside of a TopGit repo
Message-ID: <20081120151100.GA2354@piper.oerlikon.madduck.net>
URL: https://gitlist.dev/e/20081120151100.GA2354%40piper.oerlikon.madduck.net
In-Reply-To: <20081120150832.GI10491@machine.or.cz>

```
also sprach Petr Baudis <pasky@suse.cz> [2008.11.20.1608 +0100]:
> What is that?

http://en.wikipedia.org/wiki/Law_of_Demeter

In the context of our discussion, it basically means that if
should_do_help calls do_help, then we cannot test should_do_help
independently (without mocking out do_help, which is plain ugly).
Not that we have a testing infrastructure (yet) anyway, but...

> > True, but then we are parsing command lines. Would you be opposed to
> > a getopt (POSIX) approach to normalising/parsing the command line?
> 
> No. :-)

Good. I will see what I can do about this. In the mean time, I will
just leave things as they are.

-- 
 .''`.   martin f. krafft <madduck@debian.org>
: :'  :  proud Debian developer, author, administrator, and user
`. `'`   http://people.debian.org/~madduck - http://debiansystem.info
  `-  Debian - when you have better things to do than fixing systems
 
windoze nt crashed.
i am the blue screen of death.
no one hears your screams.

```
