| 1 |
use clap::{Arg, ArgAction}; |
| 2 |
use roff::{Inline, Roff, bold, italic, roman}; |
| 3 |
|
| 4 |
pub(crate) fn subcommand_heading(cmd: &clap::Command) -> &str { |
| 5 |
match cmd.get_subcommand_help_heading() { |
| 6 |
Some(title) => title, |
| 7 |
None => "SUBCOMMANDS", |
| 8 |
} |
| 9 |
} |
| 10 |
|
| 11 |
pub(crate) fn about(roff: &mut Roff, cmd: &clap::Command) { |
| 12 |
let name = cmd.get_display_name().unwrap_or_else(|| cmd.get_name()); |
| 13 |
let s = match cmd.get_about().or_else(|| cmd.get_long_about()) { |
| 14 |
Some(about) => format!("{name} - {about}"), |
| 15 |
None => name.to_owned(), |
| 16 |
}; |
| 17 |
roff.text([roman(s)]); |
| 18 |
} |
| 19 |
|
| 20 |
pub(crate) fn description(roff: &mut Roff, cmd: &clap::Command) { |
| 21 |
if let Some(about) = cmd.get_long_about().or_else(|| cmd.get_about()) { |
| 22 |
for line in about.to_string().lines() { |
| 23 |
if line.trim().is_empty() { |
| 24 |
roff.control("PP", []); |
| 25 |
} else { |
| 26 |
roff.text([roman(line)]); |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) { |
| 33 |
let name = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name()); |
| 34 |
|
| 35 |
if let Some(usage) = cmd.get_overridden_usage() { |
| 36 |
override_synopsis(roff, name, &usage.to_string()); |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
let mut line = vec![bold(name), roman(" ")]; |
| 41 |
|
| 42 |
let required_groups: Vec<Vec<&Arg>> = cmd |
| 43 |
.get_groups() |
| 44 |
.filter(|g| g.is_required_set()) |
| 45 |
.map(|g| unroll_group_args(cmd, g.get_id())) |
| 46 |
.filter(|members| !members.is_empty()) |
| 47 |
.collect(); |
| 48 |
let group_members: Vec<&Arg> = required_groups.iter().flatten().copied().collect(); |
| 49 |
|
| 50 |
let mut opts: Vec<_> = cmd |
| 51 |
.get_arguments() |
| 52 |
.filter(|i| { |
| 53 |
!i.is_hide_set() |
| 54 |
&& !i.is_positional() |
| 55 |
&& !group_members.iter().any(|gm| gm.get_id() == i.get_id()) |
| 56 |
}) |
| 57 |
.collect(); |
| 58 |
|
| 59 |
opts.sort_by_key(|opt| option_sort_key(opt)); |
| 60 |
|
| 61 |
for opt in opts.iter().filter(|opt| !opt.is_required_set()) { |
| 62 |
render_synopsis_option(&mut line, opt); |
| 63 |
} |
| 64 |
|
| 65 |
for opt in opts.iter().filter(|opt| opt.is_required_set()) { |
| 66 |
render_synopsis_option(&mut line, opt); |
| 67 |
} |
| 68 |
|
| 69 |
for members in &required_groups { |
| 70 |
line.push(roman("<")); |
| 71 |
for (i, arg) in members.iter().enumerate() { |
| 72 |
if i > 0 { |
| 73 |
line.push(roman("|")); |
| 74 |
} |
| 75 |
line.extend(render_synopsis_arg(arg)); |
| 76 |
} |
| 77 |
line.push(roman(">")); |
| 78 |
line.push(roman(" ")); |
| 79 |
} |
| 80 |
|
| 81 |
for arg in cmd.get_positionals().filter(|arg| !arg.is_hide_set()) { |
| 82 |
if group_members.iter().any(|gm| gm.get_id() == arg.get_id()) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
let (lhs, rhs) = option_markers(arg); |
| 87 |
line.push(roman(lhs)); |
| 88 |
if let Some(value) = arg.get_value_names() { |
| 89 |
line.push(italic(value.join(" "))); |
| 90 |
} else { |
| 91 |
line.push(italic(arg.get_id().as_str())); |
| 92 |
} |
| 93 |
line.push(roman(rhs)); |
| 94 |
line.push(roman(" ")); |
| 95 |
} |
| 96 |
|
| 97 |
if cmd.has_subcommands() { |
| 98 |
let (lhs, rhs) = subcommand_markers(cmd); |
| 99 |
line.push(roman(lhs)); |
| 100 |
line.push(italic( |
| 101 |
cmd.get_subcommand_value_name() |
| 102 |
.unwrap_or_else(|| subcommand_heading(cmd)) |
| 103 |
.to_lowercase(), |
| 104 |
)); |
| 105 |
line.push(roman(rhs)); |
| 106 |
} |
| 107 |
|
| 108 |
roff.text(line); |
| 109 |
} |
| 110 |
|
| 111 |
fn render_synopsis_option(line: &mut Vec<Inline>, opt: &Arg) { |
| 112 |
let (lhs, rhs) = option_markers(opt); |
| 113 |
match (opt.get_short(), opt.get_long()) { |
| 114 |
(Some(short), Some(long)) => { |
| 115 |
line.push(roman(lhs)); |
| 116 |
line.push(bold(format!("-{short}"))); |
| 117 |
line.push(roman("|")); |
| 118 |
line.push(bold(format!("--{long}"))); |
| 119 |
line.push(roman(rhs)); |
| 120 |
} |
| 121 |
(Some(short), None) => { |
| 122 |
line.push(roman(lhs)); |
| 123 |
line.push(bold(format!("-{short} "))); |
| 124 |
line.push(roman(rhs)); |
| 125 |
} |
| 126 |
(None, Some(long)) => { |
| 127 |
line.push(roman(lhs)); |
| 128 |
line.push(bold(format!("--{long}"))); |
| 129 |
line.push(roman(rhs)); |
| 130 |
} |
| 131 |
(None, None) => return, |
| 132 |
} |
| 133 |
|
| 134 |
if matches!(opt.get_action(), ArgAction::Count) { |
| 135 |
line.push(roman("...")); |
| 136 |
} |
| 137 |
line.push(roman(" ")); |
| 138 |
} |
| 139 |
|
| 140 |
fn unroll_group_args<'a>(cmd: &'a clap::Command, group: &clap::Id) -> Vec<&'a Arg> { |
| 141 |
let mut g_vec = vec![group]; |
| 142 |
let mut args: Vec<&'a Arg> = vec![]; |
| 143 |
|
| 144 |
while let Some(g) = g_vec.pop() { |
| 145 |
let Some(grp) = cmd.get_groups().find(|grp| grp.get_id() == g) else { |
| 146 |
continue; |
| 147 |
}; |
| 148 |
for n in grp.get_args() { |
| 149 |
if args.iter().any(|a| a.get_id() == n) { |
| 150 |
continue; |
| 151 |
} |
| 152 |
if let Some(arg) = cmd.get_arguments().find(|a| a.get_id() == n) { |
| 153 |
args.push(arg); |
| 154 |
} else { |
| 155 |
g_vec.push(n); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
args |
| 161 |
} |
| 162 |
|
| 163 |
fn render_synopsis_arg(arg: &Arg) -> Vec<Inline> { |
| 164 |
if arg.is_positional() { |
| 165 |
let value = arg |
| 166 |
.get_value_names() |
| 167 |
.map(|v| v.join(" ")) |
| 168 |
.unwrap_or_else(|| arg.get_id().as_str().to_owned()); |
| 169 |
return vec![italic(value)]; |
| 170 |
} |
| 171 |
|
| 172 |
let mut inline = if let Some(long) = arg.get_long() { |
| 173 |
vec![bold(format!("--{long}"))] |
| 174 |
} else if let Some(short) = arg.get_short() { |
| 175 |
vec![bold(format!("-{short}"))] |
| 176 |
} else { |
| 177 |
vec![] |
| 178 |
}; |
| 179 |
|
| 180 |
if matches!(arg.get_action(), ArgAction::Count) { |
| 181 |
inline.push(roman("...")); |
| 182 |
} |
| 183 |
|
| 184 |
inline |
| 185 |
} |
| 186 |
|
| 187 |
/// Render the usage set with [`clap::Command::override_usage`]. |
| 188 |
/// |
| 189 |
/// Such a usage may document several invocation forms, one per line, indented |
| 190 |
/// to line up under the `Usage: ` prefix of the help output. Each line is |
| 191 |
/// trimmed and given a line of its own, so that commands documenting more than |
| 192 |
/// one form keep them all visible instead of having them collapsed into the |
| 193 |
/// single line derived from the arguments. Empty lines are dropped. |
| 194 |
/// |
| 195 |
/// A form starting with the name of the command has that name set in bold, to |
| 196 |
/// match the synopsis derived from the arguments. |
| 197 |
fn override_synopsis(roff: &mut Roff, name: &str, usage: &str) { |
| 198 |
let mut first = true; |
| 199 |
|
| 200 |
for form in usage.lines().map(str::trim).filter(|l| !l.is_empty()) { |
| 201 |
// Each form is rendered as its own text line, separated by an explicit |
| 202 |
// break, rather than as a single line holding `Inline::LineBreak`s: a |
| 203 |
// form starting with a control character is only escaped by `roff` when |
| 204 |
// it starts the line it is rendered on. |
| 205 |
if !first { |
| 206 |
roff.control("br", []); |
| 207 |
} |
| 208 |
first = false; |
| 209 |
|
| 210 |
match form.strip_prefix(name) { |
| 211 |
Some(rest) if rest.is_empty() || rest.starts_with(' ') => { |
| 212 |
roff.text([bold(name), roman(rest)]); |
| 213 |
} |
| 214 |
_ => { |
| 215 |
roff.text([roman(form)]); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) { |
| 222 |
let mut sorted_items = items.to_vec(); |
| 223 |
sorted_items.sort_by_key(|opt| option_sort_key(opt)); |
| 224 |
|
| 225 |
for opt in sorted_items.iter().filter(|a| !a.is_positional()) { |
| 226 |
let mut header = match (opt.get_short(), opt.get_long()) { |
| 227 |
(Some(short), Some(long)) => { |
| 228 |
vec![short_option(short), roman(", "), long_option(long)] |
| 229 |
} |
| 230 |
(Some(short), None) => vec![short_option(short)], |
| 231 |
(None, Some(long)) => vec![long_option(long)], |
| 232 |
(None, None) => vec![], |
| 233 |
}; |
| 234 |
|
| 235 |
let arg_range = opt.get_num_args().expect("built"); |
| 236 |
if arg_range.takes_values() { |
| 237 |
if let Some(value_names) = &opt.get_value_names() { |
| 238 |
let (lhs, rhs) = option_value_markers(opt); |
| 239 |
|
| 240 |
header.push(roman(lhs)); |
| 241 |
for (i, name) in value_names.iter().enumerate() { |
| 242 |
if i > 0 { |
| 243 |
header.push(italic(" ")); |
| 244 |
} |
| 245 |
|
| 246 |
let mut val = format!("<{name}>"); |
| 247 |
|
| 248 |
// If this is the last value and it's variadic, add "..." |
| 249 |
let is_last = i == value_names.len() - 1; |
| 250 |
|
| 251 |
if is_last && arg_range.max_values() > value_names.len() { |
| 252 |
val.push_str("..."); |
| 253 |
} |
| 254 |
header.push(italic(val)); |
| 255 |
} |
| 256 |
header.push(roman(rhs)); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if let Some(defs) = option_default_values(opt) { |
| 261 |
header.push(roman(" ")); |
| 262 |
header.push(roman(defs)); |
| 263 |
} |
| 264 |
|
| 265 |
let mut body = vec![]; |
| 266 |
let mut arg_help_written = false; |
| 267 |
if let Some(help) = option_help(opt) { |
| 268 |
arg_help_written = true; |
| 269 |
body.push(roman(help.to_string())); |
| 270 |
} |
| 271 |
|
| 272 |
roff.control("TP", []); |
| 273 |
roff.text(header); |
| 274 |
roff.text(body); |
| 275 |
|
| 276 |
possible_options(roff, opt, arg_help_written); |
| 277 |
|
| 278 |
if let Some(env) = option_environment(opt) { |
| 279 |
roff.control("RS", []); |
| 280 |
roff.text(env); |
| 281 |
roff.control("RE", []); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
for pos in items.iter().filter(|a| a.is_positional()) { |
| 286 |
let mut header = vec![]; |
| 287 |
let (lhs, rhs) = option_markers(pos); |
| 288 |
header.push(roman(lhs)); |
| 289 |
if let Some(value) = pos.get_value_names() { |
| 290 |
header.push(italic(value.join(" "))); |
| 291 |
} else { |
| 292 |
header.push(italic(pos.get_id().as_str())); |
| 293 |
}; |
| 294 |
header.push(roman(rhs)); |
| 295 |
|
| 296 |
if let Some(defs) = option_default_values(pos) { |
| 297 |
header.push(roman(format!(" {defs}"))); |
| 298 |
} |
| 299 |
|
| 300 |
let mut body = vec![]; |
| 301 |
let mut arg_help_written = false; |
| 302 |
if let Some(help) = option_help(pos) { |
| 303 |
body.push(roman(help.to_string())); |
| 304 |
arg_help_written = true; |
| 305 |
} |
| 306 |
|
| 307 |
roff.control("TP", []); |
| 308 |
roff.text(header); |
| 309 |
roff.text(body); |
| 310 |
|
| 311 |
if let Some(env) = option_environment(pos) { |
| 312 |
roff.control("RS", []); |
| 313 |
roff.text(env); |
| 314 |
roff.control("RE", []); |
| 315 |
} |
| 316 |
|
| 317 |
possible_options(roff, pos, arg_help_written); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
fn possible_options(roff: &mut Roff, arg: &Arg, arg_help_written: bool) { |
| 322 |
if let Some(possible_values_text) = get_possible_values(arg) { |
| 323 |
if arg_help_written { |
| 324 |
// It looks nice to have a separation between the help and the values |
| 325 |
roff.text([Inline::LineBreak]); |
| 326 |
} |
| 327 |
roff.text([Inline::LineBreak, italic("Possible values:")]); |
| 328 |
|
| 329 |
// Need to indent twice to get it to look right, because .TP heading indents, but |
| 330 |
// that indent doesn't Carry over to the .IP for the bullets. The standard shift |
| 331 |
// size is 7 for terminal devices |
| 332 |
roff.control("RS", ["14"]); |
| 333 |
for line in possible_values_text { |
| 334 |
roff.control("IP", ["\\(bu", "2"]); |
| 335 |
roff.extend([line]); |
| 336 |
} |
| 337 |
roff.control("RE", []); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
pub(crate) fn subcommands(roff: &mut Roff, cmd: &clap::Command, section: &str) { |
| 342 |
let mut sorted_subcommands: Vec<_> = |
| 343 |
cmd.get_subcommands().filter(|s| !s.is_hide_set()).collect(); |
| 344 |
sorted_subcommands.sort_by_key(|c| subcommand_sort_key(c)); |
| 345 |
for sub in sorted_subcommands { |
| 346 |
roff.control("TP", []); |
| 347 |
|
| 348 |
let name = format!( |
| 349 |
"{}-{}({})", |
| 350 |
cmd.get_display_name().unwrap_or_else(|| cmd.get_name()), |
| 351 |
sub.get_name(), |
| 352 |
section |
| 353 |
); |
| 354 |
roff.text([roman(name)]); |
| 355 |
|
| 356 |
if let Some(about) = sub.get_about().or_else(|| sub.get_long_about()) { |
| 357 |
for line in about.to_string().lines() { |
| 358 |
roff.text([roman(line)]); |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
pub(crate) fn version(cmd: &clap::Command) -> String { |
| 365 |
format!( |
| 366 |
"v{}", |
| 367 |
cmd.get_long_version() |
| 368 |
.or_else(|| cmd.get_version()) |
| 369 |
.unwrap() |
| 370 |
) |
| 371 |
} |
| 372 |
|
| 373 |
pub(crate) fn after_help(roff: &mut Roff, cmd: &clap::Command) { |
| 374 |
if let Some(about) = cmd.get_after_long_help().or_else(|| cmd.get_after_help()) { |
| 375 |
for line in about.to_string().lines() { |
| 376 |
roff.text([roman(line)]); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
fn subcommand_markers(cmd: &clap::Command) -> (&'static str, &'static str) { |
| 382 |
markers(cmd.is_subcommand_required_set()) |
| 383 |
} |
| 384 |
|
| 385 |
fn option_markers(opt: &Arg) -> (&'static str, &'static str) { |
| 386 |
markers(opt.is_required_set()) |
| 387 |
} |
| 388 |
|
| 389 |
fn markers(required: bool) -> (&'static str, &'static str) { |
| 390 |
if required { ("<", ">") } else { ("[", "]") } |
| 391 |
} |
| 392 |
|
| 393 |
fn option_value_markers(arg: &Arg) -> (&'static str, &'static str) { |
| 394 |
let range = arg.get_num_args().expect("built"); |
| 395 |
|
| 396 |
if !range.takes_values() { |
| 397 |
return ("", ""); // no value, so nothing to render |
| 398 |
} |
| 399 |
|
| 400 |
let required = range.min_values() > 0; |
| 401 |
let require_equals = arg.is_require_equals_set(); |
| 402 |
|
| 403 |
match (required, require_equals) { |
| 404 |
// Required, no equals: <VALUE> |
| 405 |
(true, false) => (" ", ""), |
| 406 |
|
| 407 |
// Optional, no equals: [<VALUE>] |
| 408 |
(false, false) => (" [", "]"), |
| 409 |
|
| 410 |
// Optional, with equals: [=<VALUE>] |
| 411 |
(false, true) => ("[=", "]"), |
| 412 |
|
| 413 |
// Required, with equals |
| 414 |
(true, true) => ("=", ""), |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
fn short_option(opt: char) -> Inline { |
| 419 |
bold(format!("-{opt}")) |
| 420 |
} |
| 421 |
|
| 422 |
fn long_option(opt: &str) -> Inline { |
| 423 |
bold(format!("--{opt}")) |
| 424 |
} |
| 425 |
|
| 426 |
fn option_help(opt: &Arg) -> Option<&clap::builder::StyledStr> { |
| 427 |
if !opt.is_hide_long_help_set() { |
| 428 |
let long_help = opt.get_long_help(); |
| 429 |
if long_help.is_some() { |
| 430 |
return long_help; |
| 431 |
} |
| 432 |
} |
| 433 |
if !opt.is_hide_short_help_set() { |
| 434 |
return opt.get_help(); |
| 435 |
} |
| 436 |
|
| 437 |
None |
| 438 |
} |
| 439 |
|
| 440 |
fn option_environment(_opt: &Arg) -> Option<Vec<Inline>> { |
| 441 |
#[cfg(feature = "env")] |
| 442 |
{ |
| 443 |
let opt = _opt; |
| 444 |
if opt.is_hide_env_set() { |
| 445 |
return None; |
| 446 |
} else if let Some(env) = opt.get_env() { |
| 447 |
return Some(vec![ |
| 448 |
roman("May also be specified with the "), |
| 449 |
bold(env.to_string_lossy().into_owned()), |
| 450 |
roman(" environment variable. "), |
| 451 |
]); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
None |
| 456 |
} |
| 457 |
|
| 458 |
fn option_default_values(opt: &Arg) -> Option<String> { |
| 459 |
if opt.is_hide_default_value_set() || !opt.get_num_args().expect("built").takes_values() { |
| 460 |
return None; |
| 461 |
} else if !opt.get_default_values().is_empty() { |
| 462 |
let values = opt |
| 463 |
.get_default_values() |
| 464 |
.iter() |
| 465 |
.map(|s| s.to_string_lossy()) |
| 466 |
.collect::<Vec<_>>() |
| 467 |
.join(","); |
| 468 |
|
| 469 |
return Some(format!("[default: {values}]")); |
| 470 |
} |
| 471 |
|
| 472 |
None |
| 473 |
} |
| 474 |
|
| 475 |
fn get_possible_values(arg: &Arg) -> Option<Vec<Roff>> { |
| 476 |
if arg.is_hide_possible_values_set() { |
| 477 |
return None; |
| 478 |
} |
| 479 |
|
| 480 |
let possibles = &arg.get_possible_values(); |
| 481 |
let possibles: Vec<&clap::builder::PossibleValue> = |
| 482 |
possibles.iter().filter(|pos| !pos.is_hide_set()).collect(); |
| 483 |
|
| 484 |
if !possibles.is_empty() { |
| 485 |
return Some(format_possible_values(&possibles)); |
| 486 |
} |
| 487 |
None |
| 488 |
} |
| 489 |
|
| 490 |
fn format_possible_values(possibles: &Vec<&clap::builder::PossibleValue>) -> Vec<Roff> { |
| 491 |
let mut lines = vec![]; |
| 492 |
for value in possibles { |
| 493 |
let mut roff = Roff::default(); |
| 494 |
let val_name = value.get_name(); |
| 495 |
match value.get_help() { |
| 496 |
Some(help) => { |
| 497 |
roff.text([roman(format!("{val_name}: {help}"))]); |
| 498 |
} |
| 499 |
None => { |
| 500 |
roff.text([roman(val_name.to_owned())]); |
| 501 |
} |
| 502 |
} |
| 503 |
lines.push(roff); |
| 504 |
} |
| 505 |
lines |
| 506 |
} |
| 507 |
|
| 508 |
fn subcommand_sort_key(command: &clap::Command) -> (usize, &str) { |
| 509 |
(command.get_display_order(), command.get_name()) |
| 510 |
} |
| 511 |
|
| 512 |
/// Note that this function is duplicated from `clap::builder` |
| 513 |
fn option_sort_key(arg: &Arg) -> (usize, String) { |
| 514 |
let key = if let Some(x) = arg.get_short() { |
| 515 |
let mut s = x.to_ascii_lowercase().to_string(); |
| 516 |
s.push(if x.is_ascii_lowercase() { '0' } else { '1' }); |
| 517 |
s |
| 518 |
} else if let Some(x) = arg.get_long() { |
| 519 |
x.to_owned() |
| 520 |
} else { |
| 521 |
let mut s = '{'.to_string(); |
| 522 |
s.push_str(arg.get_id().as_str()); |
| 523 |
s |
| 524 |
}; |
| 525 |
(arg.get_display_order(), key) |
| 526 |
} |