88#include "common.h"
99#include "str.h"
1010
11- static int print_spec_name (git_str * out , const cli_opt_spec * spec )
11+ #define is_switch_or_value (spec ) \
12+ ((spec)->type == CLI_OPT_TYPE_SWITCH || \
13+ (spec)->type == CLI_OPT_TYPE_VALUE)
14+
15+ static int print_spec_args (git_str * out , const cli_opt_spec * spec )
1216{
13- if (spec -> type == CLI_OPT_TYPE_VALUE && spec -> alias &&
14- !(spec -> usage & CLI_OPT_USAGE_VALUE_OPTIONAL ) &&
15- !(spec -> usage & CLI_OPT_USAGE_SHOW_LONG ))
16- return git_str_printf (out , "-%c <%s>" , spec -> alias , spec -> value_name );
17- if (spec -> type == CLI_OPT_TYPE_VALUE && spec -> alias &&
18- !(spec -> usage & CLI_OPT_USAGE_SHOW_LONG ))
19- return git_str_printf (out , "-%c [<%s>]" , spec -> alias , spec -> value_name );
20- if (spec -> type == CLI_OPT_TYPE_VALUE &&
21- !(spec -> usage & CLI_OPT_USAGE_VALUE_OPTIONAL ))
22- return git_str_printf (out , "--%s[=<%s>]" , spec -> name , spec -> value_name );
23- if (spec -> type == CLI_OPT_TYPE_VALUE )
24- return git_str_printf (out , "--%s=<%s>" , spec -> name , spec -> value_name );
17+ GIT_ASSERT (!is_switch_or_value (spec ));
18+
2519 if (spec -> type == CLI_OPT_TYPE_ARG )
2620 return git_str_printf (out , "<%s>" , spec -> value_name );
2721 if (spec -> type == CLI_OPT_TYPE_ARGS )
2822 return git_str_printf (out , "<%s>..." , spec -> value_name );
2923 if (spec -> type == CLI_OPT_TYPE_LITERAL )
3024 return git_str_printf (out , "--" );
31- if (spec -> alias && !(spec -> usage & CLI_OPT_USAGE_SHOW_LONG ))
25+
26+ GIT_ASSERT (!"unknown option spec type" );
27+ return -1 ;
28+ }
29+
30+ GIT_INLINE (int ) print_spec_alias (git_str * out , const cli_opt_spec * spec )
31+ {
32+ GIT_ASSERT (is_switch_or_value (spec ) && spec -> alias );
33+
34+ if (spec -> type == CLI_OPT_TYPE_VALUE &&
35+ !(spec -> usage & CLI_OPT_USAGE_VALUE_OPTIONAL ))
36+ return git_str_printf (out , "-%c <%s>" , spec -> alias , spec -> value_name );
37+ else if (spec -> type == CLI_OPT_TYPE_VALUE )
38+ return git_str_printf (out , "-%c [<%s>]" , spec -> alias , spec -> value_name );
39+ else
3240 return git_str_printf (out , "-%c" , spec -> alias );
33- if (spec -> name )
41+ }
42+
43+ GIT_INLINE (int ) print_spec_name (git_str * out , const cli_opt_spec * spec )
44+ {
45+ GIT_ASSERT (is_switch_or_value (spec ) && spec -> name );
46+
47+ if (spec -> type == CLI_OPT_TYPE_VALUE &&
48+ !(spec -> usage & CLI_OPT_USAGE_VALUE_OPTIONAL ))
49+ return git_str_printf (out , "--%s=<%s>" , spec -> name , spec -> value_name );
50+ else if (spec -> type == CLI_OPT_TYPE_VALUE )
51+ return git_str_printf (out , "--%s[=<%s>]" , spec -> name , spec -> value_name );
52+ else
3453 return git_str_printf (out , "--%s" , spec -> name );
54+ }
55+
56+ GIT_INLINE (int ) print_spec_full (git_str * out , const cli_opt_spec * spec )
57+ {
58+ int error = 0 ;
59+
60+ if (is_switch_or_value (spec )) {
61+ if (spec -> alias )
62+ error |= print_spec_alias (out , spec );
63+
64+ if (spec -> alias && spec -> name )
65+ error |= git_str_printf (out , ", " );
66+
67+ if (spec -> name )
68+ error |= print_spec_name (out , spec );
69+ } else {
70+ error |= print_spec_args (out , spec );
71+ }
72+
73+ return error ;
74+ }
75+
76+ GIT_INLINE (int ) print_spec (git_str * out , const cli_opt_spec * spec )
77+ {
78+ if (is_switch_or_value (spec )) {
79+ if (spec -> alias && !(spec -> usage & CLI_OPT_USAGE_SHOW_LONG ))
80+ return print_spec_alias (out , spec );
81+ else
82+ return print_spec_name (out , spec );
83+ }
3584
36- GIT_ASSERT ( 0 );
85+ return print_spec_args ( out , spec );
3786}
3887
3988/*
@@ -56,7 +105,7 @@ int cli_opt_usage_fprint(
56105 int error ;
57106
58107 /* TODO: query actual console width. */
59- int console_width = 80 ;
108+ int console_width = 78 ;
60109
61110 if ((error = git_str_printf (& usage , "usage: %s" , command )) < 0 )
62111 goto done ;
@@ -88,7 +137,7 @@ int cli_opt_usage_fprint(
88137 if (!optional && !choice && next_choice )
89138 git_str_putc (& opt , '(' );
90139
91- if ((error = print_spec_name (& opt , spec )) < 0 )
140+ if ((error = print_spec (& opt , spec )) < 0 )
92141 goto done ;
93142
94143 if (!optional && choice && !next_choice )
@@ -113,11 +162,11 @@ int cli_opt_usage_fprint(
113162 git_str_putc (& usage , ' ' );
114163
115164 linelen = prefixlen ;
116- } else {
117- git_str_putc (& usage , ' ' );
118- linelen += git_str_len (& opt ) + 1 ;
119165 }
120166
167+ git_str_putc (& usage , ' ' );
168+ linelen += git_str_len (& opt ) + 1 ;
169+
121170 git_str_puts (& usage , git_str_cstr (& opt ));
122171
123172 if (git_str_oom (& usage )) {
@@ -169,13 +218,13 @@ int cli_opt_help_fprint(
169218
170219 git_str_printf (& help , " " );
171220
172- if ((error = print_spec_name (& help , spec )) < 0 )
221+ if ((error = print_spec_full (& help , spec )) < 0 )
173222 goto done ;
174223
175- if (spec -> help )
176- git_str_printf (& help , ": %s" , spec -> help );
177-
178224 git_str_printf (& help , "\n" );
225+
226+ if (spec -> help )
227+ git_str_printf (& help , " %s\n" , spec -> help );
179228 }
180229
181230 /* Display the remaining arguments */
@@ -192,13 +241,13 @@ int cli_opt_help_fprint(
192241
193242 git_str_printf (& help , " " );
194243
195- if ((error = print_spec_name (& help , spec )) < 0 )
244+ if ((error = print_spec_full (& help , spec )) < 0 )
196245 goto done ;
197246
198- if (spec -> help )
199- git_str_printf (& help , ": %s" , spec -> help );
200-
201247 git_str_printf (& help , "\n" );
248+
249+ if (spec -> help )
250+ git_str_printf (& help , " %s\n" , spec -> help );
202251 }
203252
204253 if (git_str_oom (& help ) ||
0 commit comments