libk  parse.fn.c at [ebe506a75e]

File mod/kcli/parse.fn.c artifact ff83a12d45 part of check-in ebe506a75e


#include <k/cli.h>

kcli_cond
kcli_parse(kcli_set prg) {
	bool no_more_opts = false;
	const kcli_param* param = prg.params;

	for (sz i = 0; (i < prg.argc) && (prg.args[i] != null); ++i) {
		if (no_more_opts) goto is_param;

		const char* const arg = prg.args[i];
		if (arg[0] == '-') {
			if(arg[1] == '-') {
				if (arg[2] == 0) { no_more_opts = true; continue; }
				else if (arg[2] == '@') {
					// TODO: implement reading parameters from files
					continue;
				} else goto is_long_opt;
			} else if (arg[1] == '@') {
				// TODO: implement newline-separated file reading behavior
				continue;
			} else if (arg[1] == 0) {
				goto is_param;
			} else {
				goto is_short_opt;
			}
		} else if (arg[0] == '@') {
			// TODO: implement parsing file for options and parameters
			continue;
		} else {
			goto is_param;
		}

		is_short_opt: {
			continue;
		}
		is_long_opt: {
			continue;
		}
		is_param: {
			void* val = param -> val;
			if (param -> kind & kcli_param_int) {
				ubyte base = param->kind & ~kcli_param_int;
				/* TODO: parse integer */
			} else switch (param -> kind) {
				case kcli_param_none: continue;
				case kcli_param_string: *(char const**)val = arg; break;
				case kcli_param_enum: /* TODO */ break;
				default: return kcli_cond_spec;
			}
		}
	}
}