Fixed usage display formatting
This commit is contained in:
parent
77f65c79c2
commit
75c277db3a
2 changed files with 28 additions and 24 deletions
|
@ -136,7 +136,7 @@ impl Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.long_name.is_empty() {
|
if !self.long_name.is_empty() {
|
||||||
repr.push_str(" | --");
|
repr.push_str("|--");
|
||||||
repr.push_str(&self.long_name);
|
repr.push_str(&self.long_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
50
src/vars.rs
50
src/vars.rs
|
@ -32,9 +32,25 @@ pub fn vars(program_name: &str, options: &[&str]) -> Result<Vars, Error> {
|
||||||
let mut tokens: Vec<Token> = Vec::new();
|
let mut tokens: Vec<Token> = Vec::new();
|
||||||
let mut opts: HashMap<String, usize> = HashMap::new();
|
let mut opts: HashMap<String, usize> = HashMap::new();
|
||||||
let mut args: VecDeque<usize> = VecDeque::new();
|
let mut args: VecDeque<usize> = VecDeque::new();
|
||||||
let mut longest_token_len: usize = 0;
|
|
||||||
let mut index: 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() {
|
for opt in options.iter() {
|
||||||
let token = match token(opt) {
|
let token = match token(opt) {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
|
@ -54,34 +70,22 @@ pub fn vars(program_name: &str, options: &[&str]) -> Result<Vars, Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let token_len = token.len();
|
if token.len() > longest_token_len {
|
||||||
if token_len > 0 {
|
longest_token_len = token.len();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tokens.push(token);
|
tokens.push(token);
|
||||||
index += 1;
|
index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let help_token = Token {
|
// Finally, adjust the padding for each token so they display properly
|
||||||
short_name: String::from("h"),
|
for t in tokens.iter_mut() {
|
||||||
long_name: String::from("help"),
|
let token_len = t.len();
|
||||||
description: String::from("Display usage information"),
|
if token_len < longest_token_len {
|
||||||
is_arg: false,
|
let delta = longest_token_len - token_len;
|
||||||
has_arg: false,
|
t.adjust_padding(delta);
|
||||||
is_group: false,
|
}
|
||||||
padding: 0
|
}
|
||||||
};
|
|
||||||
|
|
||||||
tokens.push(help_token);
|
|
||||||
opts.insert(String::from("h"), index);
|
|
||||||
opts.insert(String::from("help"), index);
|
|
||||||
|
|
||||||
Ok(Vars {
|
Ok(Vars {
|
||||||
opts: opts,
|
opts: opts,
|
||||||
|
|
Loading…
Add table
Reference in a new issue