Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
cli.c
Go to the documentation of this file.
1
17
18#include <banjo/cli.h>
19#include <banjo/log.h>
20#include <banjo/main.h>
21#include <banjo/memory.h>
22
23#include <stdio.h>
24
25int main(int argc, char* argv[]) {
26 // Declare variables to store parsed values. Set defaults here - if the
27 // user doesn't provide a value, these defaults remain unchanged.
28 int verbose = 0;
29 const char* input_file = "default.txt";
30 const char* output_file = NULL;
31 int count = 1;
32 int threads = 4;
33 double tolerance = 0.001;
34 int enable_feature = 0;
35
36 // Define argument specifications. Each bj_cli_argument describes one option.
37 // Fields:
38 // - name: long option name (--name)
39 // - shortname: single-character shortcut (-n)
40 // - help: description shown in --help
41 // - metavar: placeholder for value display (--count <N>)
42 // - action: function to parse and store the value
43 // - dest: pointer to variable that receives the value
44 // - required: whether this argument must be provided
45 struct bj_cli_argument args[] = {
46 // bj_print_cli_help_action: special action that prints help and exits.
47 // Use this for --help flags. No dest needed.
48 {
49 .shortname = 'h',
50 .name = "help",
51 .help = "Show this help message and exit",
53 },
54
55 // Boolean flag (no action specified): toggles the variable to 1 if present.
56 // Usage: -v or --verbose sets verbose=1
57 {
58 .shortname = 'v',
59 .name = "verbose",
60 .help = "Enable verbose output",
61 .dest = &verbose
62 },
63
64 // bj_store_cli_cstring: parses the next argument as a string.
65 // Usage: --input file.txt or -i file.txt
66 {
67 .shortname = 'i',
68 .name = "input",
69 .help = "Input file path",
70 .metavar = "FILE",
71 .action = bj_store_cli_cstring,
72 .dest = &input_file
73 },
74
75 // bj_store_cli_int: parses the next argument as an integer.
76 // Usage: --count 42 or -c 42
77 {
78 .shortname = 'c',
79 .name = "count",
80 .help = "Number of iterations to perform",
81 .metavar = "N",
82 .action = bj_store_cli_int,
83 .dest = &count
84 },
85
86 // Another integer option. Can have shortname or not.
87 {
88 .shortname = 't',
89 .name = "threads",
90 .help = "Number of worker threads",
91 .metavar = "NUM",
92 .action = bj_store_cli_int,
93 .dest = &threads
94 },
95
96 // bj_store_cli_double: parses the next argument as a floating-point number.
97 // Usage: --tolerance 0.001
98 {
99 .name = "tolerance",
100 .help = "Tolerance level for calculations",
101 .metavar = "TOL",
102 .action = bj_store_cli_double,
103 .dest = &tolerance
104 },
105
106 // bj_store_cli_bool: parses true/false, yes/no, 1/0 as boolean.
107 // Usage: --enable-feature true
108 {
109 .name = "enable-feature",
110 .help = "Enable experimental feature (true/false)",
111 .metavar = "BOOL",
112 .action = bj_store_cli_bool,
113 .dest = &enable_feature
114 },
115
116 // Positional argument: no name or shortname, just help and metavar.
117 // Matches arguments that don't start with dashes, in order.
118 // Usage: program [options] output.txt
119 {
120 .help = "Output file path (optional)",
121 .metavar = "OUTPUT",
122 .action = bj_store_cli_cstring,
123 .dest = &output_file,
124 .required = 0
125 },
126 };
127
128 // Create the parser with program metadata.
129 // description: shown in help before options list
130 // epilog: shown in help after options list (good for examples)
131 // The parser auto-generates help text from argument definitions.
132 struct bj_cli parser = {
133 .prog = "example_cli",
134 .description = "Example program demonstrating Banjo's argument parsing.\n"
135 "Shows flags, named options, type conversion, and positional arguments.",
136 .epilog = "Examples:\n"
137 " example_cli -v -i data.txt output.txt\n"
138 " example_cli --count 10 --threads 8 --tolerance 0.01\n"
139 " example_cli -vci input.txt -t 4 result.txt",
140 .arguments_len = sizeof(args) / sizeof(args[0]),
141 .arguments = args,
142 };
143
144 // Parse command-line arguments. This processes argv, calls actions to
145 // convert and store values, and validates required arguments.
146 // Returns false if parsing fails (unknown option, missing value, etc.)
147 // On failure, print help and exit with error code.
148 if (!bj_parse_cli(&parser, argc, argv, 0)) {
149 bj_print_cli_help(&parser);
150 return 1;
151 }
152
153 // After successful parsing, all variables contain the parsed values.
154 // Just use them normally - the parser has already converted types and
155 // validated everything. If a value wasn't provided, it still has the
156 // default you initialised it with.
157 bj_info("=== Parsed Arguments ===");
158 bj_info("Verbose: %s", verbose ? "enabled" : "disabled");
159 bj_info("Input File: %s", input_file);
160 bj_info("Count: %d", count);
161 bj_info("Threads: %d", threads);
162 bj_info("Tolerance: %.6f", tolerance);
163 bj_info("Feature Enabled: %s", enable_feature ? "yes" : "no");
164
165 if (output_file) {
166 bj_info("Output File: %s", output_file);
167 } else {
168 bj_info("Output File: (not specified)");
169 }
170
171 bj_info("");
172 bj_info("=== Simulating Work ===");
173 if (verbose) {
174 for (int i = 0; i < count; i++) {
175 bj_info("Processing iteration %d/%d...", i + 1, count);
176 }
177 } else {
178 bj_info("Processing %d iterations...", count);
179 }
180
181 bj_info("Done!");
182
183 return 0;
184}
int main(int argc, char *argv[])
Definition audio_pcm.c:177
POSIX/GNU-like command-line argument parser.
bj_bool bj_store_cli_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_store_cli_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.
void bj_print_cli_help(const struct bj_cli *parser)
Print help message using Banjo's logging system.
bj_bool bj_store_cli_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_store_cli_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_parse_cli(struct bj_cli *parser, int argc, char *argv[], struct bj_error **error)
Parse command-line arguments according to parser configuration.
bj_bool bj_print_cli_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.
Parser context and argument list descriptor.
Definition cli.h:173
Descriptor for a single command line argument.
Definition cli.h:151
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:141
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
All memory-related functions, including custom allocators.