Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ target_compile_features(optparse99
set_target_properties(optparse99
PROPERTIES
C_STANDARD 99
C_STANDARD_REQUIRED 99)
C_STANDARD_REQUIRED ON)

target_compile_definitions(optparse99
PUBLIC
Expand Down
49 changes: 33 additions & 16 deletions optparse99.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void optparse_error(char *fmt, ...)
vfprintf(stderr, fmt, ap);
va_end(ap);
#if OPTPARSE_PRINT_HELP_ON_ERROR
optparse_fprint_help(stderr, EXIT_FAILURE);
optparse_fprint_help(stderr, EXIT_FAILURE, false);
#endif
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -836,7 +836,14 @@ static void blockprint(FILE *stream, char *str, int first_line_indent,
if (first_line_printed) {
fprintf(stream, "%*s", indent, "");
} else {
width = end - first_line_indent;
if (first_line_indent > end) {
// Indentation exceeds block width
// Intentionally break into new line
width = 0;
} else {
// Indentation does not exceed block width
width = end - first_line_indent;
}
}

int n = 0; // Number of characters to be printed on the current line
Expand Down Expand Up @@ -1264,7 +1271,7 @@ static void print_subcommands(FILE *stream, struct optparse_cmd subcommands[])
// Prints a command's complete help information: about, usage, description,
// options, subcommands.
// cmd_chain: a NULL-terminated array that contains a valid command chain
static void print_help(FILE *stream, struct optparse_cmd *cmd, int exit_status)
static void print_help(FILE *stream, struct optparse_cmd *cmd, int exit_status, bool noExit)
{
if (stream != stderr && cmd->about) {
blockprint(stream, cmd->about, 0, 0, OPTPARSE_HELP_MAX_LINE_WIDTH);
Expand Down Expand Up @@ -1306,7 +1313,8 @@ static void print_help(FILE *stream, struct optparse_cmd *cmd, int exit_status)
}
#endif

exit(exit_status);
if (!noExit)
exit(exit_status);
}

#if OPTPARSE_SUBCOMMANDS
Expand Down Expand Up @@ -1440,23 +1448,23 @@ char *optparse_unshift(void)
}

// Prints the currently active command's help information.
void optparse_print_help(void)
void optparse_print_help(bool noExit)
{
#if OPTPARSE_SUBCOMMANDS
print_help(help_stream, active_cmd, EXIT_SUCCESS);
print_help(help_stream, active_cmd, EXIT_SUCCESS, noExit);
#else
print_help(help_stream, optparse_main_cmd, EXIT_SUCCESS);
print_help(help_stream, optparse_main_cmd, EXIT_SUCCESS, noExit);
#endif
}

// Same as optparse_print_help, but prints to the specified stream. Exits with
// the provided exit status.
void optparse_fprint_help(FILE *stream, int exit_status)
void optparse_fprint_help(FILE *stream, int exit_status, bool noExit)
{
#if OPTPARSE_SUBCOMMANDS
print_help(stream, active_cmd, exit_status);
print_help(stream, active_cmd, exit_status, noExit);
#else
print_help(stream, optparse_main_cmd, exit_status);
print_help(stream, optparse_main_cmd, exit_status, noExit);
#endif
}

Expand All @@ -1471,19 +1479,28 @@ void optparse_fprint_usage(FILE *stream)
}

#if OPTPARSE_SUBCOMMANDS
// Prints a subcommand's help by parsing remaining operands. To be used as a
// command structure's .function member.
void optparse_print_help_subcmd(int argc, char **argv)
{
static inline void __optparse_print_help_subcmd(int argc, char **argv, bool noExit) {
(void) argc; // To avoid compilers complaining about "unused parameter".
argv++; // To ignore the program's file name
if (*argv) {
struct optparse_cmd *subcmd = read_cmd_chain(optparse_main_cmd, argv);
print_help(stdout, subcmd, EXIT_SUCCESS);
print_help(stdout, subcmd, EXIT_SUCCESS, noExit);
} else {
print_help(stdout, optparse_main_cmd, EXIT_SUCCESS);
print_help(stdout, optparse_main_cmd, EXIT_SUCCESS, noExit);
}
}

// Prints a subcommand's help by parsing remaining operands. To be used as a
// command structure's .function member.
void optparse_print_help_subcmd(int argc, char **argv)
{
__optparse_print_help_subcmd(argc, argv, false);
}

void optparse_print_help_subcmd_noexit(int argc, char **argv)
{
__optparse_print_help_subcmd(argc, argv, true);
}
#endif

// Converts a string to a different data type.
Expand Down
6 changes: 4 additions & 2 deletions optparse99.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ void optparse_parse(struct optparse_cmd *cmd, int *argc, char ***argv);
// Prints the currently active command's full help information, listing
// available options and their descriptions. It can be called manuall or through
// an option's function member. Exits with exit status EXIT_SUCCESS.
void optparse_print_help(void);
void optparse_print_help(bool noExit);

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

// Prints the currently active command's usage information only.
void optparse_fprint_usage(FILE *stream);
Expand All @@ -311,6 +311,8 @@ void optparse_fprint_usage(FILE *stream);
// Prints a subcommand's help by parsing remaining operands. To be used as a
// command structure's .function member.
void optparse_print_help_subcmd(int argc, char **argv);

void optparse_print_help_subcmd_noexit(int argc, char **argv);
#endif

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