Skip to content

Commit

Permalink
feat: Make JSON output subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Aug 9, 2023
1 parent 33c8ed5 commit 85887da
Show file tree
Hide file tree
Showing 3 changed files with 525 additions and 140 deletions.
39 changes: 30 additions & 9 deletions src/gen/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,64 @@ fn generate_cmd(
) {
let cmd = quote(cmd);
// Avoid trailing commas
let end = if last { "]" } else { "]," };
let end = if last { "}" } else { "}," };
let mut args = cmd_info.args.into_iter();
if let Some(mut arg) = args.next() {
println_indent(indent, out, format!("{cmd}: ["));
println_indent(indent, out, format!("{cmd}: {{"));
println_indent(indent + 1, out, "\"args\": [");
while {
println_indent(indent + 1, out, "{");
println_indent(indent + 2, out, "{");
let forms = arg
.forms
.iter()
.map(|a| quote(&a))
.collect::<Vec<_>>()
.join(", ");
print_indent(indent + 2, out, format!(r#""forms": [{forms}]"#));
print_indent(indent + 3, out, format!(r#""forms": [{forms}]"#));
if let Some(desc) = &arg.desc {
out.push_str(",\n");
println_indent(
indent + 2,
indent + 3,
out,
format!(r#""description": {}"#, quote(desc)),
);
} else {
out.push_str("\n");
}
if let Some(next) = args.next() {
println_indent(indent + 1, out, "},");
println_indent(indent + 2, out, "},");
arg = next;
true
} else {
// Avoid trailing comma
println_indent(indent + 1, out, "}");
println_indent(indent + 2, out, "}");
false
}
} {}
println_indent(indent + 1, out, "],");

let mut subcmds = cmd_info.subcommands.into_iter();
if let Some((mut name, mut info)) = subcmds.next() {
println_indent(indent + 1, out, "\"subcommands\": {");
loop {
if let Some(next) = subcmds.next() {
generate_cmd(&name, info, indent + 2, false, out);
name = next.0;
info = next.1;
} else {
generate_cmd(&name, info, indent + 2, true, out);
break;
}
}
println_indent(indent + 1, out, "}");
} else {
println_indent(indent + 1, out, "\"subcommands\": {}");
}

println_indent(indent, out, end);
} else {
// If no arguments, print `"cmd": []` on a single line
println_indent(indent, out, format!("{cmd}: [{end}"))
// If no arguments, print `"cmd": {}` on a single line
println_indent(indent, out, format!("{cmd}: {{{end}"))
}
}

Expand Down
Loading

0 comments on commit 85887da

Please sign in to comment.