feat(help): sort arguments by name so as to not display a random order

This commit is contained in:
Kevin K 2015-04-05 20:11:23 -04:00
parent 4572c79218
commit f4b2bf5767
3 changed files with 19 additions and 14 deletions

View file

@ -1,4 +1,5 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet; use std::collections::HashSet;
use std::env; use std::env;
@ -44,10 +45,10 @@ pub struct App<'a, 'v, 'ab, 'u, 'ar> {
version: Option<&'v str>, version: Option<&'v str>,
// A brief explaination of the program that gets displayed to the user when shown help/usage information // A brief explaination of the program that gets displayed to the user when shown help/usage information
about: Option<&'ab str>, about: Option<&'ab str>,
flags: HashMap<&'ar str, FlagBuilder<'ar>>, flags: BTreeMap<&'ar str, FlagBuilder<'ar>>,
opts: HashMap<&'ar str, OptBuilder<'ar>>, opts: BTreeMap<&'ar str, OptBuilder<'ar>>,
positionals_idx: BTreeMap<u8, PosBuilder<'ar>>, positionals_idx: BTreeMap<u8, PosBuilder<'ar>>,
subcommands: HashMap<String, App<'a, 'v, 'ab, 'u, 'ar>>, subcommands: BTreeMap<String, App<'a, 'v, 'ab, 'u, 'ar>>,
needs_long_help: bool, needs_long_help: bool,
needs_long_version: bool, needs_long_version: bool,
needs_short_help: bool, needs_short_help: bool,
@ -81,10 +82,10 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
author: None, author: None,
about: None, about: None,
version: None, version: None,
flags: HashMap::new(), flags: BTreeMap::new(),
opts: HashMap::new(), opts: BTreeMap::new(),
positionals_idx: BTreeMap::new(), positionals_idx: BTreeMap::new(),
subcommands: HashMap::new(), subcommands: BTreeMap::new(),
needs_long_version: true, needs_long_version: true,
needs_long_help: true, needs_long_help: true,
needs_short_help: true, needs_short_help: true,
@ -253,7 +254,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
} }
// Check if there is anything in the possible values and add those as well // Check if there is anything in the possible values and add those as well
if let Some(ref p) = a.possible_vals { if let Some(ref p) = a.possible_vals {
let mut phs = HashSet::new(); let mut phs = BTreeSet::new();
// without derefing n = &&str // without derefing n = &&str
for n in p { phs.insert(*n); } for n in p { phs.insert(*n); }
pb.possible_vals = Some(phs); pb.possible_vals = Some(phs);
@ -291,7 +292,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
} }
// Check if there is anything in the possible values and add those as well // Check if there is anything in the possible values and add those as well
if let Some(ref p) = a.possible_vals { if let Some(ref p) = a.possible_vals {
let mut phs = HashSet::new(); let mut phs = BTreeSet::new();
// without derefing n = &&str // without derefing n = &&str
for n in p { phs.insert(*n); } for n in p { phs.insert(*n); }
ob.possible_vals = Some(phs); ob.possible_vals = Some(phs);
@ -896,9 +897,10 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
} }
fn create_help_and_version(&mut self) { fn create_help_and_version(&mut self) {
// name is "hclap_help" because flags are sorted by name
if self.needs_long_help { if self.needs_long_help {
let mut arg = FlagBuilder { let mut arg = FlagBuilder {
name: "clap_help", name: "hclap_help",
short: None, short: None,
long: Some("help"), long: Some("help"),
help: Some("Prints help information"), help: Some("Prints help information"),
@ -909,11 +911,12 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
if self.needs_short_help { if self.needs_short_help {
arg.short = Some('h'); arg.short = Some('h');
} }
self.flags.insert("clap_help", arg); self.flags.insert("hclap_help", arg);
} }
if self.needs_long_version { if self.needs_long_version {
// name is "vclap_version" because flags are sorted by name
let mut arg = FlagBuilder { let mut arg = FlagBuilder {
name: "clap_version", name: "vclap_version",
short: None, short: None,
long: Some("version"), long: Some("version"),
help: Some("Prints version information"), help: Some("Prints version information"),
@ -924,7 +927,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
if self.needs_short_version { if self.needs_short_version {
arg.short = Some('v'); arg.short = Some('v');
} }
self.flags.insert("clap_version", arg); self.flags.insert("vclap_version", arg);
} }
if self.needs_subcmd_help && !self.subcommands.is_empty() { if self.needs_subcmd_help && !self.subcommands.is_empty() {
self.subcommands.insert("help".to_owned(), App::new("help").about("Prints this message")); self.subcommands.insert("help".to_owned(), App::new("help").about("Prints this message"));

View file

@ -1,4 +1,5 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::collections::BTreeSet;
/// `OptArg` represents a option argument for command line applications, which is one that /// `OptArg` represents a option argument for command line applications, which is one that
/// takes an additional value. Option arguments are always preceded by either a `-` /// takes an additional value. Option arguments are always preceded by either a `-`
@ -41,7 +42,7 @@ pub struct OptBuilder<'n> {
/// exclusive arguments are evaluated. /// exclusive arguments are evaluated.
pub required: bool, pub required: bool,
/// A list of possible values for this argument /// A list of possible values for this argument
pub possible_vals: Option<HashSet<&'n str>>, pub possible_vals: Option<BTreeSet<&'n str>>,
/// A list of names of other arguments that are *required* to be used when /// A list of names of other arguments that are *required* to be used when
/// this flag is used /// this flag is used
pub requires: Option<HashSet<&'n str>>, pub requires: Option<HashSet<&'n str>>,

View file

@ -1,4 +1,5 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::collections::BTreeSet;
/// `PosArg` represents a positional argument, i.e. one that isn't preceded /// `PosArg` represents a positional argument, i.e. one that isn't preceded
/// by a `-` or `--`. `PosArg` isn't directly used by the end application /// by a `-` or `--`. `PosArg` isn't directly used by the end application
@ -40,7 +41,7 @@ pub struct PosBuilder<'n> {
/// A list of names for other arguments that *may not* be used with this flag /// A list of names for other arguments that *may not* be used with this flag
pub blacklist: Option<HashSet<&'n str>>, pub blacklist: Option<HashSet<&'n str>>,
/// A list of possible values for this argument /// A list of possible values for this argument
pub possible_vals: Option<HashSet<&'n str>>, pub possible_vals: Option<BTreeSet<&'n str>>,
/// The index of the argument /// The index of the argument
pub index: u8 pub index: u8
} }