Skip to content

Commit

Permalink
style: rustfmt run
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Aug 2, 2018
1 parent 53a7d72 commit eaa0700
Show file tree
Hide file tree
Showing 32 changed files with 1,056 additions and 715 deletions.
16 changes: 9 additions & 7 deletions benches/02_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ use clap::{App, Arg};
use test::Bencher;

macro_rules! create_app {
() => ({
() => {{
App::new("claptests")
.version("0.1")
.about("tests clap library")
.author("Kevin K. <kbknapp@gmail.com>")
.args_from_usage("-f --flag 'tests flags'
.version("0.1")
.about("tests clap library")
.author("Kevin K. <kbknapp@gmail.com>")
.args_from_usage(
"-f --flag 'tests flags'
-o --option=[opt] 'tests options'
[positional] 'tests positional'")
})
[positional] 'tests positional'",
)
}};
}

#[bench]
Expand Down
735 changes: 440 additions & 295 deletions benches/05_ripgrep.rs

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions examples/10_default_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ extern crate clap;
use clap::{App, Arg};

fn main() {
// There are two ways in which to get a default value, one is to use claps Arg::default_value
// method, and the other is to use Rust's built in Option::unwrap_or method.
//
// I'll demo both here.
//
// First, we'll use clap's Arg::default_value with an "INPUT" file.
let matches = App::new("myapp").about("does awesome things")
// There are two ways in which to get a default value, one is to use claps Arg::default_value
// method, and the other is to use Rust's built in Option::unwrap_or method.
//
// I'll demo both here.
//
// First, we'll use clap's Arg::default_value with an "INPUT" file.
let matches = App::new("myapp").about("does awesome things")
.arg(Arg::with_name("INPUT")
.help("The input file to use") // Note, we don't need to specify
// anything like, "Defaults to..."
Expand All @@ -28,13 +28,13 @@ fn main() {
.takes_value(true))
.get_matches();

// It's safe to call unwrap because the value with either be what the user input at runtime
// or "input.txt"
let input = matches.value_of("INPUT").unwrap();
// It's safe to call unwrap because the value with either be what the user input at runtime
// or "input.txt"
let input = matches.value_of("INPUT").unwrap();

// Using Option::unwrap_or we get the same affect, but without the added help text injection
let config_file = matches.value_of("CONFIG").unwrap_or("config.json");
// Using Option::unwrap_or we get the same affect, but without the added help text injection
let config_file = matches.value_of("CONFIG").unwrap_or("config.json");

println!("The input file is: {}", input);
println!("The config file is: {}", config_file);
println!("The input file is: {}", input);
println!("The config file is: {}", config_file);
}
41 changes: 28 additions & 13 deletions src/build/app/settings.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Std
#[allow(unused_imports)]
use std::ascii::AsciiExt;
use std::str::FromStr;
use std::ops::BitOr;
use std::str::FromStr;

bitflags! {
struct Flags: u64 {
Expand Down Expand Up @@ -62,8 +62,11 @@ impl BitOr for AppFlags {
impl Default for AppFlags {
fn default() -> Self {
AppFlags(
Flags::NEEDS_LONG_VERSION | Flags::NEEDS_LONG_HELP | Flags::NEEDS_SC_HELP
| Flags::UTF8_NONE | Flags::COLOR_AUTO,
Flags::NEEDS_LONG_VERSION
| Flags::NEEDS_LONG_HELP
| Flags::NEEDS_SC_HELP
| Flags::UTF8_NONE
| Flags::COLOR_AUTO,
)
}
}
Expand Down Expand Up @@ -713,7 +716,10 @@ pub enum AppSettings {
///
/// assert!(m.subcommand_matches("foo").is_none());
/// ```
#[deprecated(since = "2.27.0", note = "No longer required to propagate values")]
#[deprecated(
since = "2.27.0",
note = "No longer required to propagate values"
)]
PropagateGlobalValuesDown,

/// Allows [`SubCommand`]s to override all requirements of the parent command.
Expand Down Expand Up @@ -925,23 +931,32 @@ pub enum AppSettings {
/// [`SubCommand`]: ./struct.SubCommand.html
WaitOnError,

#[doc(hidden)] NeedsLongVersion,
#[doc(hidden)]
NeedsLongVersion,

#[doc(hidden)] NeedsLongHelp,
#[doc(hidden)]
NeedsLongHelp,

#[doc(hidden)] NeedsSubcommandHelp,
#[doc(hidden)]
NeedsSubcommandHelp,

#[doc(hidden)] LowIndexMultiplePositional,
#[doc(hidden)]
LowIndexMultiplePositional,

#[doc(hidden)] TrailingValues,
#[doc(hidden)]
TrailingValues,

#[doc(hidden)] ValidNegNumFound,
#[doc(hidden)]
ValidNegNumFound,

#[doc(hidden)] Propagated,
#[doc(hidden)]
Propagated,

#[doc(hidden)] ValidArgFound,
#[doc(hidden)]
ValidArgFound,

#[doc(hidden)] ContainsLast,
#[doc(hidden)]
ContainsLast,
}

impl FromStr for AppSettings {
Expand Down
8 changes: 5 additions & 3 deletions src/build/arg/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub enum ArgSettings {
AllowEmptyValues,
/// Sets an arg to be global (i.e. exist in all subcommands)
/// **DEPRECATED**
#[deprecated(since="2.32.0", note="Use `App::global_arg` instead")]
#[deprecated(since = "2.32.0", note = "Use `App::global_arg` instead")]
Global,
/// Hides an arg from the help message
Hidden,
Expand Down Expand Up @@ -119,8 +119,10 @@ pub enum ArgSettings {
HiddenShortHelp,
/// The argument should **not** be shown in long help text
HiddenLongHelp,
#[doc(hidden)] RequiredUnlessAll,
#[doc(hidden)] ValueDelimiterNotSet,
#[doc(hidden)]
RequiredUnlessAll,
#[doc(hidden)]
ValueDelimiterNotSet,
}

impl FromStr for ArgSettings {
Expand Down
4 changes: 3 additions & 1 deletion src/build/arg_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ impl<'a> ArgGroup<'a> {
/// # }
/// ```
#[cfg(feature = "yaml")]
pub fn from_yaml(y: &'a yaml_rust::Yaml) -> ArgGroup<'a> { ArgGroup::from(y.as_hash().unwrap()) }
pub fn from_yaml(y: &'a yaml_rust::Yaml) -> ArgGroup<'a> {
ArgGroup::from(y.as_hash().unwrap())
}

/// Adds an [argument] to this group by name
///
Expand Down
98 changes: 55 additions & 43 deletions src/build/macros.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,75 @@
#[cfg(feature = "yaml")]
macro_rules! yaml_tuple2 {
($a:ident, $v:ident, $c:ident) => {{
if let Some(vec) = $v.as_vec() {
for ys in vec {
if let Some(tup) = ys.as_vec() {
debug_assert_eq!(2, tup.len());
$a = $a.$c(yaml_str!(tup[0]), yaml_str!(tup[1]));
} else {
panic!("Failed to convert YAML value to vec");
}
if let Some(vec) = $v.as_vec() {
for ys in vec {
if let Some(tup) = ys.as_vec() {
debug_assert_eq!(2, tup.len());
$a = $a.$c(yaml_str!(tup[0]), yaml_str!(tup[1]));
} else {
panic!("Failed to convert YAML value to vec");
}
} else {
panic!("Failed to convert YAML value to vec");
}
$a
} else {
panic!("Failed to convert YAML value to vec");
}
};
$a
}};
}

#[cfg(feature = "yaml")]
macro_rules! yaml_tuple3 {
($a:ident, $v:ident, $c:ident) => {{
if let Some(vec) = $v.as_vec() {
for ys in vec {
if let Some(tup) = ys.as_vec() {
debug_assert_eq!(3, tup.len());
$a = $a.$c(yaml_str!(tup[0]), yaml_opt_str!(tup[1]), yaml_str!(tup[2]));
} else {
panic!("Failed to convert YAML value to vec");
}
if let Some(vec) = $v.as_vec() {
for ys in vec {
if let Some(tup) = ys.as_vec() {
debug_assert_eq!(3, tup.len());
$a = $a.$c(yaml_str!(tup[0]), yaml_opt_str!(tup[1]), yaml_str!(tup[2]));
} else {
panic!("Failed to convert YAML value to vec");
}
} else {
panic!("Failed to convert YAML value to vec");
}
$a
} else {
panic!("Failed to convert YAML value to vec");
}
};
$a
}};
}

#[cfg(feature = "yaml")]
macro_rules! yaml_vec_or_str {
($v:ident, $a:ident, $c:ident) => {{
let maybe_vec = $v.as_vec();
if let Some(vec) = maybe_vec {
for ys in vec {
if let Some(s) = ys.as_str() {
$a = $a.$c(s);
} else {
panic!("Failed to convert YAML value {:?} to a string", ys);
}
}
} else {
if let Some(s) = $v.as_str() {
let maybe_vec = $v.as_vec();
if let Some(vec) = maybe_vec {
for ys in vec {
if let Some(s) = ys.as_str() {
$a = $a.$c(s);
} else {
panic!("Failed to convert YAML value {:?} to either a vec or string", $v);
panic!("Failed to convert YAML value {:?} to a string", ys);
}
}
$a
} else {
if let Some(s) = $v.as_str() {
$a = $a.$c(s);
} else {
panic!(
"Failed to convert YAML value {:?} to either a vec or string",
$v
);
}
}
};
$a
}};
}

#[cfg(feature = "yaml")]
macro_rules! yaml_opt_str {
($v:expr) => {{
if $v.is_null() {
Some($v.as_str().unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)))
Some(
$v.as_str()
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)),
)
} else {
None
}
Expand All @@ -76,7 +79,8 @@ macro_rules! yaml_opt_str {
#[cfg(feature = "yaml")]
macro_rules! yaml_str {
($v:expr) => {{
$v.as_str().unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v))
$v.as_str()
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v))
}};
}

Expand All @@ -90,20 +94,28 @@ macro_rules! yaml_to_str {
#[cfg(feature = "yaml")]
macro_rules! yaml_to_bool {
($a:ident, $v:ident, $c:ident) => {{
$a.$c($v.as_bool().unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)))
$a.$c($v
.as_bool()
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)))
}};
}

#[cfg(feature = "yaml")]
macro_rules! yaml_to_u64 {
($a:ident, $v:ident, $c:ident) => {{
$a.$c($v.as_i64().unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)) as u64)
$a.$c($v
.as_i64()
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v))
as u64)
}};
}

#[cfg(feature = "yaml")]
macro_rules! yaml_to_usize {
($a:ident, $v:ident, $c:ident) => {{
$a.$c($v.as_i64().unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)) as usize)
$a.$c($v
.as_i64()
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v))
as usize)
}};
}
4 changes: 2 additions & 2 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod arg;
mod arg_group;
mod usage_parser;

