@@ -66,7 +66,7 @@ static void optparse_error(char *fmt, ...)
6666 vfprintf (stderr , fmt , ap );
6767 va_end (ap );
6868#if OPTPARSE_PRINT_HELP_ON_ERROR
69- optparse_fprint_help (stderr , EXIT_FAILURE );
69+ optparse_fprint_help (stderr , EXIT_FAILURE , false );
7070#endif
7171 exit (EXIT_FAILURE );
7272}
@@ -836,7 +836,14 @@ static void blockprint(FILE *stream, char *str, int first_line_indent,
836836 if (first_line_printed ) {
837837 fprintf (stream , "%*s" , indent , "" );
838838 } else {
839- width = end - first_line_indent ;
839+ if (first_line_indent > end ) {
840+ // Indentation exceeds block width
841+ // Intentionally break into new line
842+ width = 0 ;
843+ } else {
844+ // Indentation does not exceed block width
845+ width = end - first_line_indent ;
846+ }
840847 }
841848
842849 int n = 0 ; // Number of characters to be printed on the current line
@@ -1264,7 +1271,7 @@ static void print_subcommands(FILE *stream, struct optparse_cmd subcommands[])
12641271// Prints a command's complete help information: about, usage, description,
12651272// options, subcommands.
12661273// cmd_chain: a NULL-terminated array that contains a valid command chain
1267- static void print_help (FILE * stream , struct optparse_cmd * cmd , int exit_status )
1274+ static void print_help (FILE * stream , struct optparse_cmd * cmd , int exit_status , bool noExit )
12681275{
12691276 if (stream != stderr && cmd -> about ) {
12701277 blockprint (stream , cmd -> about , 0 , 0 , OPTPARSE_HELP_MAX_LINE_WIDTH );
@@ -1306,7 +1313,8 @@ static void print_help(FILE *stream, struct optparse_cmd *cmd, int exit_status)
13061313 }
13071314#endif
13081315
1309- exit (exit_status );
1316+ if (!noExit )
1317+ exit (exit_status );
13101318}
13111319
13121320#if OPTPARSE_SUBCOMMANDS
@@ -1440,23 +1448,23 @@ char *optparse_unshift(void)
14401448}
14411449
14421450// Prints the currently active command's help information.
1443- void optparse_print_help (void )
1451+ void optparse_print_help (bool noExit )
14441452{
14451453#if OPTPARSE_SUBCOMMANDS
1446- print_help (help_stream , active_cmd , EXIT_SUCCESS );
1454+ print_help (help_stream , active_cmd , EXIT_SUCCESS , noExit );
14471455#else
1448- print_help (help_stream , optparse_main_cmd , EXIT_SUCCESS );
1456+ print_help (help_stream , optparse_main_cmd , EXIT_SUCCESS , noExit );
14491457#endif
14501458}
14511459
14521460// Same as optparse_print_help, but prints to the specified stream. Exits with
14531461// the provided exit status.
1454- void optparse_fprint_help (FILE * stream , int exit_status )
1462+ void optparse_fprint_help (FILE * stream , int exit_status , bool noExit )
14551463{
14561464#if OPTPARSE_SUBCOMMANDS
1457- print_help (stream , active_cmd , exit_status );
1465+ print_help (stream , active_cmd , exit_status , noExit );
14581466#else
1459- print_help (stream , optparse_main_cmd , exit_status );
1467+ print_help (stream , optparse_main_cmd , exit_status , noExit );
14601468#endif
14611469}
14621470
@@ -1471,19 +1479,28 @@ void optparse_fprint_usage(FILE *stream)
14711479}
14721480
14731481#if OPTPARSE_SUBCOMMANDS
1474- // Prints a subcommand's help by parsing remaining operands. To be used as a
1475- // command structure's .function member.
1476- void optparse_print_help_subcmd (int argc , char * * argv )
1477- {
1482+ static inline void __optparse_print_help_subcmd (int argc , char * * argv , bool noExit ) {
14781483 (void ) argc ; // To avoid compilers complaining about "unused parameter".
14791484 argv ++ ; // To ignore the program's file name
14801485 if (* argv ) {
14811486 struct optparse_cmd * subcmd = read_cmd_chain (optparse_main_cmd , argv );
1482- print_help (stdout , subcmd , EXIT_SUCCESS );
1487+ print_help (stdout , subcmd , EXIT_SUCCESS , noExit );
14831488 } else {
1484- print_help (stdout , optparse_main_cmd , EXIT_SUCCESS );
1489+ print_help (stdout , optparse_main_cmd , EXIT_SUCCESS , noExit );
14851490 }
14861491}
1492+
1493+ // Prints a subcommand's help by parsing remaining operands. To be used as a
1494+ // command structure's .function member.
1495+ void optparse_print_help_subcmd (int argc , char * * argv )
1496+ {
1497+ __optparse_print_help_subcmd (argc , argv , false);
1498+ }
1499+
1500+ void optparse_print_help_subcmd_noexit (int argc , char * * argv )
1501+ {
1502+ __optparse_print_help_subcmd (argc , argv , true);
1503+ }
14871504#endif
14881505
14891506// Converts a string to a different data type.
0 commit comments