diff --git a/src/token.rs b/src/token.rs index 3eee0c2..e1ca886 100644 --- a/src/token.rs +++ b/src/token.rs @@ -136,7 +136,7 @@ impl Token { } if !self.long_name.is_empty() { - repr.push_str(" | --"); + repr.push_str("|--"); repr.push_str(&self.long_name); } diff --git a/src/vars.rs b/src/vars.rs index 3b6184b..979cc88 100644 --- a/src/vars.rs +++ b/src/vars.rs @@ -32,9 +32,25 @@ pub fn vars(program_name: &str, options: &[&str]) -> Result { let mut tokens: Vec = Vec::new(); let mut opts: HashMap = HashMap::new(); let mut args: VecDeque = VecDeque::new(); - let mut longest_token_len: usize = 0; let mut index: usize = 0; + + // The first option should be the help option, i.e. -h, --help + let help_token = Token { + short_name: String::from("h"), + long_name: String::from("help"), + description: String::from("Display usage information"), + is_arg: false, + has_arg: false, + is_group: false, + padding: 0 + }; + opts.insert(help_token.short_name.clone(), index); + opts.insert(help_token.long_name.clone(), index); + let mut longest_token_len: usize = help_token.len(); + tokens.push(help_token); + index += 1; + // Second, add the other, user defined options for opt in options.iter() { let token = match token(opt) { Ok(t) => t, @@ -54,34 +70,22 @@ pub fn vars(program_name: &str, options: &[&str]) -> Result { } } - let token_len = token.len(); - if token_len > 0 { - if token_len > longest_token_len { - longest_token_len = token_len; - for t in tokens.iter_mut() { - let diff = longest_token_len - t.len(); - t.adjust_padding(diff); - } - } + if token.len() > longest_token_len { + longest_token_len = token.len(); } } tokens.push(token); index += 1; } - let help_token = Token { - short_name: String::from("h"), - long_name: String::from("help"), - description: String::from("Display usage information"), - is_arg: false, - has_arg: false, - is_group: false, - padding: 0 - }; - - tokens.push(help_token); - opts.insert(String::from("h"), index); - opts.insert(String::from("help"), index); + // Finally, adjust the padding for each token so they display properly + for t in tokens.iter_mut() { + let token_len = t.len(); + if token_len < longest_token_len { + let delta = longest_token_len - token_len; + t.adjust_padding(delta); + } + } Ok(Vars { opts: opts,