9# define maug_add_arg( arg, arg_sz, help, help_sz, arg_cb, data ) (0)
24#ifndef MAUG_CLI_SIGIL_SZ
25# define MAUG_CLI_SIGIL_SZ 1
28#if !defined( MAUG_CLI_SIGIL ) && defined( MAUG_OS_WIN )
29# define MAUG_CLI_SIGIL "/"
30#elif !defined( MAUG_CLI_SIGIL ) && defined( MAUG_OS_DOS )
31# define MAUG_CLI_SIGIL "/"
32#elif !defined( MAUG_CLI_SIGIL ) && defined( MAUG_OS_DOS_REAL )
33# define MAUG_CLI_SIGIL "/"
34#elif !defined( MAUG_CLI_SIGIL )
39# define MAUG_CLI_SIGIL "-"
42#define MAUG_CLI_ARG_C_DEFAULT -1
44#ifndef MAUG_CLI_ARG_LIST_SZ_MAX
45# define MAUG_CLI_ARG_LIST_SZ_MAX 20
48#ifndef MAUG_CLI_ARG_HELP_SZ_MAX
49# define MAUG_CLI_ARG_HELP_SZ_MAX 127
52#ifndef MAUG_CLI_ARG_SZ_MAX
53# define MAUG_CLI_ARG_SZ_MAX 20
57MERROR_RETVAL (*maug_cli_cb)(
const char* arg, ssize_t arg_c,
void* data );
60#define MAUG_CLI( f ) \
61 f( MAUG_CLI_SIGIL "h", 3, "Display help and exit.", maug_cli_h )
63int maug_parse_args(
int argc,
char* argv[] );
74 const char* arg,
int arg_sz,
const char* help,
int help_sz,
75 maug_cli_cb arg_cb,
void* data );
81#define MAUG_CLI_ARG_ARG( arg, arg_sz, help, arg_callback ) \
84static char g_maug_cli_args[MAUG_CLI_ARG_LIST_SZ_MAX][MAUG_CLI_ARG_SZ_MAX] = {
89#define MAUG_CLI_ARG_SZ( arg, arg_sz, help, arg_callback ) \
92static int g_maug_cli_arg_sz[MAUG_CLI_ARG_LIST_SZ_MAX] = {
97#define MAUG_CLI_ARG_HELP( arg, arg_sz, help, arg_callback ) \
100static char g_maug_cli_arg_help[MAUG_CLI_ARG_LIST_SZ_MAX][MAUG_CLI_ARG_HELP_SZ_MAX] = {
105#define MAUG_CLI_ARG_DATA( arg, arg_sz, help, arg_callback ) NULL,
106static void* g_maug_cli_data[MAUG_CLI_ARG_LIST_SZ_MAX] = {
111#define MAUG_CLI_ARG_CALLED( arg, arg_sz, help, arg_callback ) 0,
112static int g_maug_cli_arg_called[MAUG_CLI_ARG_LIST_SZ_MAX] = {
119static int maug_cli_h(
const char* arg, ssize_t arg_c,
void* args ) {
127 fprintf( stderr,
"usage:\n\n" );
130 while(
'\0' != g_maug_cli_args[i][0] ) {
131 fprintf( stderr,
"\t%s\t%s\n", g_maug_cli_args[i],
132 g_maug_cli_arg_help[i] );
136 fprintf( stderr,
"\n" );
145#define MAUG_CLI_ARG_CB( arg, arg_sz, help, arg_callback ) \
148maug_cli_cb g_maug_cli_arg_callbacks[MAUG_CLI_ARG_LIST_SZ_MAX] = {
153int maug_parse_args(
int argc,
char* argv[] ) {
159 for( arg_i = 1 ; argc > arg_i ; arg_i++ ) {
160 debug_printf( 1,
"found arg: %s", argv[arg_i] );
162 while(
'\0' != g_maug_cli_args[const_i][0] ) {
164 g_maug_cli_args[const_i],
166 g_maug_cli_arg_sz[const_i]
172 g_maug_cli_arg_called[const_i]++;
173 retval = g_maug_cli_arg_callbacks[const_i]( argv[arg_i],
174 g_maug_cli_arg_called[const_i], g_maug_cli_data[const_i] );
175 if( MERROR_OK != retval ) {
185 if(
'\0' == g_maug_cli_args[const_i][0] ) {
188 g_maug_cli_arg_called[last_i]++;
189 retval = g_maug_cli_arg_callbacks[last_i]( argv[arg_i],
190 g_maug_cli_arg_called[last_i], g_maug_cli_data[last_i] );
191 if( MERROR_OK != retval ) {
199 while(
'\0' != g_maug_cli_args[const_i][0] ) {
201 0 == g_maug_cli_arg_called[const_i] &&
202 NULL != g_maug_cli_arg_callbacks[const_i]
204 debug_printf( 1,
"calling default arg for uncalled \"%s\"...",
205 g_maug_cli_args[const_i] );
207 g_maug_cli_arg_callbacks[const_i](
208 "", MAUG_CLI_ARG_C_DEFAULT, g_maug_cli_data[const_i] );
209 if( MERROR_OK != retval ) {
224 const char* arg,
int arg_sz,
const char* help,
int help_sz,
225 maug_cli_cb arg_cb,
void* data
231 '\0' != g_maug_cli_args[slot_idx][0] &&
232 MAUG_CLI_ARG_LIST_SZ_MAX > slot_idx
240 if( MAUG_CLI_ARG_LIST_SZ_MAX - 1 <= slot_idx ) {
241 return MERROR_OVERFLOW;
245 arg_sz = maug_strlen( arg );
247 assert( arg_sz < MAUG_CLI_ARG_SZ_MAX );
250 help_sz = maug_strlen( help );
252 assert( help_sz < MAUG_CLI_ARG_HELP_SZ_MAX );
256 maug_strncpy( g_maug_cli_args[slot_idx], arg, arg_sz );
257 g_maug_cli_args[slot_idx + 1][0] =
'\0';
259 maug_strncpy( g_maug_cli_arg_help[slot_idx], help, help_sz );
260 g_maug_cli_arg_help[slot_idx + 1][0] =
'\0';
262 g_maug_cli_arg_sz[slot_idx] = arg_sz;
263 g_maug_cli_arg_sz[slot_idx + 1] = 0;
265 g_maug_cli_arg_callbacks[slot_idx] = arg_cb;
266 g_maug_cli_arg_callbacks[slot_idx + 1] = NULL;
268 g_maug_cli_arg_called[slot_idx] = 0;
269 g_maug_cli_arg_called[slot_idx + 1] = 0;
271 g_maug_cli_data[slot_idx] = data;
272 g_maug_cli_data[slot_idx + 1] = NULL;
MERROR_RETVAL maug_add_arg(const char *arg, int arg_sz, const char *help, int help_sz, maug_cli_cb arg_cb, void *data)
Add a command-line argument to the built-in parser.
#define MAUG_CLI(f)
Default CLI arguments for all RetroFlat programs.
Definition marge.h:60
int MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:19