pub use self::usage_parser::UsageParser;
pub use self::app::{App, AppFlags, AppSettings, Propagation};
pub use self::arg::{Arg, ArgFlags, ArgSettings};
pub use self::arg_group::ArgGroup;
pub use self::arg_group::ArgGroup;
pub use self::usage_parser::UsageParser;
8 changes: 5 additions & 3 deletions src/build/usage_parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Internal
use INTERNAL_ERROR_MSG;
use build::{Arg, ArgSettings};
use util::VecMap;
use INTERNAL_ERROR_MSG;

#[derive(PartialEq, Debug)]
enum UsageToken {
Expand Down Expand Up @@ -74,7 +74,8 @@ impl<'a> UsageParser<'a> {

fn name(&mut self, arg: &mut Arg<'a, 'a>) {
debugln!("UsageParser::name;");
if *self.usage
if *self
.usage
.as_bytes()
.get(self.pos)
.expect(INTERNAL_ERROR_MSG) == b'<' && !self.explicit_name_set
Expand Down Expand Up @@ -122,7 +123,8 @@ impl<'a> UsageParser<'a> {
fn short_or_long(&mut self, arg: &mut Arg<'a, 'a>) {
debugln!("UsageParser::short_or_long;");
self.pos += 1;
if *self.usage
if *self
.usage
.as_bytes()
.get(self.pos)
.expect(INTERNAL_ERROR_MSG) == b'-'
Expand Down

0 comments on commit eaa0700

Please sign in to comment.