1use std::collections::BTreeSet;
2
3use crate::{
4 buffer::{Block, Doc, Style, Token},
5 info::Info,
6 item::{Item, ShortLong},
7 Meta,
8};
9
10#[doc(hidden)]
11#[derive(Debug, Clone, Copy)]
12pub struct Metavar(pub(crate) &'static str);
13
14#[derive(Debug, Clone, Copy)]
15pub(crate) enum HelpItem<'a> {
16 DecorSuffix {
17 help: &'a Doc,
18 ty: HiTy,
19 },
20 GroupStart {
21 help: &'a Doc,
22 ty: HiTy,
23 },
24 GroupEnd {
25 ty: HiTy,
26 },
27 Any {
28 metavar: &'a Doc,
29 anywhere: bool,
30 help: Option<&'a Doc>,
31 },
32 Positional {
33 metavar: Metavar,
34 help: Option<&'a Doc>,
35 },
36 Command {
37 name: &'static str,
38 short: Option<char>,
39 help: Option<&'a Doc>,
40 meta: &'a Meta,
41 #[cfg(feature = "docgen")]
42 info: &'a Info,
43 },
44 Flag {
45 name: ShortLong,
46 env: Option<&'static str>,
47 help: Option<&'a Doc>,
48 },
49 Argument {
50 name: ShortLong,
51 metavar: Metavar,
52 env: Option<&'static str>,
53 help: Option<&'a Doc>,
54 },
55 AdjacentStart {
56 inner: &'a Meta,
57 ty: HiTy,
58 },
59 AdjacentStop {
60 ty: HiTy,
61 },
62}
63impl HelpItem<'_> {
64 fn has_help(&self) -> bool {
65 match self {
66 HelpItem::Positional { help, .. }
67 | HelpItem::Command { help, .. }
68 | HelpItem::Flag { help, .. }
69 | HelpItem::Any { help, .. }
70 | HelpItem::Argument { help, .. } => help.is_some(),
71 HelpItem::GroupStart { .. } | HelpItem::DecorSuffix { .. } => true,
72 HelpItem::GroupEnd { .. }
73 | HelpItem::AdjacentStart { .. }
74 | HelpItem::AdjacentStop { .. } => false,
75 }
76 }
77
78 fn ty(&self) -> HiTy {
79 match self {
80 HelpItem::GroupStart { ty, .. }
81 | HelpItem::DecorSuffix { ty, .. }
82 | HelpItem::GroupEnd { ty }
83 | HelpItem::AdjacentStart { ty, .. }
84 | HelpItem::AdjacentStop { ty } => *ty,
85 HelpItem::Any {
86 anywhere: false, ..
87 }
88 | HelpItem::Positional { .. } => HiTy::Positional,
89 HelpItem::Command { .. } => HiTy::Command,
90 HelpItem::Any { anywhere: true, .. }
91 | HelpItem::Flag { .. }
92 | HelpItem::Argument { .. } => HiTy::Flag,
93 }
94 }
95}
96
97#[derive(Default, Debug)]
98pub(crate) struct HelpItems<'a> {
102 pub(crate) items: Vec<HelpItem<'a>>,
103}
104
105#[derive(Copy, Clone, Eq, PartialEq, Debug)]
106pub(crate) enum HiTy {
107 Flag,
108 Command,
109 Positional,
110}
111
112enum ItemBlock {
113 No,
114 Decor(HiTy),
115 Anywhere(HiTy),
116}
117
118pub(crate) struct HelpItemsIter<'a, 'b> {
119 items: &'b [HelpItem<'a>],
120 target: HiTy,
121 cur: usize,
122 block: ItemBlock,
123}
124
125impl<'a, 'b> Iterator for HelpItemsIter<'a, 'b> {
126 type Item = &'b HelpItem<'a>;
127
128 fn next(&mut self) -> Option<Self::Item> {
129 loop {
130 let item = self.items.get(self.cur)?;
131 self.cur += 1;
132
133 let keep = match item {
134 HelpItem::AdjacentStart { ty, .. } => {
135 self.block = ItemBlock::Anywhere(*ty);
136 *ty == self.target
137 }
138 HelpItem::GroupStart { ty, .. } => {
139 self.block = ItemBlock::Decor(*ty);
140 *ty == self.target
141 }
142 HelpItem::GroupEnd { ty, .. } | HelpItem::AdjacentStop { ty, .. } => {
143 self.block = ItemBlock::No;
144 *ty == self.target
145 }
146 HelpItem::DecorSuffix { .. }
147 | HelpItem::Any { .. }
148 | HelpItem::Command { .. }
149 | HelpItem::Positional { .. }
150 | HelpItem::Flag { .. }
151 | HelpItem::Argument { .. } => {
152 let ty = item.ty();
153 match self.block {
154 ItemBlock::No => ty == self.target,
155 ItemBlock::Decor(t) => t == self.target,
156 ItemBlock::Anywhere(t) => t == self.target && item.has_help(),
157 }
158 }
159 };
160 if keep {
161 return Some(item);
162 }
163 }
164 }
165}
166
167impl<'a> HelpItems<'a> {
168 #[inline(never)]
169 fn items_of_ty(&self, target: HiTy) -> impl Iterator<Item = &HelpItem<'a>> {
170 HelpItemsIter {
171 items: &self.items,
172 target,
173 cur: 0,
174 block: ItemBlock::No,
175 }
176 }
177}
178
179impl Meta {
180 fn peek_front_ty(&self) -> Option<HiTy> {
181 match self {
182 Meta::And(xs) | Meta::Or(xs) => xs.iter().find_map(Meta::peek_front_ty),
183 Meta::Optional(x)
184 | Meta::Required(x)
185 | Meta::Adjacent(x)
186 | Meta::Many(x)
187 | Meta::Subsection(x, _)
188 | Meta::Suffix(x, _)
189 | Meta::Strict(x)
190 | Meta::CustomUsage(x, _) => x.peek_front_ty(),
191 Meta::Item(i) => Some(HiTy::from(i.as_ref())),
192 Meta::Skip => None,
193 }
194 }
195}
196
197impl<'a> HelpItems<'a> {
198 pub(crate) fn append_meta(&mut self, meta: &'a Meta) {
200 fn go<'a>(hi: &mut HelpItems<'a>, meta: &'a Meta, no_ss: bool) {
201 match meta {
202 Meta::And(xs) | Meta::Or(xs) => {
203 for x in xs {
204 go(hi, x, no_ss);
205 }
206 }
207 Meta::Adjacent(m) => {
208 if let Some(ty) = m.peek_front_ty() {
209 hi.items.push(HelpItem::AdjacentStart {
210 inner: m.as_ref(),
211 ty,
212 });
213 go(hi, m, no_ss);
214 hi.items.push(HelpItem::AdjacentStop { ty });
215 }
216 }
217 Meta::CustomUsage(x, _)
218 | Meta::Required(x)
219 | Meta::Optional(x)
220 | Meta::Many(x)
221 | Meta::Strict(x) => go(hi, x, no_ss),
222 Meta::Item(item) => {
223 if matches!(item.as_ref(), Item::Positional { help: None, .. }) {
224 return;
225 }
226 hi.items.push(HelpItem::from(item.as_ref()));
227 }
228 Meta::Subsection(m, help) => {
229 if let Some(ty) = m.peek_front_ty() {
230 if no_ss {
231 go(hi, m, true);
232 } else {
233 hi.items.push(HelpItem::GroupStart { help, ty });
234 go(hi, m, true);
235 hi.items.push(HelpItem::GroupEnd { ty });
236 }
237 }
238 }
239 Meta::Suffix(m, help) => {
240 if let Some(ty) = m.peek_front_ty() {
241 go(hi, m, no_ss);
242 hi.items.push(HelpItem::DecorSuffix { help, ty });
243 }
244 }
245 Meta::Skip => (),
246 }
247 }
248 go(self, meta, false);
249 }
250
251 fn find_group(&self) -> Option<std::ops::RangeInclusive<usize>> {
252 let start = self
253 .items
254 .iter()
255 .position(|i| matches!(i, HelpItem::GroupStart { .. }))?;
256 let end = self
257 .items
258 .iter()
259 .position(|i| matches!(i, HelpItem::GroupEnd { .. }))?;
260 Some(start..=end)
261 }
262}
263
264impl From<&Item> for HiTy {
265 fn from(value: &Item) -> Self {
266 match value {
267 Item::Positional { .. }
268 | Item::Any {
269 anywhere: false, ..
270 } => Self::Positional,
271 Item::Command { .. } => Self::Command,
272 Item::Any { anywhere: true, .. } | Item::Flag { .. } | Item::Argument { .. } => {
273 Self::Flag
274 }
275 }
276 }
277}
278
279impl<'a> From<&'a Item> for HelpItem<'a> {
280 fn from(item: &'a Item) -> Self {
282 match item {
283 Item::Positional { metavar, help } => Self::Positional {
284 metavar: *metavar,
285 help: help.as_ref(),
286 },
287 Item::Command {
288 name,
289 short,
290 help,
291 meta,
292 #[cfg(feature = "docgen")]
293 info,
294 #[cfg(not(feature = "docgen"))]
295 info: _,
296 } => Self::Command {
297 name,
298 short: *short,
299 help: help.as_ref(),
300 meta,
301 #[cfg(feature = "docgen")]
302 info,
303 },
304 Item::Flag {
305 name,
306 env,
307 help,
308 shorts: _,
309 } => Self::Flag {
310 name: *name,
311 env: *env,
312 help: help.as_ref(),
313 },
314 Item::Argument {
315 name,
316 metavar,
317 env,
318 help,
319 shorts: _,
320 } => Self::Argument {
321 name: *name,
322 metavar: *metavar,
323 env: *env,
324 help: help.as_ref(),
325 },
326 Item::Any {
327 metavar,
328 anywhere,
329 help,
330 } => Self::Any {
331 metavar,
332 anywhere: *anywhere,
333 help: help.as_ref(),
334 },
335 }
336 }
337} impl Doc {
340 #[inline(never)]
341 pub(crate) fn metavar(&mut self, metavar: Metavar) {
342 if metavar
343 .0
344 .chars()
345 .all(|c| c.is_uppercase() || c.is_ascii_digit() || c == '-' || c == '_')
346 {
347 self.write_str(metavar.0, Style::Metavar);
348 } else {
349 self.write_char('<', Style::Metavar);
350 self.write_str(metavar.0, Style::Metavar);
351 self.write_char('>', Style::Metavar);
352 }
353 }
354}
355
356#[allow(clippy::too_many_lines)] fn write_help_item(buf: &mut Doc, item: &HelpItem) {
358 match item {
359 HelpItem::GroupStart { help, .. } => {
360 buf.token(Token::BlockStart(Block::Block));
361 buf.token(Token::BlockStart(Block::Section2));
362 buf.em_doc(help);
363 buf.token(Token::BlockEnd(Block::Section2));
364 buf.token(Token::BlockStart(Block::DefinitionList));
365 }
366 HelpItem::GroupEnd { .. } => {
367 buf.token(Token::BlockEnd(Block::DefinitionList));
368 buf.token(Token::BlockEnd(Block::Block));
369 }
370 HelpItem::DecorSuffix { help, .. } => {
371 buf.token(Token::BlockStart(Block::ItemTerm));
372 buf.token(Token::BlockEnd(Block::ItemTerm));
373 buf.token(Token::BlockStart(Block::ItemBody));
374 buf.doc(help);
375 buf.token(Token::BlockEnd(Block::ItemBody));
376 }
377 HelpItem::Any {
378 metavar,
379 help,
380 anywhere: _,
381 } => {
382 buf.token(Token::BlockStart(Block::ItemTerm));
383 buf.doc(metavar);
384 buf.token(Token::BlockEnd(Block::ItemTerm));
385 if let Some(help) = help {
386 buf.token(Token::BlockStart(Block::ItemBody));
387 buf.doc(help);
388 buf.token(Token::BlockEnd(Block::ItemBody));
389 }
390 }
391 HelpItem::Positional { metavar, help } => {
392 buf.token(Token::BlockStart(Block::ItemTerm));
393 buf.metavar(*metavar);
394 buf.token(Token::BlockEnd(Block::ItemTerm));
395 if let Some(help) = help {
396 buf.token(Token::BlockStart(Block::ItemBody));
397 buf.doc(help);
398 buf.token(Token::BlockEnd(Block::ItemBody));
399 }
400 }
401 HelpItem::Command {
402 name,
403 short,
404 help,
405 meta: _,
406 #[cfg(feature = "docgen")]
407 info: _,
408 } => {
409 buf.token(Token::BlockStart(Block::ItemTerm));
410 buf.write_str(name, Style::Literal);
411 if let Some(short) = short {
412 buf.write_str(", ", Style::Text);
413 buf.write_char(*short, Style::Literal);
414 }
415 buf.token(Token::BlockEnd(Block::ItemTerm));
416 if let Some(help) = help {
417 buf.token(Token::BlockStart(Block::ItemBody));
418 buf.doc(help);
419 buf.token(Token::BlockEnd(Block::ItemBody));
420 }
421 }
422 HelpItem::Flag { name, env, help } => {
423 buf.token(Token::BlockStart(Block::ItemTerm));
424 write_shortlong(buf, *name);
425 buf.token(Token::BlockEnd(Block::ItemTerm));
426 if let Some(help) = help {
427 buf.token(Token::BlockStart(Block::ItemBody));
428 buf.doc(help);
429 buf.token(Token::BlockEnd(Block::ItemBody));
430 }
431 if let Some(env) = env {
432 if help.is_some() {
433 buf.token(Token::BlockStart(Block::ItemTerm));
434 buf.token(Token::BlockEnd(Block::ItemTerm));
435 }
436 buf.token(Token::BlockStart(Block::ItemBody));
437 buf.text("[env: ");
438 buf.literal(env);
439 buf.text("]");
440 buf.token(Token::BlockEnd(Block::ItemBody));
441 }
442 }
443 HelpItem::Argument {
444 name,
445 metavar,
446 env,
447 help,
448 } => {
449 buf.token(Token::BlockStart(Block::ItemTerm));
450 write_shortlong(buf, *name);
451 buf.write_str("=", Style::Text);
452 buf.metavar(*metavar);
453 buf.token(Token::BlockEnd(Block::ItemTerm));
454
455 if let Some(help) = help {
456 buf.token(Token::BlockStart(Block::ItemBody));
457 buf.doc(help);
458 buf.token(Token::BlockEnd(Block::ItemBody));
459 }
460
461 if let Some(env) = env {
462 if help.is_some() {
463 buf.token(Token::BlockStart(Block::ItemTerm));
464 buf.token(Token::BlockEnd(Block::ItemTerm));
465 }
466 buf.token(Token::BlockStart(Block::ItemBody));
467
468 buf.text("[env: ");
469 buf.text(env);
470 buf.text("]");
471
472 buf.token(Token::BlockEnd(Block::ItemBody));
473 }
474 }
475 HelpItem::AdjacentStart { inner, .. } => {
476 buf.token(Token::BlockStart(Block::Section4));
477 buf.write_meta(inner, true);
478 buf.token(Token::BlockEnd(Block::Section4));
479 }
480 HelpItem::AdjacentStop { .. } => {
481 buf.token(Token::BlockStart(Block::Block));
482 buf.token(Token::BlockEnd(Block::Block));
483 }
484 }
485}
486
487fn write_shortlong(buf: &mut Doc, name: ShortLong) {
488 match name {
489 ShortLong::Short(s) => {
490 buf.write_char('-', Style::Literal);
491 buf.write_char(s, Style::Literal);
492 }
493 ShortLong::Long(l) => {
494 buf.write_str(" --", Style::Literal);
495 buf.write_str(l, Style::Literal);
496 }
497 ShortLong::Both(s, l) => {
498 buf.write_char('-', Style::Literal);
499 buf.write_char(s, Style::Literal);
500 buf.write_str(", ", Style::Text);
501 buf.write_str("--", Style::Literal);
502 buf.write_str(l, Style::Literal);
503 }
504 }
505}
506
507#[inline(never)]
508pub(crate) fn render_help(
509 path: &[String],
510 info: &Info,
511 parser_meta: &Meta,
512 help_meta: &Meta,
513) -> Doc {
514 parser_meta.positional_invariant_check(false);
515 let mut buf = Doc::default();
516
517 if let Some(t) = &info.descr {
518 buf.token(Token::BlockStart(Block::Block));
519 buf.doc(t);
520 buf.token(Token::BlockEnd(Block::Block));
521 }
522
523 buf.token(Token::BlockStart(Block::Block));
524 if let Some(usage) = &info.usage {
525 buf.doc(usage);
526 } else {
527 buf.write_str("Usage", Style::Emphasis);
528 buf.write_str(": ", Style::Text);
529 buf.token(Token::BlockStart(Block::Mono));
530 buf.write_path(path);
531 buf.write_meta(parser_meta, true);
532 buf.token(Token::BlockEnd(Block::Mono));
533 }
534 buf.token(Token::BlockEnd(Block::Block));
535
536 if let Some(t) = &info.header {
537 buf.token(Token::BlockStart(Block::Block));
538 buf.doc(t);
539 buf.token(Token::BlockEnd(Block::Block));
540 }
541
542 let mut items = HelpItems::default();
543 items.append_meta(parser_meta);
544 items.append_meta(help_meta);
545
546 buf.write_help_item_groups(items);
547
548 if let Some(footer) = &info.footer {
549 buf.token(Token::BlockStart(Block::Block));
550 buf.doc(footer);
551 buf.token(Token::BlockEnd(Block::Block));
552 }
553 buf
554}
555
556#[derive(Default)]
557struct Dedup {
558 items: BTreeSet<String>,
559 keep: bool,
560}
561
562impl Dedup {
563 fn check(&mut self, item: &HelpItem) -> bool {
564 match item {
565 HelpItem::DecorSuffix { .. } => std::mem::take(&mut self.keep),
566 HelpItem::GroupStart { .. }
567 | HelpItem::GroupEnd { .. }
568 | HelpItem::AdjacentStart { .. }
569 | HelpItem::AdjacentStop { .. } => {
570 self.keep = true;
571 true
572 }
573 HelpItem::Any { metavar, help, .. } => {
574 self.keep = self.items.insert(format!("{:?} {:?}", metavar, help));
575 self.keep
576 }
577 HelpItem::Positional { metavar, help } => {
578 self.keep = self.items.insert(format!("{:?} {:?}", metavar.0, help));
579 self.keep
580 }
581 HelpItem::Command { name, help, .. } => {
582 self.keep = self.items.insert(format!("{:?} {:?}", name, help));
583 self.keep
584 }
585 HelpItem::Flag { name, help, .. } => {
586 self.keep = self.items.insert(format!("{:?} {:?}", name, help));
587 self.keep
588 }
589 HelpItem::Argument {
590 name,
591 metavar,
592 help,
593 ..
594 } => {
595 self.keep = self
596 .items
597 .insert(format!("{:?} {} {:?}", name, metavar.0, help));
598 self.keep
599 }
600 }
601 }
602}
603
604impl Doc {
605 #[inline(never)]
606 pub(crate) fn write_help_item_groups(&mut self, mut items: HelpItems) {
607 while let Some(range) = items.find_group() {
608 let mut dd = Dedup::default();
609 let mut adjacent = 0usize;
610 for item in items.items.drain(range) {
611 match item {
612 HelpItem::AdjacentStart { .. } => adjacent += 1,
613 HelpItem::AdjacentStop { .. } => adjacent = adjacent.saturating_sub(1),
614 _ if adjacent > 0 && !item.has_help() => continue,
615 _ => {}
616 }
617 if dd.check(&item) {
618 write_help_item(self, &item);
619 }
620 }
621 }
622
623 for (ty, name) in [
624 (HiTy::Positional, "Available positional items:"),
625 (HiTy::Flag, "Available options:"),
626 (HiTy::Command, "Available commands:"),
627 ] {
628 self.write_help_items(&items, ty, name);
629 }
630 }
631
632 #[inline(never)]
633 fn write_help_items(&mut self, items: &HelpItems, ty: HiTy, name: &str) {
634 let mut xs = items.items_of_ty(ty).peekable();
635 if xs.peek().is_some() {
636 self.token(Token::BlockStart(Block::Block));
637 self.token(Token::BlockStart(Block::Section2));
638 self.write_str(name, Style::Emphasis);
639 self.token(Token::BlockEnd(Block::Section2));
640 self.token(Token::BlockStart(Block::DefinitionList));
641 let mut dd = Dedup::default();
642 for item in xs {
643 if dd.check(item) {
644 write_help_item(self, item);
645 }
646 }
647 self.token(Token::BlockEnd(Block::DefinitionList));
648 self.token(Token::BlockEnd(Block::Block));
649 }
650 }
651
652 pub(crate) fn write_path(&mut self, path: &[String]) {
653 for item in path {
654 self.write_str(item, Style::Literal);
655 self.write_char(' ', Style::Text);
656 }
657 }
658}