# [PATCH] usage_with_options: omit double new line on empty option list

5 messages from 2017-08-25 to 2017-08-25. Participants: Stefan Beller, Junio C Hamano.
Thread: https://gitlist.dev/t/46667

## Stefan Beller, 2017-08-25 16:48

Subject: [PATCH] usage_with_options: omit double new line on empty option list
Message-ID: <20170825164837.30118-1-sbeller@google.com>
URL: https://gitlist.dev/e/20170825164837.30118-1-sbeller%40google.com

```
Currently the worktree command gives its usage, when no subcommand is
given. However there are no general options, all options are related to
the subcommands itself, such that:

    $ git worktree
    usage: git worktree add [<options>] <path> [<branch>]
       or: git worktree list [<options>]
       or: git worktree lock [<options>] <path>
       or: git worktree prune [<options>]
       or: git worktree unlock <path>


    $

Note the two empty lines at the end of the usage string. This is because
the toplevel usage is printed with an empty options list.

Only print one new line after the usage string if the option list is empty.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 parse-options.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/parse-options.c b/parse-options.c
index 0dd9fc6a0d..1307c82861 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -603,7 +603,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
 		usagestr++;
 	}
 
-	if (opts->type != OPTION_GROUP)
+	if (opts->type != OPTION_GROUP && opts->type != OPTION_END)
 		fputc('\n', outfile);
 
 	for (; opts->type != OPTION_END; opts++) {
-- 
2.14.0.rc0.3.g6c2e499285


```

## Junio C Hamano, 2017-08-25 17:18

Subject: Re: [PATCH] usage_with_options: omit double new line on empty option list
Message-ID: <xmqq7exrtvl1.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq7exrtvl1.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <20170825164837.30118-1-sbeller@google.com>

```
Stefan Beller <sbeller@google.com> writes:

> Currently the worktree command gives its usage, when no subcommand is
> given. However there are no general options, all options are related to
> the subcommands itself, such that:
>
>     $ git worktree
>     usage: git worktree add [<options>] <path> [<branch>]
>        or: git worktree list [<options>]
>        or: git worktree lock [<options>] <path>
>        or: git worktree prune [<options>]
>        or: git worktree unlock <path>
>
>
>     $
>
> Note the two empty lines at the end of the usage string. This is because
> the toplevel usage is printed with an empty options list.
>
> Only print one new line after the usage string if the option list is empty.

Good find.  Shouldn't the last word in the sentence "non-empty", though?

>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>  parse-options.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/parse-options.c b/parse-options.c
> index 0dd9fc6a0d..1307c82861 100644
> --- a/parse-options.c
> +++ b/parse-options.c
> @@ -603,7 +603,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
>  		usagestr++;
>  	}
>  
> -	if (opts->type != OPTION_GROUP)
> +	if (opts->type != OPTION_GROUP && opts->type != OPTION_END)
>  		fputc('\n', outfile);
>  
>  	for (; opts->type != OPTION_END; opts++) {

```

## Stefan Beller, 2017-08-25 17:19

Subject: Re: [PATCH] usage_with_options: omit double new line on empty option list
Message-ID: <CAGZ79ka-PSt95TjVaUVf8yTZCdP8XuSfhJjBowJmgJbV2t0Tkg@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79ka-PSt95TjVaUVf8yTZCdP8XuSfhJjBowJmgJbV2t0Tkg%40mail.gmail.com
In-Reply-To: <xmqq7exrtvl1.fsf@gitster.mtv.corp.google.com>

```
On Fri, Aug 25, 2017 at 10:18 AM, Junio C Hamano <gitster@pobox.com> wrote:
> Stefan Beller <sbeller@google.com> writes:
>
>> Currently the worktree command gives its usage, when no subcommand is
>> given. However there are no general options, all options are related to
>> the subcommands itself, such that:
>>
>>     $ git worktree
>>     usage: git worktree add [<options>] <path> [<branch>]
>>        or: git worktree list [<options>]
>>        or: git worktree lock [<options>] <path>
>>        or: git worktree prune [<options>]
>>        or: git worktree unlock <path>
>>
>>
>>     $
>>
>> Note the two empty lines at the end of the usage string. This is because
>> the toplevel usage is printed with an empty options list.
>>
>> Only print one new line after the usage string if the option list is empty.
>
> Good find.  Shouldn't the last word in the sentence "non-empty", though?\

Yes.

Originally I wrote a lot more and then deleted it all, mixing up the negations
of what I was trying to say.

```

