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_HELP_SZ_MAX
45# define MAUG_CLI_ARG_HELP_SZ_MAX 127
48#ifndef MAUG_CLI_ARG_SZ_MAX
49# define MAUG_CLI_ARG_SZ_MAX 20
52#ifndef MAUG_CLI_TRACE_LVL
53# define MAUG_CLI_TRACE_LVL 0
57MERROR_RETVAL (*maug_cli_cb)(
const char* arg, ssize_t arg_c,
void* data );
60 char arg[MAUG_CLI_ARG_SZ_MAX];
62 char help[MAUG_CLI_ARG_HELP_SZ_MAX];
68MERROR_RETVAL maug_cli_h(
const char* arg, ssize_t arg_c,
void* args );
70int maug_parse_args(
int argc,
char* argv[] );
81 const char* arg,
int arg_sz,
const char* help,
int help_sz,
82 maug_cli_cb arg_cb,
void* data );
92MERROR_RETVAL maug_cli_h(
const char* arg, ssize_t arg_c,
void* args ) {
101 error_printf(
"usage:" );
102 error_printf(
"---" );
105 for( i = 0 ; mdata_vector_ct( &g_marge_args ) > i ; i++ ) {
106 arg_p = mdata_vector_get( &g_marge_args, i,
struct MARGE_ARG );
107 error_printf(
"\t%s\t%s", arg_p->arg, arg_p->help );
110 error_printf(
"---" );
125 debug_printf( MAUG_CLI_TRACE_LVL,
"parsing %d args...", argc );
127 mdata_vector_lock( &g_marge_args );
128 for( arg_i = 1 ; argc > arg_i ; arg_i++ ) {
129 debug_printf( MAUG_CLI_TRACE_LVL,
"found CLI: %s", argv[arg_i] );
136 const_i = 0 ; mdata_vector_ct( &g_marge_args ) > const_i ; const_i++
138 arg = mdata_vector_get( &g_marge_args, const_i,
struct MARGE_ARG );
140 if( 0 == strncmp( arg->arg, argv[arg_i], arg->arg_sz ) ) {
141 debug_printf( MAUG_CLI_TRACE_LVL,
"arg matched: %s", arg->arg );
148 retval = arg->callback( argv[arg_i], arg->called, arg->data );
149 if( MERROR_OK != retval ) {
150 error_printf(
"error calling arg!" );
156 MAUG_CLI_TRACE_LVL,
"arg parsed, moving on to next..." );
159 debug_printf( MAUG_CLI_TRACE_LVL,
"arg NOT matched: %s", arg->arg );
165 arg = mdata_vector_get( &g_marge_args, last_i,
struct MARGE_ARG );
168 MAUG_CLI_TRACE_LVL,
"passing data to previous arg..." );
170 retval = arg->callback( argv[arg_i], arg->called, arg->data );
171 if( MERROR_OK != retval ) {
172 error_printf(
"error calling arg!" );
178 debug_printf( MAUG_CLI_TRACE_LVL,
"calling defaults for uncalled args..." );
182 for( const_i = 0 ; mdata_vector_ct( &g_marge_args ) > const_i ; const_i++ ) {
183 arg = mdata_vector_get( &g_marge_args, const_i,
struct MARGE_ARG );
185 debug_printf( MAUG_CLI_TRACE_LVL,
186 "checking arg %d: %s (" SIZE_T_FMT
"): callback: %p",
187 const_i, arg->arg, arg->arg_sz, arg->callback );
188 if( NULL == arg->callback ) {
190 MAUG_CLI_TRACE_LVL,
"arg %d callback is NULL!", const_i );
193 if( 0 != arg->called ) {
195 MAUG_CLI_TRACE_LVL,
"arg %d was called; NOT calling default...",
199 debug_printf( MAUG_CLI_TRACE_LVL,
200 "calling default arg for uncalled \"%s\"...", arg->arg );
201 retval = arg->callback(
"", MAUG_CLI_ARG_C_DEFAULT, arg->data );
202 if( MERROR_OK != retval ) {
203 error_printf(
"error calling arg default!" );
210 mdata_vector_unlock( &g_marge_args );
218 const char* arg,
int arg_sz,
const char* help,
int help_sz,
219 maug_cli_cb arg_cb,
void* data
227 debug_printf( MAUG_CLI_TRACE_LVL,
"adding arg: \"%s\"", arg );
229 maug_strncpy( new_arg.arg, arg, MAUG_CLI_ARG_SZ_MAX );
230 maug_strncpy( new_arg.help, help, MAUG_CLI_ARG_HELP_SZ_MAX );
231 new_arg.arg_sz = arg_sz;
232 new_arg.callback = arg_cb;
236 idx = mdata_vector_append(
237 &g_marge_args, &new_arg,
sizeof(
struct MARGE_ARG ) );
240 retval = mdata_retval( idx );
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.
int MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:19
A vector of uniformly-sized objects, stored contiguously.
Definition mdata.h:85