1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#![allow(unused_mut, dead_code)]

use std::fmt::{ Debug, Formatter, Result };
use crate::{ Command, Argument, ArgumentType, Application };

impl Debug for Argument {
    fn fmt(&self, f: &mut Formatter) -> Result {
        match self.ty {
            ArgumentType::RequiredSingle => write!(f, "<{}>", self.name),
            ArgumentType::RequiredMultiple => write!(f, "<{}...>", self.name),
            ArgumentType::OptionalSingle => write!(f, "[{}]", self.name),
            ArgumentType::OptionalMultiple => write!(f, "[{}...]", self.name),
        }
    }
}

impl Debug for Command {
    fn fmt(&self, f: &mut Formatter) -> Result {
        let mut max_len = 0;
        let mut arg_formats = String::new();
        let mut lens = vec![];

        for opt in &self.opts {
            let arg_len = opt.arg.as_ref().map_or(0, |a| format!("{:?}", a).len());
            let used_space = opt.long.len() + opt.short.len() + arg_len;

            if  used_space > max_len {
                max_len = used_space;
            }

            lens.insert(0, used_space);
        }

        for arg in &self.args {
            arg_formats.push_str(&format!("{:?} ", arg));
        }

        if self.opts.len() > 0 {
            write!(f, "Usage: {} {}[options]\n\n", self.name, arg_formats)?;
        } else {
            write!(f, "Usage: {} {}\n\n", self.name, arg_formats)?;
        }

        write!(f, "{}\n\n", self.desc.clone().unwrap_or_default())?;

        if !self.opts.is_empty() {
            write!(f, "Private options: \n")?;

            for opt in &self.opts {
                let used_space = lens.pop().unwrap_or_default();
                let arg_format = opt.arg.as_ref().map_or(String::new(), |a| format!("{:?}", a));

                write!(f, "  {}", format!("-{}, --{} {} {}", opt.short, opt.long, arg_format, " ".repeat(max_len - used_space)))?;
                write!(f, "  {}\n", opt.desc.clone().unwrap_or_default())?;
            }
        }

        write!(f, "\n")
    }
}


impl Debug for Application {
    fn fmt(&self, f: &mut Formatter) -> Result {
        let mut max_len = 0;
        let mut lens = vec![];

        for opt in &self.opts {
            let arg_len = opt.arg.as_ref().map_or(0, |a| format!("{:?}", a).len());
            let used_space = opt.long.len() + opt.short.len() + arg_len;

            if  used_space > max_len {
                max_len = used_space;
            }

            lens.insert(0, used_space);
        }

        write!(f, "Usage: {}", self.name)?;

        if self.cmds.len() > 0 {
            write!(f, " <command>")?;
        }

        if self.opts.len() > 0 {
            write!(f, " [options]")?;
        }

        if self.direct_args.len() > 0 {
            write!(f, " OR {} ", self.name)?;

            for arg in self.direct_args.iter() {
                write!(f, "{:?} ", arg)?;
            }
            write!(f, "[options]")?;
        }

        write!(f, "\n\n{}\n\n", self.desc)?;

        if !self.opts.is_empty() {
            write!(f, "Public options: \n")?;

            for opt in &self.opts {
                let used_space = lens.pop().unwrap_or_default();
                let arg_format = opt.arg.as_ref().map_or(String::new(), |a| format!("{:?}", a));

                write!(f, "  {}", format!("-{}, --{} {} {}", opt.short, opt.long, arg_format, " ".repeat(max_len - used_space)))?;
                write!(f, "  {}\n", opt.desc.clone().unwrap_or_default())?;
            }
        }

        if !self.cmds.is_empty() {
            write!(f, "\nCommands:\n")?;
            max_len = 0;

            for cmd in &self.cmds {
                let mut used_space = cmd.name.len() + 13;

                for arg in &cmd.args {
                    used_space += format!("{:?}", arg).len() + 1;
                }

                if  used_space > max_len {
                    max_len = used_space;
                }

                lens.insert(0, used_space);
            }

            for cmd in &self.cmds {
                let used_space = lens.pop().unwrap_or_default();

                write!(f, "  {} ", cmd.name)?;

                for arg in &cmd.args {
                    write!(f, "{:?} ", arg)?;
                }

                if cmd.opts.len() > 0 {
                    write!(f, "[options]")?;
                }

                write!(f, "{}  {}\n", " ".repeat(max_len - used_space), cmd.desc.clone().unwrap_or_default())?;
            }
        }

        if !self.cmds.is_empty() {
            write!(f, "\nSee '{} <command> --help' for more information on a specific command\n", self.name)
        } else {
            write!(f, "\n")
        }
    }
}