1#![allow(unused_mut, dead_code)]
2
3use std::fmt::{ Debug, Formatter, Result };
4use crate::{ Command, Argument, ArgumentType, Application };
5
6impl Debug for Argument {
7 fn fmt(&self, f: &mut Formatter) -> Result {
8 match self.ty {
9 ArgumentType::RequiredSingle => write!(f, "<{}>", self.name),
10 ArgumentType::RequiredMultiple => write!(f, "<{}...>", self.name),
11 ArgumentType::OptionalSingle => write!(f, "[{}]", self.name),
12 ArgumentType::OptionalMultiple => write!(f, "[{}...]", self.name),
13 }
14 }
15}
16
17impl Debug for Command {
18 fn fmt(&self, f: &mut Formatter) -> Result {
19 let mut max_len = 0;
20 let mut arg_formats = String::new();
21 let mut lens = vec![];
22
23 for opt in &self.opts {
24 let arg_len = opt.arg.as_ref().map_or(0, |a| format!("{:?}", a).len());
25 let used_space = opt.long.len() + opt.short.len() + arg_len;
26
27 if used_space > max_len {
28 max_len = used_space;
29 }
30
31 lens.insert(0, used_space);
32 }
33
34 for arg in &self.args {
35 arg_formats.push_str(&format!("{:?} ", arg));
36 }
37
38 if self.opts.len() > 0 {
39 write!(f, "Usage: {} {}[options]\n\n", self.name, arg_formats)?;
40 } else {
41 write!(f, "Usage: {} {}\n\n", self.name, arg_formats)?;
42 }
43
44 write!(f, "{}\n\n", self.desc.clone().unwrap_or_default())?;
45
46 if !self.opts.is_empty() {
47 write!(f, "Private options: \n")?;
48
49 for opt in &self.opts {
50 let used_space = lens.pop().unwrap_or_default();
51 let arg_format = opt.arg.as_ref().map_or(String::new(), |a| format!("{:?}", a));
52
53 write!(f, " {}", format!("-{}, --{} {} {}", opt.short, opt.long, arg_format, " ".repeat(max_len - used_space)))?;
54 write!(f, " {}\n", opt.desc.clone().unwrap_or_default())?;
55 }
56 }
57
58 write!(f, "\n")
59 }
60}
61
62
63impl Debug for Application {
64 fn fmt(&self, f: &mut Formatter) -> Result {
65 let mut max_len = 0;
66 let mut lens = vec![];
67
68 for opt in &self.opts {
69 let arg_len = opt.arg.as_ref().map_or(0, |a| format!("{:?}", a).len());
70 let used_space = opt.long.len() + opt.short.len() + arg_len;
71
72 if used_space > max_len {
73 max_len = used_space;
74 }
75
76 lens.insert(0, used_space);
77 }
78
79 write!(f, "Usage: {}", self.name)?;
80
81 if self.cmds.len() > 0 {
82 write!(f, " <command>")?;
83 }
84
85 if self.opts.len() > 0 {
86 write!(f, " [options]")?;
87 }
88
89 if self.direct_args.len() > 0 {
90 write!(f, " OR {} ", self.name)?;
91
92 for arg in self.direct_args.iter() {
93 write!(f, "{:?} ", arg)?;
94 }
95 write!(f, "[options]")?;
96 }
97
98 write!(f, "\n\n{}\n\n", self.desc)?;
99
100 if !self.opts.is_empty() {
101 write!(f, "Public options: \n")?;
102
103 for opt in &self.opts {
104 let used_space = lens.pop().unwrap_or_default();
105 let arg_format = opt.arg.as_ref().map_or(String::new(), |a| format!("{:?}", a));
106
107 write!(f, " {}", format!("-{}, --{} {} {}", opt.short, opt.long, arg_format, " ".repeat(max_len - used_space)))?;
108 write!(f, " {}\n", opt.desc.clone().unwrap_or_default())?;
109 }
110 }
111
112 if !self.cmds.is_empty() {
113 write!(f, "\nCommands:\n")?;
114 max_len = 0;
115
116 for cmd in &self.cmds {
117 let mut used_space = cmd.name.len() + 13;
118
119 for arg in &cmd.args {
120 used_space += format!("{:?}", arg).len() + 1;
121 }
122
123 if used_space > max_len {
124 max_len = used_space;
125 }
126
127 lens.insert(0, used_space);
128 }
129
130 for cmd in &self.cmds {
131 let used_space = lens.pop().unwrap_or_default();
132
133 write!(f, " {} ", cmd.name)?;
134
135 for arg in &cmd.args {
136 write!(f, "{:?} ", arg)?;
137 }
138
139 if cmd.opts.len() > 0 {
140 write!(f, "[options]")?;
141 }
142
143 write!(f, "{} {}\n", " ".repeat(max_len - used_space), cmd.desc.clone().unwrap_or_default())?;
144 }
145 }
146
147 if !self.cmds.is_empty() {
148 write!(f, "\nSee '{} <command> --help' for more information on a specific command\n", self.name)
149 } else {
150 write!(f, "\n")
151 }
152 }
153}