From a3d8ca2f90ab1e7be708d21f70ff8370aa2380b0 Mon Sep 17 00:00:00 2001 From: jdx <216188+jdx@users.noreply.github.com> Date: Mon, 9 Dec 2024 10:16:13 -0600 Subject: [PATCH] feat: `usage g json` (#184) --- cli/src/cli/generate/json.rs | 26 + cli/src/cli/generate/mod.rs | 5 +- cli/usage.usage.kdl | 8 + docs/.vitepress/config.mts | 21 +- docs/cli/bash.md | 26 + docs/cli/complete-word.md | 33 ++ docs/cli/generate.md | 12 + docs/cli/generate/completion.md | 33 ++ docs/cli/generate/fig.md | 18 + docs/cli/generate/json.md | 16 + docs/cli/generate/markdown.md | 29 + docs/cli/reference.md | 189 ------- docs/cli/reference/bash.md | 30 + docs/cli/reference/commands.json | 527 ++++++++++++++++++ docs/cli/reference/complete-word.md | 33 ++ docs/cli/reference/generate.md | 12 + docs/cli/reference/generate/completion.md | 33 ++ docs/cli/reference/generate/fig.md | 18 + docs/cli/reference/generate/json.md | 16 + docs/cli/reference/generate/markdown.md | 29 + docs/cli/reference/index.md | 29 + examples/docs/MISE_INLINE.md | 4 + examples/docs/MISE_MULTI.md | 4 + .../markdown/templates/arg_template.md.tera | 2 +- .../markdown/templates/cmd_template.md.tera | 8 +- .../markdown/templates/flag_template.md.tera | 2 +- .../markdown/templates/spec_template.md.tera | 2 +- lib/src/spec/arg.rs | 10 + lib/src/spec/cmd.rs | 12 + lib/src/spec/complete.rs | 2 + lib/src/spec/flag.rs | 12 + lib/src/spec/mod.rs | 18 + mise.toml | 5 +- 33 files changed, 1025 insertions(+), 199 deletions(-) create mode 100644 cli/src/cli/generate/json.rs create mode 100644 docs/cli/bash.md create mode 100644 docs/cli/complete-word.md create mode 100644 docs/cli/generate.md create mode 100644 docs/cli/generate/completion.md create mode 100644 docs/cli/generate/fig.md create mode 100644 docs/cli/generate/json.md create mode 100644 docs/cli/generate/markdown.md delete mode 100644 docs/cli/reference.md create mode 100644 docs/cli/reference/bash.md create mode 100644 docs/cli/reference/commands.json create mode 100644 docs/cli/reference/complete-word.md create mode 100644 docs/cli/reference/generate.md create mode 100644 docs/cli/reference/generate/completion.md create mode 100644 docs/cli/reference/generate/fig.md create mode 100644 docs/cli/reference/generate/json.md create mode 100644 docs/cli/reference/generate/markdown.md create mode 100644 docs/cli/reference/index.md diff --git a/cli/src/cli/generate/json.rs b/cli/src/cli/generate/json.rs new file mode 100644 index 0000000..5e383c6 --- /dev/null +++ b/cli/src/cli/generate/json.rs @@ -0,0 +1,26 @@ +use crate::cli::generate; +use crate::Result; +use miette::IntoDiagnostic; +use std::path::PathBuf; + +/// Outputs a usage spec in json format +#[derive(clap::Args)] +#[clap()] +pub struct Json { + /// A usage spec taken in as a file + #[clap(short, long)] + file: Option, + + /// raw string spec input + #[clap(long, required_unless_present = "file", overrides_with = "file")] + spec: Option, +} + +impl Json { + pub fn run(&self) -> Result<()> { + let spec = generate::file_or_spec(&self.file, &self.spec)?; + let json = serde_json::to_string_pretty(&spec).into_diagnostic()?; + println!("{json}"); + Ok(()) + } +} diff --git a/cli/src/cli/generate/mod.rs b/cli/src/cli/generate/mod.rs index 54673f9..5ff6473 100644 --- a/cli/src/cli/generate/mod.rs +++ b/cli/src/cli/generate/mod.rs @@ -5,6 +5,7 @@ use usage::Spec; mod completion; mod fig; +mod json; mod markdown; #[derive(clap::Args)] @@ -18,6 +19,7 @@ pub struct Generate { pub enum Command { Completion(completion::Completion), Fig(fig::Fig), + Json(json::Json), Markdown(markdown::Markdown), } @@ -25,8 +27,9 @@ impl Generate { pub fn run(&self) -> miette::Result<()> { match &self.command { Command::Completion(cmd) => cmd.run(), - Command::Markdown(cmd) => cmd.run(), Command::Fig(cmd) => cmd.run(), + Command::Json(cmd) => cmd.run(), + Command::Markdown(cmd) => cmd.run(), } } } diff --git a/cli/usage.usage.kdl b/cli/usage.usage.kdl index c20aee6..e0f1f60 100644 --- a/cli/usage.usage.kdl +++ b/cli/usage.usage.kdl @@ -68,6 +68,14 @@ cmd "generate" subcommand_required=true { arg "" } } + cmd "json" help="Outputs a usage spec in json format" { + flag "-f --file" help="A usage spec taken in as a file" { + arg "" + } + flag "--spec" help="raw string spec input" { + arg "" + } + } cmd "markdown" { alias "md" flag "-f --file" help="A usage spec taken in as a file" required=true { diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index bb0fe0e..cff0de1 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -1,4 +1,17 @@ import { defineConfig } from "vitepress"; +import spec from "../cli/reference/commands.json"; + +function getCommands(cmd): string[][] { + const commands = []; + for (const [name, sub] of Object.entries(cmd.subcommands)) { + if (sub.hide) continue; + commands.push(sub.full_cmd); + commands.push(...getCommands(sub)); + } + return commands; +} + +const commands = getCommands(spec.cmd); // https://vitepress.dev/reference/site-config export default defineConfig({ @@ -31,7 +44,13 @@ export default defineConfig({ { text: "Manpages", link: "/cli/manpages" }, { text: "Markdown", link: "/cli/markdown" }, { text: "Scripts", link: "/cli/scripts" }, - { text: "CLI Reference", link: "/cli/reference" } + { + text: "CLI Reference", link: "/cli/reference/", items: + commands.map((command) => ({ + text: command.join(" "), + link: `/cli/reference/${command.join("/")}` + })) + } ] }, { diff --git a/docs/cli/bash.md b/docs/cli/bash.md new file mode 100644 index 0000000..4c99418 --- /dev/null +++ b/docs/cli/bash.md @@ -0,0 +1,26 @@ +# `usage bash` + +- **Usage**: `usage bash [-h] [--help]