@@ -1023,14 +1023,37 @@ static int usage_argh(const struct option *opts, FILE *outfile)
10231023 return utf8_fprintf (outfile , s , opts -> argh ? _ (opts -> argh ) : _ ("..." ));
10241024}
10251025
1026- #define USAGE_OPTS_WIDTH 24
1027- #define USAGE_GAP 2
1026+ static int usage_indent (FILE * outfile )
1027+ {
1028+ return fprintf (outfile , " " );
1029+ }
1030+
1031+ #define USAGE_OPTS_WIDTH 26
1032+
1033+ static void usage_padding (FILE * outfile , size_t pos )
1034+ {
1035+ if (pos < USAGE_OPTS_WIDTH )
1036+ fprintf (outfile , "%*s" , USAGE_OPTS_WIDTH - (int )pos , "" );
1037+ else
1038+ fprintf (outfile , "\n%*s" , USAGE_OPTS_WIDTH , "" );
1039+ }
1040+
1041+ static const struct option * find_option_by_long_name (const struct option * opts ,
1042+ const char * long_name )
1043+ {
1044+ for (; opts -> type != OPTION_END ; opts ++ ) {
1045+ if (opts -> long_name && !strcmp (opts -> long_name , long_name ))
1046+ return opts ;
1047+ }
1048+ return NULL ;
1049+ }
10281050
10291051static enum parse_opt_result usage_with_options_internal (struct parse_opt_ctx_t * ctx ,
10301052 const char * const * usagestr ,
10311053 const struct option * opts ,
10321054 int full , int err )
10331055{
1056+ const struct option * all_opts = opts ;
10341057 FILE * outfile = err ? stderr : stdout ;
10351058 int need_newline ;
10361059
@@ -1111,8 +1134,8 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11111134
11121135 for (; opts -> type != OPTION_END ; opts ++ ) {
11131136 size_t pos ;
1114- int pad ;
11151137 const char * cp , * np ;
1138+ const char * positive_name = NULL ;
11161139
11171140 if (opts -> type == OPTION_SUBCOMMAND )
11181141 continue ;
@@ -1131,7 +1154,7 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11311154 need_newline = 0 ;
11321155 }
11331156
1134- pos = fprintf (outfile , " " );
1157+ pos = usage_indent (outfile );
11351158 if (opts -> short_name ) {
11361159 if (opts -> flags & PARSE_OPT_NODASH )
11371160 pos += fprintf (outfile , "%c" , opts -> short_name );
@@ -1140,38 +1163,46 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11401163 }
11411164 if (opts -> long_name && opts -> short_name )
11421165 pos += fprintf (outfile , ", " );
1143- if (opts -> long_name )
1144- pos += fprintf (outfile , "--%s" , opts -> long_name );
1166+ if (opts -> long_name ) {
1167+ const char * long_name = opts -> long_name ;
1168+ if ((opts -> flags & PARSE_OPT_NONEG ) ||
1169+ skip_prefix (long_name , "no-" , & positive_name ))
1170+ pos += fprintf (outfile , "--%s" , long_name );
1171+ else
1172+ pos += fprintf (outfile , "--[no-]%s" , long_name );
1173+ }
1174+
11451175 if (opts -> type == OPTION_NUMBER )
11461176 pos += utf8_fprintf (outfile , _ ("-NUM" ));
11471177
11481178 if ((opts -> flags & PARSE_OPT_LITERAL_ARGHELP ) ||
11491179 !(opts -> flags & PARSE_OPT_NOARG ))
11501180 pos += usage_argh (opts , outfile );
11511181
1152- if (pos == USAGE_OPTS_WIDTH + 1 )
1153- pad = -1 ;
1154- else if (pos <= USAGE_OPTS_WIDTH )
1155- pad = USAGE_OPTS_WIDTH - pos ;
1156- else {
1157- fputc ('\n' , outfile );
1158- pad = USAGE_OPTS_WIDTH ;
1159- }
11601182 if (opts -> type == OPTION_ALIAS ) {
1161- fprintf (outfile , "%*s" , pad + USAGE_GAP , "" );
1183+ usage_padding (outfile , pos );
11621184 fprintf_ln (outfile , _ ("alias of --%s" ),
11631185 (const char * )opts -> value );
11641186 continue ;
11651187 }
11661188
11671189 for (cp = _ (opts -> help ); * cp ; cp = np ) {
11681190 np = strchrnul (cp , '\n' );
1169- fprintf (outfile ,
1170- "%*s%.*s\n" , pad + USAGE_GAP , "" ,
1171- (int )(np - cp ), cp );
1191+ usage_padding (outfile , pos );
1192+ fprintf (outfile , "%.*s\n" , (int )(np - cp ), cp );
11721193 if (* np )
11731194 np ++ ;
1174- pad = USAGE_OPTS_WIDTH ;
1195+ pos = 0 ;
1196+ }
1197+
1198+ if (positive_name ) {
1199+ if (find_option_by_long_name (all_opts , positive_name ))
1200+ continue ;
1201+ pos = usage_indent (outfile );
1202+ pos += fprintf (outfile , "--%s" , positive_name );
1203+ usage_padding (outfile , pos );
1204+ fprintf_ln (outfile , _ ("opposite of --no-%s" ),
1205+ positive_name );
11751206 }
11761207 }
11771208 fputc ('\n' , outfile );
0 commit comments