Skip to content

Commit c11e659

Browse files
authored
Merge pull request DeusData#716 from DeusData/fix/680-cli-flags
feat(cli): accept flags, stdin, and --args-file for tools + per-tool --help (DeusData#680)
2 parents 96a2f55 + 3c2c120 commit c11e659

6 files changed

Lines changed: 511 additions & 1 deletion

File tree

‎src/cli/cli.c‎

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "foundation/compat.h"
99
#include "foundation/platform.h"
1010
#include "foundation/constants.h"
11+
#include "mcp/mcp.h" // cbm_mcp_tool_input_schema — CLI flag parser + per-tool --help
1112

1213
/* CLI buffer size constants. */
1314
enum {
@@ -4441,3 +4442,254 @@ int cbm_cmd_update(int argc, char **argv) {
44414442
(void)variant;
44424443
return 0;
44434444
}
4445+
4446+
/* ── CLI tool arguments (flags / --args-file / --help) ────────────── */
4447+
4448+
/* Flag-name normalization: kebab-case CLI flags map to snake_case JSON keys
4449+
* (--name-pattern -> name_pattern). In-place; buffer is NUL-terminated. */
4450+
static void cli_kebab_to_snake(char *s) {
4451+
for (; *s; s++) {
4452+
if (*s == '-') {
4453+
*s = '_';
4454+
}
4455+
}
4456+
}
4457+
4458+
/* snake_case JSON key -> kebab-case flag name (for --help display). In-place. */
4459+
static void cli_snake_to_kebab(char *s) {
4460+
for (; *s; s++) {
4461+
if (*s == '_') {
4462+
*s = '-';
4463+
}
4464+
}
4465+
}
4466+
4467+
/* Heap-format a one-argument error message for *err_out. Caller frees. */
4468+
static char *cli_heap_msgf(const char *fmt, const char *arg) {
4469+
char buf[CLI_BUF_512];
4470+
snprintf(buf, sizeof(buf), fmt, arg);
4471+
return cbm_strdup(buf);
4472+
}
4473+
4474+
/* True if the schema's required[] array contains `key`. */
4475+
static bool cli_schema_required_has(yyjson_val *required, const char *key) {
4476+
if (!required || !yyjson_is_arr(required)) {
4477+
return false;
4478+
}
4479+
size_t idx;
4480+
size_t max;
4481+
yyjson_val *v;
4482+
yyjson_arr_foreach(required, idx, max, v) {
4483+
if (yyjson_is_str(v) && strcmp(yyjson_get_str(v), key) == 0) {
4484+
return true;
4485+
}
4486+
}
4487+
return false;
4488+
}
4489+
4490+
/* Look up a property's JSON-schema "type" string (string/integer/number/
4491+
* boolean/array). Returns NULL when the schema or property is unknown — the
4492+
* caller then treats the value as a plain string. */
4493+
static const char *cli_schema_type(yyjson_val *props, const char *key) {
4494+
if (!props || !yyjson_is_obj(props)) {
4495+
return NULL;
4496+
}
4497+
yyjson_val *p = yyjson_obj_get(props, key);
4498+
if (!p || !yyjson_is_obj(p)) {
4499+
return NULL;
4500+
}
4501+
yyjson_val *t = yyjson_obj_get(p, "type");
4502+
return (t && yyjson_is_str(t)) ? yyjson_get_str(t) : NULL;
4503+
}
4504+
4505+
/* Append a typed value to the output object under `key`. For array-typed
4506+
* properties, repeated flags accumulate into a single JSON array. */
4507+
static void cli_add_typed(yyjson_mut_doc *out, yyjson_mut_val *obj, const char *key,
4508+
const char *type, const char *value, bool have_value) {
4509+
if (type && strcmp(type, "array") == 0) {
4510+
yyjson_mut_val *arr = yyjson_mut_obj_get(obj, key);
4511+
if (!arr || !yyjson_mut_is_arr(arr)) {
4512+
arr = yyjson_mut_arr(out);
4513+
yyjson_mut_obj_add(obj, yyjson_mut_strcpy(out, key), arr);
4514+
}
4515+
yyjson_mut_arr_add_strcpy(out, arr, have_value ? value : "");
4516+
return;
4517+
}
4518+
4519+
yyjson_mut_val *vv;
4520+
if (type && strcmp(type, "boolean") == 0) {
4521+
bool b = !have_value || strcmp(value, "true") == 0 || strcmp(value, "1") == 0 ||
4522+
strcmp(value, "yes") == 0;
4523+
vv = yyjson_mut_bool(out, b);
4524+
} else if (type && strcmp(type, "integer") == 0) {
4525+
char *endp = NULL;
4526+
const char *v = have_value ? value : "";
4527+
long n = strtol(v, &endp, CLI_STRTOL_BASE);
4528+
vv = (endp && endp != v && *endp == '\0') ? yyjson_mut_int(out, (int64_t)n)
4529+
: yyjson_mut_strcpy(out, v);
4530+
} else if (type && strcmp(type, "number") == 0) {
4531+
char *endp = NULL;
4532+
const char *v = have_value ? value : "";
4533+
double d = strtod(v, &endp);
4534+
vv = (endp && endp != v && *endp == '\0') ? yyjson_mut_real(out, d)
4535+
: yyjson_mut_strcpy(out, v);
4536+
} else {
4537+
/* string or unknown type */
4538+
vv = yyjson_mut_strcpy(out, have_value ? value : "");
4539+
}
4540+
yyjson_mut_obj_add(obj, yyjson_mut_strcpy(out, key), vv);
4541+
}
4542+
4543+
char *cbm_cli_build_args_json(const char *tool_name, int argc, char **argv, char **err_out) {
4544+
if (err_out) {
4545+
*err_out = NULL;
4546+
}
4547+
4548+
/* The tool's input_schema (may be NULL for an unknown tool — then every
4549+
* value is treated as a string). Static lifetime; do not free. */
4550+
const char *schema_str = cbm_mcp_tool_input_schema(tool_name);
4551+
yyjson_doc *schema_doc = NULL;
4552+
yyjson_val *props = NULL;
4553+
if (schema_str) {
4554+
schema_doc = yyjson_read(schema_str, strlen(schema_str), 0);
4555+
if (schema_doc) {
4556+
props = yyjson_obj_get(yyjson_doc_get_root(schema_doc), "properties");
4557+
}
4558+
}
4559+
4560+
yyjson_mut_doc *out = yyjson_mut_doc_new(NULL);
4561+
if (!out) {
4562+
if (schema_doc) {
4563+
yyjson_doc_free(schema_doc);
4564+
}
4565+
return NULL;
4566+
}
4567+
yyjson_mut_val *obj = yyjson_mut_obj(out);
4568+
yyjson_mut_doc_set_root(out, obj);
4569+
4570+
bool ok = true;
4571+
for (int i = 0; i < argc && ok; i++) {
4572+
const char *arg = argv[i];
4573+
if (strcmp(arg, "--") == 0) {
4574+
break; /* end of flag parsing */
4575+
}
4576+
if (strncmp(arg, "--", CLI_PAIR_LEN) != 0) {
4577+
if (err_out) {
4578+
*err_out = cli_heap_msgf("unexpected argument '%s' (expected --flag value)", arg);
4579+
}
4580+
ok = false;
4581+
break;
4582+
}
4583+
4584+
const char *body = arg + CLI_PAIR_LEN; /* skip leading "--" */
4585+
const char *eq = strchr(body, '=');
4586+
char key[CLI_BUF_256];
4587+
const char *value = NULL;
4588+
bool have_value = false;
4589+
4590+
if (eq) {
4591+
/* --key=value : split on the FIRST '='; value may contain '='/spaces. */
4592+
size_t klen = (size_t)(eq - body);
4593+
if (klen >= sizeof(key)) {
4594+
klen = sizeof(key) - CLI_SKIP_ONE;
4595+
}
4596+
memcpy(key, body, klen);
4597+
key[klen] = '\0';
4598+
value = eq + CLI_SKIP_ONE;
4599+
have_value = true;
4600+
} else {
4601+
snprintf(key, sizeof(key), "%s", body);
4602+
/* Consume the next token as the value unless it is itself a flag
4603+
* (then this is a bare boolean/string flag). */
4604+
if (i + CLI_SKIP_ONE < argc &&
4605+
strncmp(argv[i + CLI_SKIP_ONE], "--", CLI_PAIR_LEN) != 0) {
4606+
value = argv[i + CLI_SKIP_ONE];
4607+
have_value = true;
4608+
i++;
4609+
}
4610+
}
4611+
4612+
cli_kebab_to_snake(key);
4613+
const char *type = cli_schema_type(props, key);
4614+
4615+
if (type && strcmp(type, "array") == 0 && !have_value) {
4616+
if (err_out) {
4617+
*err_out = cli_heap_msgf("flag --%s requires a value", key);
4618+
}
4619+
ok = false;
4620+
break;
4621+
}
4622+
4623+
cli_add_typed(out, obj, key, type, value, have_value);
4624+
}
4625+
4626+
char *result = NULL;
4627+
if (ok) {
4628+
size_t len = 0;
4629+
result = yyjson_mut_write(out, 0, &len); /* malloc'd; caller frees */
4630+
}
4631+
4632+
yyjson_mut_doc_free(out);
4633+
if (schema_doc) {
4634+
yyjson_doc_free(schema_doc);
4635+
}
4636+
return result;
4637+
}
4638+
4639+
int cbm_cli_print_tool_help(const char *tool_name) {
4640+
const char *schema_str = cbm_mcp_tool_input_schema(tool_name);
4641+
if (!schema_str) {
4642+
return CLI_ERR;
4643+
}
4644+
4645+
yyjson_doc *doc = yyjson_read(schema_str, strlen(schema_str), 0);
4646+
yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
4647+
yyjson_val *props = root ? yyjson_obj_get(root, "properties") : NULL;
4648+
yyjson_val *required = root ? yyjson_obj_get(root, "required") : NULL;
4649+
4650+
printf("Usage:\n");
4651+
printf(" codebase-memory-mcp cli %s --flag value [--flag2 value2 ...]\n", tool_name);
4652+
printf(" codebase-memory-mcp cli %s --args-file <path-to-json>\n", tool_name);
4653+
printf(" echo '<json>' | codebase-memory-mcp cli %s\n", tool_name);
4654+
printf(" codebase-memory-mcp cli %s '<raw-json-args>'\n", tool_name);
4655+
4656+
printf("\nFlags:\n");
4657+
if (props && yyjson_is_obj(props)) {
4658+
yyjson_obj_iter iter;
4659+
yyjson_obj_iter_init(props, &iter);
4660+
yyjson_val *pkey;
4661+
while ((pkey = yyjson_obj_iter_next(&iter)) != NULL) {
4662+
yyjson_val *pval = yyjson_obj_iter_get_val(pkey);
4663+
const char *name = yyjson_get_str(pkey);
4664+
if (!name) {
4665+
continue;
4666+
}
4667+
const char *type = "string";
4668+
const char *desc = "";
4669+
if (yyjson_is_obj(pval)) {
4670+
yyjson_val *t = yyjson_obj_get(pval, "type");
4671+
if (t && yyjson_is_str(t)) {
4672+
type = yyjson_get_str(t);
4673+
}
4674+
yyjson_val *d = yyjson_obj_get(pval, "description");
4675+
if (d && yyjson_is_str(d)) {
4676+
desc = yyjson_get_str(d);
4677+
}
4678+
}
4679+
char flag[CLI_BUF_256];
4680+
snprintf(flag, sizeof(flag), "%s", name);
4681+
cli_snake_to_kebab(flag);
4682+
bool req = cli_schema_required_has(required, name);
4683+
printf(" --%s <%s>%s", flag, type, req ? " [required]" : "");
4684+
if (desc[0]) {
4685+
printf(" %s", desc);
4686+
}
4687+
printf("\n");
4688+
}
4689+
}
4690+
4691+
if (doc) {
4692+
yyjson_doc_free(doc);
4693+
}
4694+
return CLI_OK;
4695+
}

‎src/cli/cli.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ void cbm_cli_set_version(const char *ver);
1919
/* Get the version string. */
2020
const char *cbm_cli_get_version(void);
2121

22+
/* ── CLI tool arguments (flags / --args-file / --help) ────────── */
23+
24+
/* Convert `--flag value` / `--flag=value` / bare-boolean `--flag` arguments for
25+
* a tool into a JSON arguments object string, using the tool's input_schema to
26+
* type values (string/integer/boolean) and to collect repeated flags into
27+
* array-typed properties. kebab-case flags map to snake_case keys
28+
* (--repo-path -> repo_path). A bare `--` ends flag parsing. On error returns
29+
* NULL and, if err_out is non-NULL, sets *err_out to a heap message the caller
30+
* must free. Caller frees the returned JSON string. */
31+
char *cbm_cli_build_args_json(const char *tool_name, int argc, char **argv, char **err_out);
32+
33+
/* Print per-tool help (usage + the tool's flags with type/description/required)
34+
* derived from its input_schema, to stdout. Returns 0 if the tool is known,
35+
* non-zero (and prints nothing) if it is not. */
36+
int cbm_cli_print_tool_help(const char *tool_name);
37+
2238
/* ── Self-update: version comparison ──────────────────────────── */
2339

2440
/* Compare two semver strings (e.g. "0.2.1" vs "0.2.0").

0 commit comments

Comments
 (0)