bird_tool_utils_man/
option.rs

1use super::*;
2use roff::{bold, italic, list};
3
4/// Arguments that take values.
5#[derive(Debug, Clone)]
6pub struct Opt {
7  pub(crate) name: String,
8  pub(crate) default: Option<String>,
9  pub(crate) help: Option<String>,
10  pub(crate) short: Option<String>,
11  pub(crate) long: Option<String>,
12}
13
14impl Opt {
15  /// Create a new instance.
16  pub fn new(name: &str) -> Self {
17    Self {
18      name: name.into(),
19      default: None,
20      help: None,
21      short: None,
22      long: None,
23    }
24  }
25
26  /// Set the default value.
27  pub fn default_value(mut self, default: &str) -> Self {
28    self.default = Some(default.into());
29    self
30  }
31
32  /// Set the help.
33  pub fn help(mut self, help: &str) -> Self {
34    self.help = Some(help.into());
35    self
36  }
37
38  /// Set the short value.
39  pub fn short(mut self, short: &str) -> Self {
40    self.short = Some(short.into());
41    self
42  }
43
44  /// Set the long value.
45  pub fn long(mut self, long: &str) -> Self {
46    self.long = Some(long.into());
47    self
48  }
49}
50
51impl FlagOrOption for Opt {
52  fn render(&self) -> String {
53    let mut args: Vec<String> = vec![];
54    if let Some(ref short) = self.short {
55      args.push(bold(&short));
56    }
57    if let Some(ref long) = self.long {
58      if !args.is_empty() {
59        args.push(", ".to_string());
60      }
61      args.push(bold(&long));
62    }
63    args.push(" ".into());
64    args.push(italic(&self.name));
65    if let Some(ref default) = self.default {
66      if !args.is_empty() {
67        args.push(" ".to_string());
68      }
69      args.push("[".into());
70      args.push("default:".into());
71      args.push(" ".into());
72      args.push(italic(&default));
73      args.push("]".into());
74    }
75    let desc = match self.help {
76      Some(ref desc) => desc.to_string(),
77      None => "".to_string(),
78    };
79    list(&args, &[desc])
80  }
81}