Command-line argument parsing with type conversion and validation.
Command-line argument parsing with type conversion and validation.Banjo's CLI parser automatically handles:
The pattern: declare storage variables, define argument specs with actions, create parser, parse, then use the values. Actions automatically convert and store parsed values into your variables.
#include <stdio.h>
int main(int argc, char* argv[]) {
int verbose = 0;
const char* input_file = "default.txt";
const char* output_file = NULL;
int count = 1;
int threads = 4;
double tolerance = 0.001;
int enable_feature = 0;
{
.shortname = 'h',
.name = "help",
.help = "Show this help message and exit",
},
{
.shortname = 'v',
.name = "verbose",
.help = "Enable verbose output",
.dest = &verbose
},
{
.shortname = 'i',
.name = "input",
.help = "Input file path",
.metavar = "FILE",
.dest = &input_file
},
{
.shortname = 'c',
.name = "count",
.help = "Number of iterations to perform",
.metavar = "N",
.dest = &count
},
{
.shortname = 't',
.name = "threads",
.help = "Number of worker threads",
.metavar = "NUM",
.dest = &threads
},
{
.name = "tolerance",
.help = "Tolerance level for calculations",
.metavar = "TOL",
.dest = &tolerance
},
{
.name = "enable-feature",
.help = "Enable experimental feature (true/false)",
.metavar = "BOOL",
.dest = &enable_feature
},
{
.help = "Output file path (optional)",
.metavar = "OUTPUT",
.dest = &output_file,
.required = 0
},
};
.prog = "example_cli",
.description = "Example program demonstrating Banjo's argument parsing.\n"
"Shows flags, named options, type conversion, and positional arguments.",
.epilog = "Examples:\n"
" example_cli -v -i data.txt output.txt\n"
" example_cli --count 10 --threads 8 --tolerance 0.01\n"
" example_cli -vci input.txt -t 4 result.txt",
.arguments_len = sizeof(args) / sizeof(args[0]),
.arguments = args,
};
return 1;
}
bj_info(
"=== Parsed Arguments ===");
bj_info(
"Verbose: %s", verbose ?
"enabled" :
"disabled");
bj_info(
"Input File: %s", input_file);
bj_info(
"Tolerance: %.6f", tolerance);
bj_info(
"Feature Enabled: %s", enable_feature ?
"yes" :
"no");
if (output_file) {
bj_info(
"Output File: %s", output_file);
} else {
bj_info(
"Output File: (not specified)");
}
bj_info(
"=== Simulating Work ===");
if (verbose) {
for (int i = 0; i < count; i++) {
bj_info(
"Processing iteration %d/%d...", i + 1, count);
}
} else {
bj_info(
"Processing %d iterations...", count);
}
return 0;
}
POSIX/GNU-like command-line argument parser.
bj_bool bj_cli_parse(struct bj_cli *parser, int argc, char *argv[], struct bj_error **error)
Parse command-line arguments according to parser configuration.
bj_bool bj_cli_store_cstring(const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error)
Store string argument value.
bj_bool bj_cli_store_double(const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error)
Store double argument value.
bj_bool bj_cli_store_int(const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error)
Store int argument value.
bj_bool bj_cli_store_bool(const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error)
Store boolean argument value.
bj_bool bj_cli_print_help_action(const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error)
Print help argument action.
void bj_cli_print_help(const struct bj_cli *parser)
Print help message using Banjo's logging system.
Parser context and argument list descriptor.
Definition cli.h:161
Descriptor for a single command line argument.
Definition cli.h:139
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:105
Logging utility functions.
Portable main substitution and application callback facilities.
All memory-related functions, including custom allocators.