23#define eprintf(fmt, ...) \
24 tcol_fprintf(stderr, "{R+}error:{0}" fmt, ##__VA_ARGS__);
39 while (*argv) argv++, length++;
60 for (
size_t i = 0; i < app->
_length; i++) {
78 cmdopt_t** conflicts,
const char* description,
83 if (strncmp(longo,
"help", 5) == 0) {
85 }
else if (strncmp(longo,
"version", 8) == 0) {
90 option->
longo = longo;
92 option->
flags = flags;
94 const size_t conflict_count = (conflicts == NULL)
98 + conflict_count *
sizeof(
cmdopt_t*);
102 if (conflicts == NULL) {
105 memcpy(arg_int->
conflicts, conflicts, conflict_count
123 tcol_printf(
"{Y}Usage:{0} {+}%s{0} {G}[OPTION]{-}...{0} {M}ARG {-}...{0}\n", app->
_argv[0]);
131 for (
size_t i = 0; i < app->
_length; i++) {
164 "Display this information");
168 "Display program version information");
180 for (
size_t i = 0; i < app->
_length; i++) {
186 for (
size_t i = 0; i < app->
_length; i++) {
195#define IS_END_OF_FLAGS(str) \
196 ((str)[0] == '-' && (str)[1] == '-' && (str)[2] == 0)
197#define IS_LONG_FLAG(str) ((str)[0] == '-' && (str)[1] == '-')
198#define IS_SHORT_FLAG(str) ((str)[0] == '-')
201 for (
size_t i = 0; i < app->
_length; i++) {
205 eprintf(
"Required option -%c not passed\n",
210 if (!conflicts)
continue;
211 for (
size_t i = 0; conflicts[i]; i++) {
213 eprintf(
"Cannot pass both -%c and -%c\n",
230 #define APPEND_ARG(arg) if (app->_args.length + 1 > args_cap) { \
231 args_cap += (args_cap / 2); \
232 app->_args.contents = realloc(app->_args.contents, \
233 sizeof(char*) * args_cap); \
236 app->_proc(app->_user_data, NULL, arg); \
238 app->_args.contents[app->_args.length++] = arg;
240 bool only_args =
false;
241 for (
int i = 0; i < app->
_argc; i++) {
242 char* current = app->
_argv[i];
247 const char* next = app->
_argv[i + 1];
255 char*
arg = strchr(current,
'=');
263 if (
arg == NULL || *
arg == 0) {
264 eprintf(
"%s expects an argument\n", current);
272 eprintf(
"%s does not take arguments\n", current);
277 if (strncmp(current,
"--help", 7) == 0) {
281 }
else if (strncmp(current,
"--version", 10) == 0) {
286 eprintf(
"Unrecognized command line option %s, try --help\n", current);
299 if (!current[2] && next && next[0] !=
'-') {
302 }
else if (current[2] == 0) {
303 eprintf(
"-%c expects an argument\n", current[1]);
306 }
else if ((current[2] != 0) || (next && next[0] !=
'-')) {
307 eprintf(
"-%c does not take arguments\n", current[1]);
311 eprintf(
"Unrecognized command line option -%c, try --help\n",
320 for (
size_t j = 1; current[j]; j++) {
324 if (next && next[0] !=
'-') {
328 eprintf(
"-%c expects an argument\n", current[1]);
333 eprintf(
"Unrecognized command line option -%c\n",
360#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
365 #ifdef _POSIX_VERSION
366 if (isatty(STDERR_FILENO) || (getenv(
"CMDAPP_COLOR_ALWAYS") != NULL)) {
367 fprintf(stderr,
"%s: {R+}error:{0} ", app->
_info.
program);
375 va_start(valist,
fmt);
376 vfprintf(stderr,
fmt, valist);
void cmdapp_set(cmdapp_t *app, char shorto, const char *longo, uint8_t flags, cmdopt_t **conflicts, const char *description, const char *argtype, cmdopt_t *option)
void cmdapp_enable_procedure(cmdapp_t *app, cmdapp_procedure_t proc, void *user_data)
static size_t _argvlen(void **argv)
static int cmdapp_resolve_options(cmdapp_t *app)
void cmdapp_print_version(cmdapp_t *app)
#define IS_LONG_FLAG(str)
#define IS_END_OF_FLAGS(str)
#define IS_SHORT_FLAG(str)
void cmdapp_print_help(cmdapp_t *app)
cmdargs_t * cmdapp_getargs(cmdapp_t *app)
int cmdapp_run(cmdapp_t *app)
static cmdarg_internal_t * cmdapp_search(cmdapp_t *app, char shorto, const char *longo)
struct _cmdopt_internal_t cmdarg_internal_t
void cmdapp_error(cmdapp_t *app, const char *fmt,...)
void cmdapp_destroy(cmdapp_t *app)
static void cmdapp_append(cmdapp_t *app, cmdarg_internal_t *arg_int)
void cmdapp_init(cmdapp_t *app, int argc, char **argv, cmdapp_mode_t mode, const cmdapp_info_t *info)
#define CMDOPT_MAYTAKEARG
#define _CMDAPP_MODE_EXIT
void(* cmdapp_procedure_t)(void *data, cmdopt_t *option, const char *arg)
#define cmdopt_exists(opt)
#define CMDAPP_MODE_SHORTARG
#define cmdopt_is_optional(opt)
cmdopt_internal_t ** _start
int tcol_printf(const char *fmt,...)