Skip to content

Commit 072ea5d

Browse files
committed
Add #ifndef gaurds for #defines and rename, document, and add cma�
�ke options for two `#define`s. As for my previous PR, I forgot to take into account that some of the `#define`s did not have `#ifndef` guards. This breaks the cmake options. This fixes that. I also renamed two defines aswell as documented them and gave the respective options in `CMakeLists.txt`.
1 parent 2f44e05 commit 072ea5d

3 files changed

Lines changed: 38 additions & 19 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ target_compile_features(optparse99
4646
set_target_properties(optparse99
4747
PROPERTIES
4848
C_STANDARD 99
49-
C_STANDARD_REQUIRED 99)
49+
C_STANDARD_REQUIRED ON)
5050

5151
target_compile_definitions(optparse99
5252
PUBLIC

optparse99.c

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

optparse99.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ void optparse_parse(struct optparse_cmd *cmd, int *argc, char ***argv);
298298
// Prints the currently active command's full help information, listing
299299
// available options and their descriptions. It can be called manuall or through
300300
// an option's function member. Exits with exit status EXIT_SUCCESS.
301-
void optparse_print_help(void);
301+
void optparse_print_help(bool noExit);
302302

303303
// Same as optparse_print_help, but can only be called manually. It prints to
304304
// the specified stream and exits with the provided exit status.
305-
void optparse_fprint_help(FILE *stream, int exit_status);
305+
void optparse_fprint_help(FILE *stream, int exit_status, bool noExit);
306306

307307
// Prints the currently active command's usage information only.
308308
void optparse_fprint_usage(FILE *stream);
@@ -311,6 +311,8 @@ void optparse_fprint_usage(FILE *stream);
311311
// Prints a subcommand's help by parsing remaining operands. To be used as a
312312
// command structure's .function member.
313313
void optparse_print_help_subcmd(int argc, char **argv);
314+
315+
void optparse_print_help_subcmd_noexit(int argc, char **argv);
314316
#endif
315317

316318
// Advances the parser's internal index and returns the next command line

0 commit comments

Comments
 (0)