|
| 1 | +//! Export CLI structure as JSON for documentation generation |
| 2 | +
|
| 3 | +use clap::Command; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +/// Representation of a CLI option for JSON export |
| 7 | +#[derive(Debug, Serialize, Deserialize)] |
| 8 | +pub struct CliOption { |
| 9 | + pub long: String, |
| 10 | + pub short: Option<String>, |
| 11 | + pub value_name: Option<String>, |
| 12 | + pub default: Option<String>, |
| 13 | + pub help: String, |
| 14 | + pub possible_values: Vec<String>, |
| 15 | + pub required: bool, |
| 16 | +} |
| 17 | + |
| 18 | +/// Representation of a CLI command for JSON export |
| 19 | +#[derive(Debug, Serialize, Deserialize)] |
| 20 | +pub struct CliCommand { |
| 21 | + pub name: String, |
| 22 | + pub about: Option<String>, |
| 23 | + pub options: Vec<CliOption>, |
| 24 | + pub positionals: Vec<CliPositional>, |
| 25 | + pub subcommands: Vec<CliCommand>, |
| 26 | +} |
| 27 | + |
| 28 | +/// Representation of a positional argument |
| 29 | +#[derive(Debug, Serialize, Deserialize)] |
| 30 | +pub struct CliPositional { |
| 31 | + pub name: String, |
| 32 | + pub help: Option<String>, |
| 33 | + pub required: bool, |
| 34 | + pub multiple: bool, |
| 35 | +} |
| 36 | + |
| 37 | +/// Convert a clap Command to our JSON representation |
| 38 | +pub fn command_to_json(cmd: &Command) -> CliCommand { |
| 39 | + let mut options = Vec::new(); |
| 40 | + let mut positionals = Vec::new(); |
| 41 | + |
| 42 | + // Extract arguments |
| 43 | + for arg in cmd.get_arguments() { |
| 44 | + let id = arg.get_id().as_str(); |
| 45 | + |
| 46 | + // Skip built-in help and version |
| 47 | + if id == "help" || id == "version" { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + // Skip hidden arguments |
| 52 | + if arg.is_hide_set() { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + if arg.is_positional() { |
| 57 | + // Handle positional arguments |
| 58 | + positionals.push(CliPositional { |
| 59 | + name: id.to_string(), |
| 60 | + help: arg.get_help().map(|h| h.to_string()), |
| 61 | + required: arg.is_required_set(), |
| 62 | + multiple: false, // For now, simplify this |
| 63 | + }); |
| 64 | + } else { |
| 65 | + // Handle options/flags |
| 66 | + let mut possible_values = Vec::new(); |
| 67 | + let pvs = arg.get_possible_values(); |
| 68 | + if !pvs.is_empty() { |
| 69 | + for pv in pvs { |
| 70 | + possible_values.push(pv.get_name().to_string()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + let help = arg.get_help().map(|h| h.to_string()).unwrap_or_default(); |
| 75 | + |
| 76 | + options.push(CliOption { |
| 77 | + long: arg |
| 78 | + .get_long() |
| 79 | + .map(String::from) |
| 80 | + .unwrap_or_else(|| id.to_string()), |
| 81 | + short: arg.get_short().map(|c| c.to_string()), |
| 82 | + value_name: arg |
| 83 | + .get_value_names() |
| 84 | + .and_then(|names| names.first()) |
| 85 | + .map(|s| s.to_string()), |
| 86 | + default: arg |
| 87 | + .get_default_values() |
| 88 | + .first() |
| 89 | + .and_then(|v| v.to_str()) |
| 90 | + .map(String::from), |
| 91 | + help, |
| 92 | + possible_values, |
| 93 | + required: arg.is_required_set(), |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Extract subcommands |
| 99 | + let mut subcommands = Vec::new(); |
| 100 | + for subcmd in cmd.get_subcommands() { |
| 101 | + // Skip help subcommand |
| 102 | + if subcmd.get_name() == "help" { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + // Skip hidden subcommands |
| 107 | + if subcmd.is_hide_set() { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + subcommands.push(command_to_json(subcmd)); |
| 112 | + } |
| 113 | + |
| 114 | + CliCommand { |
| 115 | + name: cmd.get_name().to_string(), |
| 116 | + about: cmd.get_about().map(|s| s.to_string()), |
| 117 | + options, |
| 118 | + positionals, |
| 119 | + subcommands, |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// Dump the entire CLI structure as JSON |
| 124 | +pub fn dump_cli_json(cmd: &Command) -> Result<String, serde_json::Error> { |
| 125 | + let cli_structure = command_to_json(cmd); |
| 126 | + serde_json::to_string_pretty(&cli_structure) |
| 127 | +} |
0 commit comments