|
| 1 | +//! Renders `docs/dev/operations/cli-commands.md` from the command line's clap |
| 2 | +//! model, so it says what `--help` says, and checks the committed page is |
| 3 | +//! current. `just docs reference` rewrites it. |
| 4 | +
|
| 5 | +use super::Cli; |
| 6 | +use clap::{Arg, ArgAction, Command, CommandFactory}; |
| 7 | +use piquel_common::config::GeneratedFile; |
| 8 | +use std::fmt::Write as _; |
| 9 | + |
| 10 | +/// The page's title and introduction, after the generated-file comment. |
| 11 | +const INTRO: &str = "\ |
| 12 | +# auth-service commands |
| 13 | +
|
| 14 | +This page lists every `auth-service` command with its arguments and options. It |
| 15 | +is generated from the same definitions as `auth-service --help`, so `--help` on |
| 16 | +any command prints the same text. [Administration](administration.md) shows the |
| 17 | +commands at work, and [deploying](deploying.md#running-auth-service-commands) |
| 18 | +how to run them in an environment. |
| 19 | +
|
| 20 | +Every command also accepts `-h` and `--help`. |
| 21 | +"; |
| 22 | + |
| 23 | +/// The page, built one command at a time, depth first. |
| 24 | +struct Reference { |
| 25 | + /// The Markdown written so far. |
| 26 | + page: String, |
| 27 | +} |
| 28 | + |
| 29 | +impl Reference { |
| 30 | + /// Renders the page from the whole command tree. |
| 31 | + fn render() -> String { |
| 32 | + let mut command = Cli::command().bin_name("auth-service"); |
| 33 | + // Building fills in each subcommand's full name and its usage. |
| 34 | + command.build(); |
| 35 | + let mut reference = Self { |
| 36 | + page: format!( |
| 37 | + "<!-- Generated by `just docs reference` from the clap definitions in \ |
| 38 | + `apps/auth-service/src/cli`. Do not edit it by hand. -->\n\n{INTRO}" |
| 39 | + ), |
| 40 | + }; |
| 41 | + reference.command(&command); |
| 42 | + reference.page |
| 43 | + } |
| 44 | + |
| 45 | + /// Adds `command`'s section: what it does, its usage, its arguments and |
| 46 | + /// options, and links to its subcommands, whose sections follow. |
| 47 | + fn command(&mut self, command: &Command) { |
| 48 | + let path = command.get_bin_name().unwrap_or(command.get_name()); |
| 49 | + let _ = writeln!(self.page, "\n## `{path}`\n"); |
| 50 | + if let Some(about) = command.get_long_about().or(command.get_about()) { |
| 51 | + let _ = writeln!(self.page, "{}\n", Self::sentence(&about.to_string())); |
| 52 | + } |
| 53 | + let usage = command.clone().render_usage().to_string(); |
| 54 | + let _ = writeln!( |
| 55 | + self.page, |
| 56 | + "```text\n{}\n```", |
| 57 | + usage.trim_start_matches("Usage: ") |
| 58 | + ); |
| 59 | + |
| 60 | + let (positionals, options): (Vec<&Arg>, Vec<&Arg>) = command |
| 61 | + .get_arguments() |
| 62 | + .filter(|argument| Self::documented(argument)) |
| 63 | + .partition(|argument| argument.is_positional()); |
| 64 | + for (title, arguments) in [("Arguments", positionals), ("Options", options)] { |
| 65 | + if !arguments.is_empty() { |
| 66 | + let _ = writeln!(self.page, "\n{title}:\n"); |
| 67 | + for argument in arguments { |
| 68 | + self.argument(argument); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + let subcommands: Vec<&Command> = command |
| 74 | + .get_subcommands() |
| 75 | + // clap's own `help` command is the `--help` the introduction covers. |
| 76 | + .filter(|subcommand| !subcommand.is_hide_set() && subcommand.get_name() != "help") |
| 77 | + .collect(); |
| 78 | + if !subcommands.is_empty() { |
| 79 | + let _ = writeln!(self.page, "\nCommands:\n"); |
| 80 | + for subcommand in &subcommands { |
| 81 | + let name = format!("{path} {}", subcommand.get_name()); |
| 82 | + let about = subcommand |
| 83 | + .get_about() |
| 84 | + .map(|about| Self::sentence(&about.to_string())) |
| 85 | + .unwrap_or_default(); |
| 86 | + let _ = writeln!( |
| 87 | + self.page, |
| 88 | + "- [`{name}`](#{}): {about}", |
| 89 | + name.replace(' ', "-") |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + for subcommand in subcommands { |
| 94 | + self.command(subcommand); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// Adds one argument or option: how to write it, what it does, and its |
| 99 | + /// default and accepted values. |
| 100 | + fn argument(&mut self, argument: &Arg) { |
| 101 | + let syntax = match (argument.get_short(), argument.get_long()) { |
| 102 | + (Some(short), Some(_)) => format!("-{short}, {argument}"), |
| 103 | + _ => argument.to_string(), |
| 104 | + }; |
| 105 | + let help = argument |
| 106 | + .get_long_help() |
| 107 | + .or(argument.get_help()) |
| 108 | + .map(|help| Self::sentence(&help.to_string())) |
| 109 | + .unwrap_or_default(); |
| 110 | + let mut text = format!("`{syntax}`: {help}"); |
| 111 | + if argument.is_required_set() && !argument.is_positional() { |
| 112 | + text.push_str(" Required."); |
| 113 | + } |
| 114 | + let takes_values = argument.get_action().takes_values(); |
| 115 | + let defaults: Vec<String> = argument |
| 116 | + .get_default_values() |
| 117 | + .iter() |
| 118 | + .map(|value| format!("`{}`", value.to_string_lossy())) |
| 119 | + .filter(|value| value != "``") |
| 120 | + .collect(); |
| 121 | + if takes_values && !defaults.is_empty() { |
| 122 | + let _ = write!(text, " Default: {}.", defaults.join(", ")); |
| 123 | + } |
| 124 | + let values: Vec<String> = argument |
| 125 | + .get_possible_values() |
| 126 | + .iter() |
| 127 | + .filter(|value| !value.is_hide_set()) |
| 128 | + .map(|value| match value.get_help() { |
| 129 | + Some(help) => format!("`{}` ({})", value.get_name(), help), |
| 130 | + None => format!("`{}`", value.get_name()), |
| 131 | + }) |
| 132 | + .collect(); |
| 133 | + if takes_values && !values.is_empty() { |
| 134 | + let _ = write!(text, " Values: {}.", values.join(", ")); |
| 135 | + } |
| 136 | + let _ = writeln!(self.page, "- {}", text.trim_end()); |
| 137 | + } |
| 138 | + |
| 139 | + /// Whether `argument` belongs on the page: not hidden, and not clap's own |
| 140 | + /// `--help` and `--version`, which the introduction covers. |
| 141 | + fn documented(argument: &Arg) -> bool { |
| 142 | + !argument.is_hide_set() |
| 143 | + && !matches!( |
| 144 | + argument.get_action(), |
| 145 | + ArgAction::Help | ArgAction::HelpShort | ArgAction::HelpLong | ArgAction::Version |
| 146 | + ) |
| 147 | + } |
| 148 | + |
| 149 | + /// `text` as prose: clap's help drops a summary's final full stop, so it |
| 150 | + /// gets one back, and its paragraphs keep their blank lines. |
| 151 | + fn sentence(text: &str) -> String { |
| 152 | + let text = text.trim(); |
| 153 | + if text.is_empty() || text.ends_with(['.', ':', '!', '?']) { |
| 154 | + text.to_owned() |
| 155 | + } else { |
| 156 | + format!("{text}.") |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +/// The committed page says what the command line's definitions say. |
| 162 | +#[test] |
| 163 | +fn cli_reference_is_current() { |
| 164 | + GeneratedFile { |
| 165 | + path: concat!( |
| 166 | + env!("CARGO_MANIFEST_DIR"), |
| 167 | + "/../../docs/dev/operations/cli-commands.md" |
| 168 | + ) |
| 169 | + .into(), |
| 170 | + text: Reference::render(), |
| 171 | + } |
| 172 | + .write_or_compare() |
| 173 | + .expect("current reference"); |
| 174 | +} |
0 commit comments