|
1 | 1 | use anyhow::{Result, bail}; |
2 | | -use clap::Parser; |
| 2 | +use clap::{Parser, Subcommand}; |
3 | 3 | use env_logger::{Builder, Env}; |
4 | | -use esp_generate::template::{GeneratorOptionItem, Template}; |
| 4 | +use esp_generate::template::{GeneratorOption, GeneratorOptionItem, Template}; |
5 | 5 | use esp_generate::{ |
6 | 6 | append_list_as_sentence, |
7 | 7 | config::{ActiveConfiguration, Relationships}, |
8 | 8 | }; |
9 | 9 | use esp_generate::{cargo, config::find_option}; |
10 | 10 | use esp_metadata::Chip; |
| 11 | +use indexmap::IndexMap; |
11 | 12 | use inquire::{Select, Text}; |
12 | 13 | use ratatui::crossterm::event; |
| 14 | +use std::collections::HashSet; |
13 | 15 | use std::{ |
14 | 16 | collections::HashMap, |
15 | 17 | env, fs, |
@@ -40,7 +42,7 @@ static TEMPLATE: LazyLock<Template> = LazyLock::new(|| { |
40 | 42 | }); |
41 | 43 |
|
42 | 44 | #[derive(Parser, Debug)] |
43 | | -#[command(author, version, about = about(), long_about = None)] |
| 45 | +#[command(author, version, about = about(), long_about = None, subcommand_negates_reqs = true)] |
44 | 46 | struct Args { |
45 | 47 | /// Name of the project to generate |
46 | 48 | name: Option<String>, |
@@ -83,6 +85,136 @@ struct Args { |
83 | 85 | /// Note that in headless mode this is not checked. |
84 | 86 | #[arg(long)] |
85 | 87 | toolchain: Option<String>, |
| 88 | + |
| 89 | + #[clap(subcommand)] |
| 90 | + subcommands: Option<SubCommands>, |
| 91 | +} |
| 92 | + |
| 93 | +#[derive(Subcommand, Debug)] |
| 94 | +enum SubCommands { |
| 95 | + /// List available template options |
| 96 | + ListOptions, |
| 97 | + |
| 98 | + /// Print information about a template option |
| 99 | + Explain { option: String }, |
| 100 | +} |
| 101 | + |
| 102 | +impl SubCommands { |
| 103 | + fn handle(&self) -> Result<()> { |
| 104 | + fn chip_info_text(options: &[&GeneratorOption], opt: &GeneratorOption) -> String { |
| 105 | + let mut chips = Vec::new(); |
| 106 | + for option in options.iter().filter(|o| o.name == opt.name) { |
| 107 | + chips.extend_from_slice(&option.chips); |
| 108 | + } |
| 109 | + |
| 110 | + let chip_count = Chip::iter().count(); |
| 111 | + |
| 112 | + if chips.is_empty() || chips.len() == chip_count { |
| 113 | + String::new() |
| 114 | + } else if chips.len() < chip_count / 2 { |
| 115 | + format!( |
| 116 | + "Only available on {}.", |
| 117 | + chips |
| 118 | + .iter() |
| 119 | + .map(ToString::to_string) |
| 120 | + .collect::<Vec<_>>() |
| 121 | + .join(", ") |
| 122 | + ) |
| 123 | + } else { |
| 124 | + format!( |
| 125 | + "Not available on {}.", |
| 126 | + Chip::iter() |
| 127 | + .filter(|c| !chips.contains(c)) |
| 128 | + .map(|c| c.to_string()) |
| 129 | + .collect::<Vec<_>>() |
| 130 | + .join(", ") |
| 131 | + ) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + match self { |
| 136 | + SubCommands::ListOptions => { |
| 137 | + println!( |
| 138 | + "The following template options are available. The group names are not part of the option name. Only one option in a group can be selected." |
| 139 | + ); |
| 140 | + let mut groups = IndexMap::new(); |
| 141 | + let mut seen = HashSet::new(); |
| 142 | + let all_options = TEMPLATE.all_options(); |
| 143 | + for (index, option) in all_options |
| 144 | + .iter() |
| 145 | + .enumerate() |
| 146 | + .filter(|(_, o)| !["toolchain", "module"].contains(&o.selection_group.as_str())) |
| 147 | + { |
| 148 | + let group = groups.entry(&option.selection_group).or_insert(Vec::new()); |
| 149 | + |
| 150 | + if seen.insert(&option.name) { |
| 151 | + group.push(index); |
| 152 | + } |
| 153 | + } |
| 154 | + for (group, options) in groups { |
| 155 | + println!("Group: {}", group); |
| 156 | + for option in options { |
| 157 | + let option = &all_options[option]; |
| 158 | + let mut help_text = option.display_name.clone(); |
| 159 | + |
| 160 | + if !option.requires.is_empty() { |
| 161 | + help_text.push_str(" Requires: "); |
| 162 | + help_text.push_str(&option.requires.join(", ")); |
| 163 | + help_text.push('.'); |
| 164 | + } |
| 165 | + let chip_info = chip_info_text(&all_options, option); |
| 166 | + if !chip_info.is_empty() { |
| 167 | + help_text.push(' '); |
| 168 | + help_text.push_str(&chip_info); |
| 169 | + } |
| 170 | + println!(" {}: {help_text}", option.name); |
| 171 | + } |
| 172 | + } |
| 173 | + Ok(()) |
| 174 | + } |
| 175 | + SubCommands::Explain { option } => { |
| 176 | + let all_options = TEMPLATE.all_options(); |
| 177 | + if let Some(option) = all_options.iter().find(|o| &o.name == option) { |
| 178 | + println!( |
| 179 | + "Option: {}\n\n{}{}", |
| 180 | + option.name, |
| 181 | + option.display_name, |
| 182 | + if option.help.is_empty() { |
| 183 | + String::new() |
| 184 | + } else { |
| 185 | + format!("\n{}\n", option.help) |
| 186 | + } |
| 187 | + ); |
| 188 | + if !option.requires.is_empty() { |
| 189 | + println!(); |
| 190 | + let positive_req = option.requires.iter().filter(|r| !r.starts_with("!")); |
| 191 | + let negative_req = option.requires.iter().filter(|r| r.starts_with("!")); |
| 192 | + if positive_req.clone().next().is_some() { |
| 193 | + println!("Requires the following options to be set:"); |
| 194 | + for require in positive_req { |
| 195 | + println!(" {}", require); |
| 196 | + } |
| 197 | + } |
| 198 | + if negative_req.clone().next().is_some() { |
| 199 | + println!("Requires the following options to NOT be set:"); |
| 200 | + for require in negative_req { |
| 201 | + if let Some(stripped) = require.strip_prefix('!') { |
| 202 | + println!(" {}", stripped); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + let chip_info = chip_info_text(&all_options, option); |
| 208 | + if !chip_info.is_empty() { |
| 209 | + println!("{}", chip_info); |
| 210 | + } |
| 211 | + } else { |
| 212 | + println!("Unknown option: {}", option); |
| 213 | + } |
| 214 | + Ok(()) |
| 215 | + } |
| 216 | + } |
| 217 | + } |
86 | 218 | } |
87 | 219 |
|
88 | 220 | /// Check crates.io for a new version of the application |
@@ -164,12 +296,17 @@ fn setup_args_interactive(args: &mut Args) -> Result<()> { |
164 | 296 | } |
165 | 297 |
|
166 | 298 | fn main() -> Result<()> { |
| 299 | + // Set up logging. |
167 | 300 | Builder::from_env(Env::default().default_filter_or(log::LevelFilter::Info.as_str())) |
168 | 301 | .format_target(false) |
169 | 302 | .init(); |
170 | 303 |
|
171 | 304 | let mut args = Args::parse(); |
172 | 305 |
|
| 306 | + if let Some(subcommand) = args.subcommands { |
| 307 | + return subcommand.handle(); |
| 308 | + } |
| 309 | + |
173 | 310 | // Only check for updates once the command-line arguments have been processed, |
174 | 311 | // to avoid printing any update notifications when the help message is |
175 | 312 | // displayed. |
|
0 commit comments