libcli/include/cli.h

48 lines
1 KiB
C

#ifndef CLI_H
#define CLI_H
#include <stddef.h>
#include <getopt.h>
enum cli_code {
CLI_EXIT_SUCCESS = -1,
CLI_SUCCESS = 0,
CLI_ERROR = 1
};
typedef int (*t_option_handler)(const char *arg, void *config);
typedef const char *(*t_arg_type_to_str)(int type);
struct option_descriptor
{
char short_opt;
const char *long_opt;
int has_arg;
t_option_handler handler;
int arg_type;
const char *description;
};
/* Default arg types — clients may define their own */
#define CLI_OPT_ARG_TYPES \
X(OPT_ARG_NONE, "" ) \
X(OPT_ARG_INT, "<NUM>") \
X(OPT_ARG_UINT, "<NUM>") \
X(OPT_ARG_FLOAT, "<NUM>") \
X(OPT_ARG_STRING, "<STR>")
#define X(name, str) name,
enum { CLI_OPT_ARG_TYPES };
#undef X
void cli_set_prog_name(const char *name);
void cli_print_options(const struct option_descriptor *opts, size_t nb_opts,
t_arg_type_to_str to_str);
const char *cli_arg_type_to_str(int type);
enum cli_code cli_parse(int argc, char **argv, void *config,
const struct option_descriptor *opts, size_t nb_opts,
char *opt_str, struct option *long_opts);
#endif