## Junio C Hamano, 2017-08-25 17:21

Subject: Re: [PATCH] usage_with_options: omit double new line on empty option list
Message-ID: <xmqq378ftvfs.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq378ftvfs.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <xmqq7exrtvl1.fsf@gitster.mtv.corp.google.com>

```
Junio C Hamano <gitster@pobox.com> writes:

> Stefan Beller <sbeller@google.com> writes:
>
>> Currently the worktree command gives its usage, when no subcommand is
>> given. However there are no general options, all options are related to
>> the subcommands itself, such that:
>>
>>     $ git worktree
>>     usage: git worktree add [<options>] <path> [<branch>]
>>        or: git worktree list [<options>]
>>        or: git worktree lock [<options>] <path>
>>        or: git worktree prune [<options>]
>>        or: git worktree unlock <path>
>>
>>
>>     $
>>
>> Note the two empty lines at the end of the usage string. This is because
>> the toplevel usage is printed with an empty options list.
>>
>> Only print one new line after the usage string if the option list is empty.
>
> Good find.  Shouldn't the last word in the sentence "non-empty", though?

Ah, the "do this" part was hard to understand and I misread it.  I
thought you were saying "only give a blank if we have something that
need to be separated from the stuff above".

So what you wrote above is not incorrect per-se, but I think it is
prone to be misunderstood.

>
>>
>> Signed-off-by: Stefan Beller <sbeller@google.com>
>> ---
>>  parse-options.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/parse-options.c b/parse-options.c
>> index 0dd9fc6a0d..1307c82861 100644
>> --- a/parse-options.c
>> +++ b/parse-options.c
>> @@ -603,7 +603,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
>>  		usagestr++;
>>  	}
>>  
>> -	if (opts->type != OPTION_GROUP)
>> +	if (opts->type != OPTION_GROUP && opts->type != OPTION_END)
>>  		fputc('\n', outfile);
>>  
>>  	for (; opts->type != OPTION_END; opts++) {

```

## Junio C Hamano, 2017-08-25 17:31

Subject: Re: [PATCH] usage_with_options: omit double new line on empty option list
Message-ID: <xmqqy3q7sge2.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqy3q7sge2.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <20170825164837.30118-1-sbeller@google.com>

```
Stefan Beller <sbeller@google.com> writes:

> Currently the worktree command gives its usage, when no subcommand is
> given. However there are no general options, all options are related to
> the subcommands itself, such that:
>
>     $ git worktree
>     usage: git worktree add [<options>] <path> [<branch>]
>        or: git worktree list [<options>]
>        or: git worktree lock [<options>] <path>
>        or: git worktree prune [<options>]
>        or: git worktree unlock <path>
>
>
>     $
>
> Note the two empty lines at the end of the usage string. This is because
> the toplevel usage is printed with an empty options list.

I have this feeling that this patch may not be sufficient.  

The reason for the first blank line is because we unconditionally
emit one, expecting that we would have a list of options to show and
want to separate the usage from that list, and the fix in this patch
is correct---it is the right way to suppress it.

But why do we need the second blank line in this case?  There is a
similar unconditional LF near the end of this function.  Is somebody
else (other than usage_with_options() which will exit immeidately)
calls this function and expects to say something more after what
this function said, and is this extra blank line at the end is to
prepare for that caller?

> Only print one new line after the usage string if the option list is empty.
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>  parse-options.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/parse-options.c b/parse-options.c
> index 0dd9fc6a0d..1307c82861 100644
> --- a/parse-options.c
> +++ b/parse-options.c
> @@ -603,7 +603,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
>  		usagestr++;
>  	}
>  
> -	if (opts->type != OPTION_GROUP)
> +	if (opts->type != OPTION_GROUP && opts->type != OPTION_END)
>  		fputc('\n', outfile);
>  
>  	for (; opts->type != OPTION_END; opts++) {

```
