Skip to content

Commit 4dfec70

Browse files
committed
Generate CLI reference
Signed-off-by: itowlson <[email protected]>
1 parent 66fee27 commit 4dfec70

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

Cargo.lock

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ anyhow = { workspace = true }
1919
async-trait = { workspace = true }
2020
bytes = { workspace = true }
2121
clap = { version = "3.2.24", features = ["derive", "env"] }
22+
clap-markdown = { git = "https://github.com/itowlson/clap-markdown", rev = "9e484a05ac70bfb928453559fa45a49534dac121" }
2223
clearscreen = "3"
2324
comfy-table = "7"
2425
command-group = "2"

src/bin/spin.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::{Context, Error};
22
use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
33
use lazy_static::lazy_static;
44
use spin_cli::commands::external::predefined_externals;
5+
use spin_cli::commands::maintenance::MaintenanceCommands;
56
use spin_cli::commands::{
67
build::BuildCommand,
78
cloud::{DeployCommand, LoginCommand},
@@ -135,6 +136,8 @@ enum SpinApp {
135136
#[clap(alias = "w")]
136137
Watch(WatchCommand),
137138
Doctor(DoctorCommand),
139+
#[clap(subcommand, hide = true)]
140+
Maintenance(MaintenanceCommands),
138141
}
139142

140143
#[derive(Subcommand)]
@@ -164,6 +167,7 @@ impl SpinApp {
164167
Self::External(cmd) => execute_external_subcommand(cmd, app).await,
165168
Self::Watch(cmd) => cmd.run().await,
166169
Self::Doctor(cmd) => cmd.run().await,
170+
Self::Maintenance(cmd) => cmd.run(SpinApp::command()).await,
167171
}
168172
}
169173
}

src/commands.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub mod cloud;
88
pub mod doctor;
99
/// Commands for external subcommands (i.e. plugins)
1010
pub mod external;
11+
/// Commands for Spin maintenance tasks.
12+
pub mod maintenance;
1113
/// Command for creating a new application.
1214
pub mod new;
1315
/// Command for adding a plugin to Spin

src/commands/maintenance.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::path::PathBuf;
2+
3+
use clap::{Parser, Subcommand};
4+
5+
/// Commands for Spin maintenance tasks.
6+
#[derive(Subcommand, Debug)]
7+
pub enum MaintenanceCommands {
8+
/// Generate CLI reference docs in Markdown format.
9+
GenerateReference(GenerateReference),
10+
}
11+
12+
impl MaintenanceCommands {
13+
pub async fn run(&self, app: clap::Command<'_>) -> anyhow::Result<()> {
14+
match self {
15+
MaintenanceCommands::GenerateReference(g) => g.run(app).await,
16+
}
17+
}
18+
}
19+
20+
#[derive(Parser, Debug)]
21+
pub struct GenerateReference {
22+
/// The file to which to generate the reference Markdown. If omitted, it is generated to stdout.
23+
#[clap(short = 'o')]
24+
pub output: Option<PathBuf>,
25+
}
26+
27+
impl GenerateReference {
28+
pub async fn run(&self, app: clap::Command<'_>) -> anyhow::Result<()> {
29+
let markdown = clap_markdown::help_markdown_command(&app);
30+
match &self.output {
31+
Some(path) => std::fs::write(path, markdown)?,
32+
None => println!("{markdown}"),
33+
}
34+
Ok(())
35+
}
36+
}

0 commit comments

Comments
 (0)