mirror of
https://github.com/clap-rs/clap
synced 2025-01-18 23:53:54 +00:00
Merge pull request #1062 from kbknapp/bluejekyll-from_env
Bluejekyll from env
This commit is contained in:
commit
797b6b3340
12 changed files with 844 additions and 290 deletions
316
src/app/help.rs
316
src/app/help.rs
|
@ -10,22 +10,19 @@ use app::{App, AppSettings};
|
||||||
use app::parser::Parser;
|
use app::parser::Parser;
|
||||||
use args::{AnyArg, ArgSettings, DispOrder};
|
use args::{AnyArg, ArgSettings, DispOrder};
|
||||||
use errors::{Error, Result as ClapResult};
|
use errors::{Error, Result as ClapResult};
|
||||||
use fmt::{Format, Colorizer, ColorizerOption};
|
use fmt::{Colorizer, ColorizerOption, Format};
|
||||||
use app::usage;
|
use app::usage;
|
||||||
use map::VecMap;
|
use map::VecMap;
|
||||||
|
|
||||||
// Third Party
|
// Third Party
|
||||||
use unicode_width::UnicodeWidthStr;
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
#[cfg(feature = "wrap_help")]
|
||||||
|
use term_size;
|
||||||
use textwrap;
|
use textwrap;
|
||||||
|
|
||||||
#[cfg(feature = "wrap_help")]
|
|
||||||
fn term_width() -> usize {
|
|
||||||
textwrap::termwidth()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "wrap_help"))]
|
#[cfg(not(feature = "wrap_help"))]
|
||||||
fn term_width() -> usize {
|
mod term_size {
|
||||||
120
|
pub fn dimensions() -> Option<(usize, usize)> { None }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn str_width(s: &str) -> usize { UnicodeWidthStr::width(s) }
|
fn str_width(s: &str) -> usize { UnicodeWidthStr::width(s) }
|
||||||
|
@ -34,13 +31,18 @@ const TAB: &'static str = " ";
|
||||||
|
|
||||||
// These are just convenient traits to make the code easier to read.
|
// These are just convenient traits to make the code easier to read.
|
||||||
trait ArgWithDisplay<'b, 'c>: AnyArg<'b, 'c> + Display {}
|
trait ArgWithDisplay<'b, 'c>: AnyArg<'b, 'c> + Display {}
|
||||||
impl<'b, 'c, T> ArgWithDisplay<'b, 'c> for T where T: AnyArg<'b, 'c> + Display {}
|
impl<'b, 'c, T> ArgWithDisplay<'b, 'c> for T
|
||||||
|
where
|
||||||
|
T: AnyArg<'b, 'c> + Display,
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
trait ArgWithOrder<'b, 'c>: ArgWithDisplay<'b, 'c> + DispOrder {
|
trait ArgWithOrder<'b, 'c>: ArgWithDisplay<'b, 'c> + DispOrder {
|
||||||
fn as_base(&self) -> &ArgWithDisplay<'b, 'c>;
|
fn as_base(&self) -> &ArgWithDisplay<'b, 'c>;
|
||||||
}
|
}
|
||||||
impl<'b, 'c, T> ArgWithOrder<'b, 'c> for T
|
impl<'b, 'c, T> ArgWithOrder<'b, 'c> for T
|
||||||
where T: ArgWithDisplay<'b, 'c> + DispOrder
|
where
|
||||||
|
T: ArgWithDisplay<'b, 'c> + DispOrder,
|
||||||
{
|
{
|
||||||
fn as_base(&self) -> &ArgWithDisplay<'b, 'c> { self }
|
fn as_base(&self) -> &ArgWithDisplay<'b, 'c> { self }
|
||||||
}
|
}
|
||||||
|
@ -87,29 +89,34 @@ pub struct Help<'a> {
|
||||||
impl<'a> Help<'a> {
|
impl<'a> Help<'a> {
|
||||||
/// Create a new `Help` instance.
|
/// Create a new `Help` instance.
|
||||||
#[cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))]
|
#[cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))]
|
||||||
pub fn new(w: &'a mut Write,
|
pub fn new(
|
||||||
next_line_help: bool,
|
w: &'a mut Write,
|
||||||
hide_pv: bool,
|
next_line_help: bool,
|
||||||
color: bool,
|
hide_pv: bool,
|
||||||
cizer: Colorizer,
|
color: bool,
|
||||||
term_w: Option<usize>,
|
cizer: Colorizer,
|
||||||
max_w: Option<usize>,
|
term_w: Option<usize>,
|
||||||
use_long: bool)
|
max_w: Option<usize>,
|
||||||
-> Self {
|
use_long: bool,
|
||||||
|
) -> Self {
|
||||||
debugln!("Help::new;");
|
debugln!("Help::new;");
|
||||||
Help {
|
Help {
|
||||||
writer: w,
|
writer: w,
|
||||||
next_line_help: next_line_help,
|
next_line_help: next_line_help,
|
||||||
hide_pv: hide_pv,
|
hide_pv: hide_pv,
|
||||||
term_w: match term_w {
|
term_w: match term_w {
|
||||||
Some(width) => if width == 0 { usize::MAX } else { width },
|
Some(width) => if width == 0 {
|
||||||
None => {
|
usize::MAX
|
||||||
cmp::min(term_width(),
|
} else {
|
||||||
match max_w {
|
width
|
||||||
None | Some(0) => usize::MAX,
|
},
|
||||||
Some(mw) => mw,
|
None => cmp::min(
|
||||||
})
|
term_size::dimensions().map_or(120, |(w, _)| w),
|
||||||
}
|
match max_w {
|
||||||
|
None | Some(0) => usize::MAX,
|
||||||
|
Some(mw) => mw,
|
||||||
|
},
|
||||||
|
),
|
||||||
},
|
},
|
||||||
color: color,
|
color: color,
|
||||||
cizer: cizer,
|
cizer: cizer,
|
||||||
|
@ -142,7 +149,12 @@ impl<'a> Help<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn _write_parser_help(w: &'a mut Write, parser: &Parser, stderr: bool, use_long: bool) -> ClapResult<()> {
|
pub fn _write_parser_help(
|
||||||
|
w: &'a mut Write,
|
||||||
|
parser: &Parser,
|
||||||
|
stderr: bool,
|
||||||
|
use_long: bool,
|
||||||
|
) -> ClapResult<()> {
|
||||||
debugln!("Help::write_parser_help;");
|
debugln!("Help::write_parser_help;");
|
||||||
let nlh = parser.is_set(AppSettings::NextLineHelp);
|
let nlh = parser.is_set(AppSettings::NextLineHelp);
|
||||||
let hide_v = parser.is_set(AppSettings::HidePossibleValuesInHelp);
|
let hide_v = parser.is_set(AppSettings::HidePossibleValuesInHelp);
|
||||||
|
@ -151,15 +163,16 @@ impl<'a> Help<'a> {
|
||||||
use_stderr: stderr,
|
use_stderr: stderr,
|
||||||
when: parser.color(),
|
when: parser.color(),
|
||||||
});
|
});
|
||||||
Self::new(w,
|
Self::new(
|
||||||
nlh,
|
w,
|
||||||
hide_v,
|
nlh,
|
||||||
color,
|
hide_v,
|
||||||
cizer,
|
color,
|
||||||
parser.meta.term_w,
|
cizer,
|
||||||
parser.meta.max_w,
|
parser.meta.term_w,
|
||||||
use_long)
|
parser.meta.max_w,
|
||||||
.write_help(parser)
|
use_long,
|
||||||
|
).write_help(parser)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the parser help to the wrapped stream.
|
/// Writes the parser help to the wrapped stream.
|
||||||
|
@ -180,16 +193,16 @@ impl<'a> Help<'a> {
|
||||||
impl<'a> Help<'a> {
|
impl<'a> Help<'a> {
|
||||||
/// Writes help for each argument in the order they were declared to the wrapped stream.
|
/// Writes help for each argument in the order they were declared to the wrapped stream.
|
||||||
fn write_args_unsorted<'b: 'd, 'c: 'd, 'd, I: 'd>(&mut self, args: I) -> io::Result<()>
|
fn write_args_unsorted<'b: 'd, 'c: 'd, 'd, I: 'd>(&mut self, args: I) -> io::Result<()>
|
||||||
where I: Iterator<Item = &'d ArgWithOrder<'b, 'c>>
|
where
|
||||||
|
I: Iterator<Item = &'d ArgWithOrder<'b, 'c>>,
|
||||||
{
|
{
|
||||||
debugln!("Help::write_args_unsorted;");
|
debugln!("Help::write_args_unsorted;");
|
||||||
// The shortest an arg can legally be is 2 (i.e. '-x')
|
// The shortest an arg can legally be is 2 (i.e. '-x')
|
||||||
self.longest = 2;
|
self.longest = 2;
|
||||||
let mut arg_v = Vec::with_capacity(10);
|
let mut arg_v = Vec::with_capacity(10);
|
||||||
for arg in args.filter(|arg| {
|
for arg in args.filter(|arg| {
|
||||||
!(arg.is_set(ArgSettings::Hidden)) ||
|
!(arg.is_set(ArgSettings::Hidden)) || arg.is_set(ArgSettings::NextLineHelp)
|
||||||
arg.is_set(ArgSettings::NextLineHelp)
|
}) {
|
||||||
}) {
|
|
||||||
if arg.longest_filter() {
|
if arg.longest_filter() {
|
||||||
self.longest = cmp::max(self.longest, str_width(arg.to_string().as_str()));
|
self.longest = cmp::max(self.longest, str_width(arg.to_string().as_str()));
|
||||||
}
|
}
|
||||||
|
@ -209,7 +222,8 @@ impl<'a> Help<'a> {
|
||||||
|
|
||||||
/// Sorts arguments by length and display order and write their help to the wrapped stream.
|
/// Sorts arguments by length and display order and write their help to the wrapped stream.
|
||||||
fn write_args<'b: 'd, 'c: 'd, 'd, I: 'd>(&mut self, args: I) -> io::Result<()>
|
fn write_args<'b: 'd, 'c: 'd, 'd, I: 'd>(&mut self, args: I) -> io::Result<()>
|
||||||
where I: Iterator<Item = &'d ArgWithOrder<'b, 'c>>
|
where
|
||||||
|
I: Iterator<Item = &'d ArgWithOrder<'b, 'c>>,
|
||||||
{
|
{
|
||||||
debugln!("Help::write_args;");
|
debugln!("Help::write_args;");
|
||||||
// The shortest an arg can legally be is 2 (i.e. '-x')
|
// The shortest an arg can legally be is 2 (i.e. '-x')
|
||||||
|
@ -338,9 +352,9 @@ impl<'a> Help<'a> {
|
||||||
let h_w = str_width(h) + str_width(&*spec_vals);
|
let h_w = str_width(h) + str_width(&*spec_vals);
|
||||||
let nlh = self.next_line_help || arg.is_set(ArgSettings::NextLineHelp);
|
let nlh = self.next_line_help || arg.is_set(ArgSettings::NextLineHelp);
|
||||||
let taken = self.longest + 12;
|
let taken = self.longest + 12;
|
||||||
self.force_next_line = !nlh && self.term_w >= taken &&
|
self.force_next_line = !nlh && self.term_w >= taken
|
||||||
(taken as f32 / self.term_w as f32) > 0.40 &&
|
&& (taken as f32 / self.term_w as f32) > 0.40
|
||||||
h_w > (self.term_w - taken);
|
&& h_w > (self.term_w - taken);
|
||||||
|
|
||||||
debug!("Help::val: Has switch...");
|
debug!("Help::val: Has switch...");
|
||||||
if arg.has_switch() {
|
if arg.has_switch() {
|
||||||
|
@ -348,10 +362,12 @@ impl<'a> Help<'a> {
|
||||||
debugln!("Help::val: force_next_line...{:?}", self.force_next_line);
|
debugln!("Help::val: force_next_line...{:?}", self.force_next_line);
|
||||||
debugln!("Help::val: nlh...{:?}", nlh);
|
debugln!("Help::val: nlh...{:?}", nlh);
|
||||||
debugln!("Help::val: taken...{}", taken);
|
debugln!("Help::val: taken...{}", taken);
|
||||||
debugln!("Help::val: help_width > (width - taken)...{} > ({} - {})",
|
debugln!(
|
||||||
h_w,
|
"Help::val: help_width > (width - taken)...{} > ({} - {})",
|
||||||
self.term_w,
|
h_w,
|
||||||
taken);
|
self.term_w,
|
||||||
|
taken
|
||||||
|
);
|
||||||
debugln!("Help::val: longest...{}", self.longest);
|
debugln!("Help::val: longest...{}", self.longest);
|
||||||
debug!("Help::val: next_line...");
|
debug!("Help::val: next_line...");
|
||||||
if !(nlh || self.force_next_line) {
|
if !(nlh || self.force_next_line) {
|
||||||
|
@ -375,7 +391,10 @@ impl<'a> Help<'a> {
|
||||||
}
|
}
|
||||||
} else if !(nlh || self.force_next_line) {
|
} else if !(nlh || self.force_next_line) {
|
||||||
sdebugln!("No, and not next_line");
|
sdebugln!("No, and not next_line");
|
||||||
write_nspaces!(self.writer, self.longest + 4 - (str_width(arg.to_string().as_str())));
|
write_nspaces!(
|
||||||
|
self.writer,
|
||||||
|
self.longest + 4 - (str_width(arg.to_string().as_str()))
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
sdebugln!("No");
|
sdebugln!("No");
|
||||||
}
|
}
|
||||||
|
@ -386,19 +405,25 @@ impl<'a> Help<'a> {
|
||||||
debugln!("Help::write_before_after_help;");
|
debugln!("Help::write_before_after_help;");
|
||||||
let mut help = String::from(h);
|
let mut help = String::from(h);
|
||||||
// determine if our help fits or needs to wrap
|
// determine if our help fits or needs to wrap
|
||||||
debugln!("Help::write_before_after_help: Term width...{}",
|
debugln!(
|
||||||
self.term_w);
|
"Help::write_before_after_help: Term width...{}",
|
||||||
|
self.term_w
|
||||||
|
);
|
||||||
let too_long = str_width(h) >= self.term_w;
|
let too_long = str_width(h) >= self.term_w;
|
||||||
|
|
||||||
debug!("Help::write_before_after_help: Too long...");
|
debug!("Help::write_before_after_help: Too long...");
|
||||||
if too_long || h.contains("{n}") {
|
if too_long || h.contains("{n}") {
|
||||||
sdebugln!("Yes");
|
sdebugln!("Yes");
|
||||||
debugln!("Help::write_before_after_help: help: {}", help);
|
debugln!("Help::write_before_after_help: help: {}", help);
|
||||||
debugln!("Help::write_before_after_help: help width: {}",
|
debugln!(
|
||||||
str_width(&*help));
|
"Help::write_before_after_help: help width: {}",
|
||||||
|
str_width(&*help)
|
||||||
|
);
|
||||||
// Determine how many newlines we need to insert
|
// Determine how many newlines we need to insert
|
||||||
debugln!("Help::write_before_after_help: Usable space: {}",
|
debugln!(
|
||||||
self.term_w);
|
"Help::write_before_after_help: Usable space: {}",
|
||||||
|
self.term_w
|
||||||
|
);
|
||||||
help = wrap_help(&help.replace("{n}", "\n"), self.term_w);
|
help = wrap_help(&help.replace("{n}", "\n"), self.term_w);
|
||||||
} else {
|
} else {
|
||||||
sdebugln!("No");
|
sdebugln!("No");
|
||||||
|
@ -467,42 +492,60 @@ impl<'a> Help<'a> {
|
||||||
fn spec_vals(&self, a: &ArgWithDisplay) -> String {
|
fn spec_vals(&self, a: &ArgWithDisplay) -> String {
|
||||||
debugln!("Help::spec_vals: a={}", a);
|
debugln!("Help::spec_vals: a={}", a);
|
||||||
let mut spec_vals = vec![];
|
let mut spec_vals = vec![];
|
||||||
|
if let Some(ref env) = a.env() {
|
||||||
|
debugln!(
|
||||||
|
"Help::spec_vals: Found environment variable...[{:?}:{:?}]",
|
||||||
|
env.0,
|
||||||
|
env.1
|
||||||
|
);
|
||||||
|
spec_vals.push(format!(
|
||||||
|
" [env:{}: {}]",
|
||||||
|
env.0.to_string_lossy(),
|
||||||
|
env.1.to_string_lossy()
|
||||||
|
));
|
||||||
|
}
|
||||||
if !a.is_set(ArgSettings::HideDefaultValue) {
|
if !a.is_set(ArgSettings::HideDefaultValue) {
|
||||||
if let Some(pv) = a.default_val() {
|
if let Some(pv) = a.default_val() {
|
||||||
debugln!("Help::spec_vals: Found default value...[{:?}]", pv);
|
debugln!("Help::spec_vals: Found default value...[{:?}]", pv);
|
||||||
spec_vals.push(format!(" [default: {}]",
|
spec_vals.push(format!(
|
||||||
if self.color {
|
" [default: {}]",
|
||||||
self.cizer.good(pv.to_string_lossy())
|
if self.color {
|
||||||
} else {
|
self.cizer.good(pv.to_string_lossy())
|
||||||
Format::None(pv.to_string_lossy())
|
} else {
|
||||||
}));
|
Format::None(pv.to_string_lossy())
|
||||||
|
}
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(ref aliases) = a.aliases() {
|
if let Some(ref aliases) = a.aliases() {
|
||||||
debugln!("Help::spec_vals: Found aliases...{:?}", aliases);
|
debugln!("Help::spec_vals: Found aliases...{:?}", aliases);
|
||||||
spec_vals.push(format!(" [aliases: {}]",
|
spec_vals.push(format!(
|
||||||
if self.color {
|
" [aliases: {}]",
|
||||||
aliases
|
if self.color {
|
||||||
.iter()
|
aliases
|
||||||
.map(|v| format!("{}", self.cizer.good(v)))
|
.iter()
|
||||||
.collect::<Vec<_>>()
|
.map(|v| format!("{}", self.cizer.good(v)))
|
||||||
.join(", ")
|
.collect::<Vec<_>>()
|
||||||
} else {
|
.join(", ")
|
||||||
aliases.join(", ")
|
} else {
|
||||||
}));
|
aliases.join(", ")
|
||||||
|
}
|
||||||
|
));
|
||||||
}
|
}
|
||||||
if !self.hide_pv && !a.is_set(ArgSettings::HidePossibleValues) {
|
if !self.hide_pv && !a.is_set(ArgSettings::HidePossibleValues) {
|
||||||
if let Some(pv) = a.possible_vals() {
|
if let Some(pv) = a.possible_vals() {
|
||||||
debugln!("Help::spec_vals: Found possible vals...{:?}", pv);
|
debugln!("Help::spec_vals: Found possible vals...{:?}", pv);
|
||||||
spec_vals.push(if self.color {
|
spec_vals.push(if self.color {
|
||||||
format!(" [values: {}]",
|
format!(
|
||||||
pv.iter()
|
" [values: {}]",
|
||||||
.map(|v| format!("{}", self.cizer.good(v)))
|
pv.iter()
|
||||||
.collect::<Vec<_>>()
|
.map(|v| format!("{}", self.cizer.good(v)))
|
||||||
.join(", "))
|
.collect::<Vec<_>>()
|
||||||
} else {
|
.join(", ")
|
||||||
format!(" [values: {}]", pv.join(", "))
|
)
|
||||||
});
|
} else {
|
||||||
|
format!(" [values: {}]", pv.join(", "))
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
spec_vals.join(" ")
|
spec_vals.join(" ")
|
||||||
|
@ -581,12 +624,11 @@ impl<'a> Help<'a> {
|
||||||
self.longest = 2;
|
self.longest = 2;
|
||||||
let mut ord_m = VecMap::new();
|
let mut ord_m = VecMap::new();
|
||||||
for sc in parser
|
for sc in parser
|
||||||
.subcommands
|
.subcommands
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|s| !s.p.is_set(AppSettings::Hidden)) {
|
.filter(|s| !s.p.is_set(AppSettings::Hidden))
|
||||||
let btm = ord_m
|
{
|
||||||
.entry(sc.p.meta.disp_ord)
|
let btm = ord_m.entry(sc.p.meta.disp_ord).or_insert(BTreeMap::new());
|
||||||
.or_insert(BTreeMap::new());
|
|
||||||
self.longest = cmp::max(self.longest, str_width(sc.p.meta.name.as_str()));
|
self.longest = cmp::max(self.longest, str_width(sc.p.meta.name.as_str()));
|
||||||
//self.longest = cmp::max(self.longest, sc.p.meta.name.len());
|
//self.longest = cmp::max(self.longest, sc.p.meta.name.len());
|
||||||
btm.insert(sc.p.meta.name.clone(), sc.clone());
|
btm.insert(sc.p.meta.name.clone(), sc.clone());
|
||||||
|
@ -679,10 +721,12 @@ impl<'a> Help<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
color!(self, "\nUSAGE:", warning)?;
|
color!(self, "\nUSAGE:", warning)?;
|
||||||
write!(self.writer,
|
write!(
|
||||||
"\n{}{}\n\n",
|
self.writer,
|
||||||
TAB,
|
"\n{}{}\n\n",
|
||||||
usage::create_usage_no_title(parser, &[]))?;
|
TAB,
|
||||||
|
usage::create_usage_no_title(parser, &[])
|
||||||
|
)?;
|
||||||
|
|
||||||
let flags = parser.has_flags();
|
let flags = parser.has_flags();
|
||||||
let pos = parser.has_positionals();
|
let pos = parser.has_positionals();
|
||||||
|
@ -748,21 +792,20 @@ fn copy_until<R: Read, W: Write>(r: &mut R, w: &mut W, delimiter_byte: u8) -> Co
|
||||||
/// - `None`: The reader was consumed.
|
/// - `None`: The reader was consumed.
|
||||||
/// - `Some(Ok(0))`: No tag was captured but the reader still contains data.
|
/// - `Some(Ok(0))`: No tag was captured but the reader still contains data.
|
||||||
/// - `Some(Ok(length>0))`: a tag with `length` was captured to the `tag_buffer`.
|
/// - `Some(Ok(length>0))`: a tag with `length` was captured to the `tag_buffer`.
|
||||||
fn copy_and_capture<R: Read, W: Write>(r: &mut R,
|
fn copy_and_capture<R: Read, W: Write>(
|
||||||
w: &mut W,
|
r: &mut R,
|
||||||
tag_buffer: &mut Cursor<Vec<u8>>)
|
w: &mut W,
|
||||||
-> Option<io::Result<usize>> {
|
tag_buffer: &mut Cursor<Vec<u8>>,
|
||||||
|
) -> Option<io::Result<usize>> {
|
||||||
use self::CopyUntilResult::*;
|
use self::CopyUntilResult::*;
|
||||||
debugln!("copy_and_capture;");
|
debugln!("copy_and_capture;");
|
||||||
|
|
||||||
// Find the opening byte.
|
// Find the opening byte.
|
||||||
match copy_until(r, w, b'{') {
|
match copy_until(r, w, b'{') {
|
||||||
|
|
||||||
// The end of the reader was reached without finding the opening tag.
|
// The end of the reader was reached without finding the opening tag.
|
||||||
// (either with or without having copied data to the writer)
|
// (either with or without having copied data to the writer)
|
||||||
// Return None indicating that we are done.
|
// Return None indicating that we are done.
|
||||||
ReaderEmpty |
|
ReaderEmpty | DelimiterNotFound(_) => None,
|
||||||
DelimiterNotFound(_) => None,
|
|
||||||
|
|
||||||
// Something went wrong.
|
// Something went wrong.
|
||||||
ReadError(e) | WriteError(e) => Some(Err(e)),
|
ReadError(e) | WriteError(e) => Some(Err(e)),
|
||||||
|
@ -770,7 +813,6 @@ fn copy_and_capture<R: Read, W: Write>(r: &mut R,
|
||||||
// The opening byte was found.
|
// The opening byte was found.
|
||||||
// (either with or without having copied data to the writer)
|
// (either with or without having copied data to the writer)
|
||||||
DelimiterFound(_) => {
|
DelimiterFound(_) => {
|
||||||
|
|
||||||
// Lets reset the buffer first and find out how long it is.
|
// Lets reset the buffer first and find out how long it is.
|
||||||
tag_buffer.set_position(0);
|
tag_buffer.set_position(0);
|
||||||
let buffer_size = tag_buffer.get_ref().len();
|
let buffer_size = tag_buffer.get_ref().len();
|
||||||
|
@ -778,7 +820,6 @@ fn copy_and_capture<R: Read, W: Write>(r: &mut R,
|
||||||
// Find the closing byte,limiting the reader to the length of the buffer.
|
// Find the closing byte,limiting the reader to the length of the buffer.
|
||||||
let mut rb = r.take(buffer_size as u64);
|
let mut rb = r.take(buffer_size as u64);
|
||||||
match copy_until(&mut rb, tag_buffer, b'}') {
|
match copy_until(&mut rb, tag_buffer, b'}') {
|
||||||
|
|
||||||
// We were already at the end of the reader.
|
// We were already at the end of the reader.
|
||||||
// Return None indicating that we are done.
|
// Return None indicating that we are done.
|
||||||
ReaderEmpty => None,
|
ReaderEmpty => None,
|
||||||
|
@ -790,17 +831,13 @@ fn copy_and_capture<R: Read, W: Write>(r: &mut R,
|
||||||
// The end of the reader was found without finding the closing tag.
|
// The end of the reader was found without finding the closing tag.
|
||||||
// Write the opening byte and captured text to the writer.
|
// Write the opening byte and captured text to the writer.
|
||||||
// Return 0 indicating that nothing was caputred but the reader still contains data.
|
// Return 0 indicating that nothing was caputred but the reader still contains data.
|
||||||
DelimiterNotFound(not_tag_length) => {
|
DelimiterNotFound(not_tag_length) => match w.write(b"{") {
|
||||||
match w.write(b"{") {
|
Err(e) => Some(Err(e)),
|
||||||
|
_ => match w.write(&tag_buffer.get_ref()[0..not_tag_length]) {
|
||||||
Err(e) => Some(Err(e)),
|
Err(e) => Some(Err(e)),
|
||||||
_ => {
|
_ => Some(Ok(0)),
|
||||||
match w.write(&tag_buffer.get_ref()[0..not_tag_length]) {
|
},
|
||||||
Err(e) => Some(Err(e)),
|
},
|
||||||
_ => Some(Ok(0)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ReadError(e) | WriteError(e) => Some(Err(e)),
|
ReadError(e) | WriteError(e) => Some(Err(e)),
|
||||||
}
|
}
|
||||||
|
@ -850,11 +887,14 @@ impl<'a> Help<'a> {
|
||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
debugln!(
|
debugln!("Help::write_template_help:iter: tag_buf={};", unsafe {
|
||||||
"Help::write_template_help:iter: tag_buf={};",
|
String::from_utf8_unchecked(
|
||||||
str::from_utf8(&tag_buf.get_ref()[..tag_length])
|
tag_buf.get_ref()[0..tag_length]
|
||||||
.unwrap_or_default()
|
.iter()
|
||||||
);
|
.map(|&i| i)
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
});
|
||||||
match &tag_buf.get_ref()[0..tag_length] {
|
match &tag_buf.get_ref()[0..tag_length] {
|
||||||
b"?" => {
|
b"?" => {
|
||||||
self.writer.write_all(b"Could not decode tag name")?;
|
self.writer.write_all(b"Could not decode tag name")?;
|
||||||
|
@ -863,24 +903,32 @@ impl<'a> Help<'a> {
|
||||||
self.write_bin_name(parser)?;
|
self.write_bin_name(parser)?;
|
||||||
}
|
}
|
||||||
b"version" => {
|
b"version" => {
|
||||||
write!(self.writer,
|
write!(
|
||||||
"{}",
|
self.writer,
|
||||||
parser.meta.version.unwrap_or("unknown version"))?;
|
"{}",
|
||||||
|
parser.meta.version.unwrap_or("unknown version")
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
b"author" => {
|
b"author" => {
|
||||||
write!(self.writer,
|
write!(
|
||||||
"{}",
|
self.writer,
|
||||||
parser.meta.author.unwrap_or("unknown author"))?;
|
"{}",
|
||||||
|
parser.meta.author.unwrap_or("unknown author")
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
b"about" => {
|
b"about" => {
|
||||||
write!(self.writer,
|
write!(
|
||||||
"{}",
|
self.writer,
|
||||||
parser.meta.about.unwrap_or("unknown about"))?;
|
"{}",
|
||||||
|
parser.meta.about.unwrap_or("unknown about")
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
b"long-about" => {
|
b"long-about" => {
|
||||||
write!(self.writer,
|
write!(
|
||||||
"{}",
|
self.writer,
|
||||||
parser.meta.long_about.unwrap_or("unknown about"))?;
|
"{}",
|
||||||
|
parser.meta.long_about.unwrap_or("unknown about")
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
b"usage" => {
|
b"usage" => {
|
||||||
write!(self.writer, "{}", usage::create_usage_no_title(parser, &[]))?;
|
write!(self.writer, "{}", usage::create_usage_no_title(parser, &[]))?;
|
||||||
|
@ -908,14 +956,18 @@ impl<'a> Help<'a> {
|
||||||
self.write_subcommands(parser)?;
|
self.write_subcommands(parser)?;
|
||||||
}
|
}
|
||||||
b"after-help" => {
|
b"after-help" => {
|
||||||
write!(self.writer,
|
write!(
|
||||||
"{}",
|
self.writer,
|
||||||
parser.meta.more_help.unwrap_or("unknown after-help"))?;
|
"{}",
|
||||||
|
parser.meta.more_help.unwrap_or("unknown after-help")
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
b"before-help" => {
|
b"before-help" => {
|
||||||
write!(self.writer,
|
write!(
|
||||||
"{}",
|
self.writer,
|
||||||
parser.meta.pre_help.unwrap_or("unknown before-help"))?;
|
"{}",
|
||||||
|
parser.meta.pre_help.unwrap_or("unknown before-help")
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
// Unknown tag, write it back.
|
// Unknown tag, write it back.
|
||||||
r => {
|
r => {
|
||||||
|
|
|
@ -1796,6 +1796,7 @@ impl<'n, 'e> AnyArg<'n, 'e> for App<'n, 'e> {
|
||||||
fn default_vals_ifs(&self) -> Option<map::Values<(&'n str, Option<&'e OsStr>, &'e OsStr)>> {
|
fn default_vals_ifs(&self) -> Option<map::Values<(&'n str, Option<&'e OsStr>, &'e OsStr)>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
fn env<'s>(&'s self) -> Option<(&'n OsStr, &'s OsString)> { None }
|
||||||
fn longest_filter(&self) -> bool { true }
|
fn longest_filter(&self) -> bool { true }
|
||||||
fn aliases(&self) -> Option<Vec<&'e str>> {
|
fn aliases(&self) -> Option<Vec<&'e str>> {
|
||||||
if let Some(ref aliases) = self.p.meta.aliases {
|
if let Some(ref aliases) = self.p.meta.aliases {
|
||||||
|
|
|
@ -1788,6 +1788,38 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_env(&mut self, matcher: &mut ArgMatcher<'a>) -> ClapResult<()> {
|
||||||
|
macro_rules! add_val {
|
||||||
|
($_self:ident, $a:ident, $m:ident) => {
|
||||||
|
if let Some(ref val) = $a.v.env {
|
||||||
|
if $m.get($a.b.name).map(|ma| ma.vals.len()).map(|len| len == 0).unwrap_or(false) {
|
||||||
|
$_self.add_val_to_arg($a, OsStr::new(&val.1), $m)?;
|
||||||
|
|
||||||
|
if $_self.cache.map_or(true, |name| name != $a.name()) {
|
||||||
|
arg_post_processing!($_self, $a, $m);
|
||||||
|
$_self.cache = Some($a.name());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$_self.add_val_to_arg($a, OsStr::new(&val.1), $m)?;
|
||||||
|
|
||||||
|
if $_self.cache.map_or(true, |name| name != $a.name()) {
|
||||||
|
arg_post_processing!($_self, $a, $m);
|
||||||
|
$_self.cache = Some($a.name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
for o in &self.opts {
|
||||||
|
add_val!(self, o, matcher);
|
||||||
|
}
|
||||||
|
for p in self.positionals.values() {
|
||||||
|
add_val!(self, p, matcher);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn flags(&self) -> Iter<FlagBuilder<'a, 'b>> { self.flags.iter() }
|
pub fn flags(&self) -> Iter<FlagBuilder<'a, 'b>> { self.flags.iter() }
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ impl<'a, 'b, 'z> Validator<'a, 'b, 'z> {
|
||||||
-> ClapResult<()> {
|
-> ClapResult<()> {
|
||||||
debugln!("Validator::validate;");
|
debugln!("Validator::validate;");
|
||||||
let mut reqs_validated = false;
|
let mut reqs_validated = false;
|
||||||
|
self.0.add_env(matcher)?;
|
||||||
self.0.add_defaults(matcher)?;
|
self.0.add_defaults(matcher)?;
|
||||||
if let ParseResult::Opt(a) = needs_val_of {
|
if let ParseResult::Opt(a) = needs_val_of {
|
||||||
debugln!("Validator::validate: needs_val_of={:?}", a);
|
debugln!("Validator::validate: needs_val_of={:?}", a);
|
||||||
|
|
|
@ -33,6 +33,7 @@ pub trait AnyArg<'n, 'e>: std_fmt::Display {
|
||||||
fn long_help(&self) -> Option<&'e str>;
|
fn long_help(&self) -> Option<&'e str>;
|
||||||
fn default_val(&self) -> Option<&'e OsStr>;
|
fn default_val(&self) -> Option<&'e OsStr>;
|
||||||
fn default_vals_ifs(&self) -> Option<map::Values<(&'n str, Option<&'e OsStr>, &'e OsStr)>>;
|
fn default_vals_ifs(&self) -> Option<map::Values<(&'n str, Option<&'e OsStr>, &'e OsStr)>>;
|
||||||
|
fn env<'s>(&'s self) -> Option<(&'n OsStr, &'s OsString)>;
|
||||||
fn longest_filter(&self) -> bool;
|
fn longest_filter(&self) -> bool;
|
||||||
fn val_terminator(&self) -> Option<&'e str>;
|
fn val_terminator(&self) -> Option<&'e str>;
|
||||||
}
|
}
|
||||||
|
|
118
src/args/arg.rs
118
src/args/arg.rs
|
@ -6,7 +6,7 @@ use std::ffi::{OsString, OsStr};
|
||||||
use osstringext::OsStrExt3;
|
use osstringext::OsStrExt3;
|
||||||
#[cfg(not(target_os="windows"))]
|
#[cfg(not(target_os="windows"))]
|
||||||
use std::os::unix::ffi::OsStrExt;
|
use std::os::unix::ffi::OsStrExt;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
#[cfg(feature = "yaml")]
|
#[cfg(feature = "yaml")]
|
||||||
use yaml_rust::Yaml;
|
use yaml_rust::Yaml;
|
||||||
|
@ -128,6 +128,7 @@ impl<'a, 'b> Arg<'a, 'b> {
|
||||||
"default_value" => yaml_to_str!(a, v, default_value),
|
"default_value" => yaml_to_str!(a, v, default_value),
|
||||||
"default_value_if" => yaml_tuple3!(a, v, default_value_if),
|
"default_value_if" => yaml_tuple3!(a, v, default_value_if),
|
||||||
"default_value_ifs" => yaml_tuple3!(a, v, default_value_if),
|
"default_value_ifs" => yaml_tuple3!(a, v, default_value_if),
|
||||||
|
"env" => yaml_to_str!(a, v, env),
|
||||||
"value_names" => yaml_vec_or_str!(v, a, value_name),
|
"value_names" => yaml_vec_or_str!(v, a, value_name),
|
||||||
"groups" => yaml_vec_or_str!(v, a, group),
|
"groups" => yaml_vec_or_str!(v, a, group),
|
||||||
"requires" => yaml_vec_or_str!(v, a, requires),
|
"requires" => yaml_vec_or_str!(v, a, requires),
|
||||||
|
@ -3014,7 +3015,7 @@ impl<'a, 'b> Arg<'a, 'b> {
|
||||||
/// **NOTE:** If the user *does not* use this argument at runtime [`ArgMatches::is_present`] will
|
/// **NOTE:** If the user *does not* use this argument at runtime [`ArgMatches::is_present`] will
|
||||||
/// still return `true`. If you wish to determine whether the argument was used at runtime or
|
/// still return `true`. If you wish to determine whether the argument was used at runtime or
|
||||||
/// not, consider [`ArgMatches::occurrences_of`] which will return `0` if the argument was *not*
|
/// not, consider [`ArgMatches::occurrences_of`] which will return `0` if the argument was *not*
|
||||||
/// used at runtmie.
|
/// used at runtime.
|
||||||
///
|
///
|
||||||
/// **NOTE:** This setting is perfectly compatible with [`Arg::default_value_if`] but slightly
|
/// **NOTE:** This setting is perfectly compatible with [`Arg::default_value_if`] but slightly
|
||||||
/// different. `Arg::default_value` *only* takes affect when the user has not provided this arg
|
/// different. `Arg::default_value` *only* takes affect when the user has not provided this arg
|
||||||
|
@ -3311,6 +3312,119 @@ impl<'a, 'b> Arg<'a, 'b> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Specifies that if the value is not passed in as an argument, that it should be retrieved
|
||||||
|
/// from the environment, if available. If it is not present in the environment, then default
|
||||||
|
/// rules will apply.
|
||||||
|
///
|
||||||
|
/// **NOTE:** If the user *does not* use this argument at runtime, [`ArgMatches::occurrences_of`]
|
||||||
|
/// will return `0` even though the [`ArgMatches::value_of`] will return the default specified.
|
||||||
|
///
|
||||||
|
/// **NOTE:** If the user *does not* use this argument at runtime [`ArgMatches::is_present`] will
|
||||||
|
/// return `true` if the variable is present in the environemnt . If you wish to determine whether
|
||||||
|
/// the argument was used at runtime or not, consider [`ArgMatches::occurrences_of`] which will
|
||||||
|
/// return `0` if the argument was *not* used at runtime.
|
||||||
|
///
|
||||||
|
/// **NOTE:** This implicitly sets [`Arg::takes_value(true)`].
|
||||||
|
///
|
||||||
|
/// **NOTE:** If [`Arg::multiple(true)`] is set then [`Arg::use_delimiter(true)`] should also be
|
||||||
|
/// set. Otherwise, only a single argument will be returned from the environment variable. The
|
||||||
|
/// default delimiter is `,` and follows all the other delimiter rules.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// In this example, we show the variable coming from the environment:
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use std::env;
|
||||||
|
/// # use clap::{App, Arg};
|
||||||
|
///
|
||||||
|
/// env::set_var("MY_FLAG", "env");
|
||||||
|
///
|
||||||
|
/// let m = App::new("prog")
|
||||||
|
/// .arg(Arg::with_name("flag")
|
||||||
|
/// .long("flag")
|
||||||
|
/// .env("MY_FLAG"))
|
||||||
|
/// .get_matches_from(vec![
|
||||||
|
/// "prog"
|
||||||
|
/// ]);
|
||||||
|
///
|
||||||
|
/// assert_eq!(m.value_of("flag"), Some("env"));
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// In this example, we show the variable coming from an option on the CLI:
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use std::env;
|
||||||
|
/// # use clap::{App, Arg};
|
||||||
|
///
|
||||||
|
/// env::set_var("MY_FLAG", "env");
|
||||||
|
///
|
||||||
|
/// let m = App::new("prog")
|
||||||
|
/// .arg(Arg::with_name("flag")
|
||||||
|
/// .long("flag")
|
||||||
|
/// .env("MY_FLAG"))
|
||||||
|
/// .get_matches_from(vec![
|
||||||
|
/// "prog", "--flag", "opt"
|
||||||
|
/// ]);
|
||||||
|
///
|
||||||
|
/// assert_eq!(m.value_of("flag"), Some("opt"));
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// In this example, we show the variable coming from the environment even with the
|
||||||
|
/// presence of a default:
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use std::env;
|
||||||
|
/// # use clap::{App, Arg};
|
||||||
|
///
|
||||||
|
/// env::set_var("MY_FLAG", "env");
|
||||||
|
///
|
||||||
|
/// let m = App::new("prog")
|
||||||
|
/// .arg(Arg::with_name("flag")
|
||||||
|
/// .long("flag")
|
||||||
|
/// .env("MY_FLAG")
|
||||||
|
/// .default_value("default"))
|
||||||
|
/// .get_matches_from(vec![
|
||||||
|
/// "prog"
|
||||||
|
/// ]);
|
||||||
|
///
|
||||||
|
/// assert_eq!(m.value_of("flag"), Some("env"));
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// In this example, we show the use of multiple values in a single environment variable:
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use std::env;
|
||||||
|
/// # use clap::{App, Arg};
|
||||||
|
///
|
||||||
|
/// env::set_var("MY_FLAG_MULTI", "env1,env2");
|
||||||
|
///
|
||||||
|
/// let m = App::new("prog")
|
||||||
|
/// .arg(Arg::with_name("flag")
|
||||||
|
/// .long("flag")
|
||||||
|
/// .env("MY_FLAG_MULTI")
|
||||||
|
/// .multiple(true)
|
||||||
|
/// .use_delimiter(true))
|
||||||
|
/// .get_matches_from(vec![
|
||||||
|
/// "prog"
|
||||||
|
/// ]);
|
||||||
|
///
|
||||||
|
/// assert_eq!(m.values_of("flag").unwrap().collect::<Vec<_>>(), vec!["env1", "env2"]);
|
||||||
|
/// ```
|
||||||
|
pub fn env(self, name: &'a str) -> Self {
|
||||||
|
self.env_os(OsStr::new(name))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Specifies that if the value is not passed in as an argument, that it should be retrieved
|
||||||
|
/// from the environment if available in the exact same manner as [`Arg::env`] only using
|
||||||
|
/// [`OsStr`]s instead.
|
||||||
|
pub fn env_os(mut self, name: &'a OsStr) -> Self {
|
||||||
|
self.setb(ArgSettings::TakesValue);
|
||||||
|
|
||||||
|
self.v.env = env::var_os(name).map(|value| (name, value));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// When set to `true` the help string will be displayed on the line after the argument and
|
/// When set to `true` the help string will be displayed on the line after the argument and
|
||||||
/// indented once. This can be helpful for arguments with very long or complex help messages.
|
/// indented once. This can be helpful for arguments with very long or complex help messages.
|
||||||
/// This can also be helpful for arguments with very long flag names, or many/long value names.
|
/// This can also be helpful for arguments with very long flag names, or many/long value names.
|
||||||
|
|
|
@ -8,20 +8,26 @@ use std::mem;
|
||||||
|
|
||||||
// Internal
|
// Internal
|
||||||
use Arg;
|
use Arg;
|
||||||
use args::{ArgSettings, Base, Switched, AnyArg, DispOrder};
|
use args::{AnyArg, ArgSettings, Base, DispOrder, Switched};
|
||||||
use map::{self, VecMap};
|
use map::{self, VecMap};
|
||||||
|
|
||||||
#[derive(Default, Clone, Debug)]
|
#[derive(Default, Clone, Debug)]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct FlagBuilder<'n, 'e>
|
pub struct FlagBuilder<'n, 'e>
|
||||||
where 'n: 'e
|
where
|
||||||
|
'n: 'e,
|
||||||
{
|
{
|
||||||
pub b: Base<'n, 'e>,
|
pub b: Base<'n, 'e>,
|
||||||
pub s: Switched<'e>,
|
pub s: Switched<'e>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'n, 'e> FlagBuilder<'n, 'e> {
|
impl<'n, 'e> FlagBuilder<'n, 'e> {
|
||||||
pub fn new(name: &'n str) -> Self { FlagBuilder { b: Base::new(name), ..Default::default() } }
|
pub fn new(name: &'n str) -> Self {
|
||||||
|
FlagBuilder {
|
||||||
|
b: Base::new(name),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for FlagBuilder<'a, 'b> {
|
impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for FlagBuilder<'a, 'b> {
|
||||||
|
@ -83,10 +89,12 @@ impl<'n, 'e> AnyArg<'n, 'e> for FlagBuilder<'n, 'e> {
|
||||||
fn default_vals_ifs(&self) -> Option<map::Values<(&'n str, Option<&'e OsStr>, &'e OsStr)>> {
|
fn default_vals_ifs(&self) -> Option<map::Values<(&'n str, Option<&'e OsStr>, &'e OsStr)>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
fn env<'s>(&'s self) -> Option<(&'n OsStr, &'s OsString)> { None }
|
||||||
fn longest_filter(&self) -> bool { self.s.long.is_some() }
|
fn longest_filter(&self) -> bool { self.s.long.is_some() }
|
||||||
fn aliases(&self) -> Option<Vec<&'e str>> {
|
fn aliases(&self) -> Option<Vec<&'e str>> {
|
||||||
if let Some(ref aliases) = self.s.aliases {
|
if let Some(ref aliases) = self.s.aliases {
|
||||||
let vis_aliases: Vec<_> = aliases.iter()
|
let vis_aliases: Vec<_> = aliases
|
||||||
|
.iter()
|
||||||
.filter_map(|&(n, v)| if v { Some(n) } else { None })
|
.filter_map(|&(n, v)| if v { Some(n) } else { None })
|
||||||
.collect();
|
.collect();
|
||||||
if vis_aliases.is_empty() {
|
if vis_aliases.is_empty() {
|
||||||
|
@ -105,9 +113,7 @@ impl<'n, 'e> DispOrder for FlagBuilder<'n, 'e> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'n, 'e> PartialEq for FlagBuilder<'n, 'e> {
|
impl<'n, 'e> PartialEq for FlagBuilder<'n, 'e> {
|
||||||
fn eq(&self, other: &FlagBuilder<'n, 'e>) -> bool {
|
fn eq(&self, other: &FlagBuilder<'n, 'e>) -> bool { self.b == other.b }
|
||||||
self.b == other.b
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -142,8 +148,12 @@ mod test {
|
||||||
fn flagbuilder_display_multiple_aliases() {
|
fn flagbuilder_display_multiple_aliases() {
|
||||||
let mut f = FlagBuilder::new("flg");
|
let mut f = FlagBuilder::new("flg");
|
||||||
f.s.short = Some('f');
|
f.s.short = Some('f');
|
||||||
f.s.aliases =
|
f.s.aliases = Some(vec![
|
||||||
Some(vec![("alias_not_visible", false), ("f2", true), ("f3", true), ("f4", true)]);
|
("alias_not_visible", false),
|
||||||
|
("f2", true),
|
||||||
|
("f3", true),
|
||||||
|
("f4", true),
|
||||||
|
]);
|
||||||
assert_eq!(&*format!("{}", f), "-f");
|
assert_eq!(&*format!("{}", f), "-f");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,15 @@ use std::ffi::{OsStr, OsString};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
// Internal
|
// Internal
|
||||||
use args::{ArgSettings, AnyArg, Base, Switched, Valued, Arg, DispOrder};
|
use args::{AnyArg, Arg, ArgSettings, Base, DispOrder, Switched, Valued};
|
||||||
use map::{self, VecMap};
|
use map::{self, VecMap};
|
||||||
|
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[derive(Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
pub struct OptBuilder<'n, 'e>
|
pub struct OptBuilder<'n, 'e>
|
||||||
where 'n: 'e
|
where
|
||||||
|
'n: 'e,
|
||||||
{
|
{
|
||||||
pub b: Base<'n, 'e>,
|
pub b: Base<'n, 'e>,
|
||||||
pub s: Switched<'e>,
|
pub s: Switched<'e>,
|
||||||
|
@ -21,7 +22,12 @@ pub struct OptBuilder<'n, 'e>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'n, 'e> OptBuilder<'n, 'e> {
|
impl<'n, 'e> OptBuilder<'n, 'e> {
|
||||||
pub fn new(name: &'n str) -> Self { OptBuilder { b: Base::new(name), ..Default::default() } }
|
pub fn new(name: &'n str) -> Self {
|
||||||
|
OptBuilder {
|
||||||
|
b: Base::new(name),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'n, 'e, 'z> From<&'z Arg<'n, 'e>> for OptBuilder<'n, 'e> {
|
impl<'n, 'e, 'z> From<&'z Arg<'n, 'e>> for OptBuilder<'n, 'e> {
|
||||||
|
@ -85,14 +91,16 @@ impl<'n, 'e> Display for OptBuilder<'n, 'e> {
|
||||||
write!(f, "...")?;
|
write!(f, "...")?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
write!(f,
|
write!(
|
||||||
|
f,
|
||||||
"<{}>{}",
|
"<{}>{}",
|
||||||
self.b.name,
|
self.b.name,
|
||||||
if self.is_set(ArgSettings::Multiple) {
|
if self.is_set(ArgSettings::Multiple) {
|
||||||
"..."
|
"..."
|
||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
})?;
|
}
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -132,10 +140,14 @@ impl<'n, 'e> AnyArg<'n, 'e> for OptBuilder<'n, 'e> {
|
||||||
fn default_vals_ifs(&self) -> Option<map::Values<(&'n str, Option<&'e OsStr>, &'e OsStr)>> {
|
fn default_vals_ifs(&self) -> Option<map::Values<(&'n str, Option<&'e OsStr>, &'e OsStr)>> {
|
||||||
self.v.default_vals_ifs.as_ref().map(|vm| vm.values())
|
self.v.default_vals_ifs.as_ref().map(|vm| vm.values())
|
||||||
}
|
}
|
||||||
|
fn env<'s>(&'s self) -> Option<(&'n OsStr, &'s OsString)> {
|
||||||
|
self.v.env.as_ref().map(|&(key, ref value)| (key, value))
|
||||||
|
}
|
||||||
fn longest_filter(&self) -> bool { true }
|
fn longest_filter(&self) -> bool { true }
|
||||||
fn aliases(&self) -> Option<Vec<&'e str>> {
|
fn aliases(&self) -> Option<Vec<&'e str>> {
|
||||||
if let Some(ref aliases) = self.s.aliases {
|
if let Some(ref aliases) = self.s.aliases {
|
||||||
let vis_aliases: Vec<_> = aliases.iter()
|
let vis_aliases: Vec<_> = aliases
|
||||||
|
.iter()
|
||||||
.filter_map(|&(n, v)| if v { Some(n) } else { None })
|
.filter_map(|&(n, v)| if v { Some(n) } else { None })
|
||||||
.collect();
|
.collect();
|
||||||
if vis_aliases.is_empty() {
|
if vis_aliases.is_empty() {
|
||||||
|
@ -154,9 +166,7 @@ impl<'n, 'e> DispOrder for OptBuilder<'n, 'e> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'n, 'e> PartialEq for OptBuilder<'n, 'e> {
|
impl<'n, 'e> PartialEq for OptBuilder<'n, 'e> {
|
||||||
fn eq(&self, other: &OptBuilder<'n, 'e>) -> bool {
|
fn eq(&self, other: &OptBuilder<'n, 'e>) -> bool { self.b == other.b }
|
||||||
self.b == other.b
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -214,8 +224,12 @@ mod test {
|
||||||
fn optbuilder_display_multiple_aliases() {
|
fn optbuilder_display_multiple_aliases() {
|
||||||
let mut o = OptBuilder::new("opt");
|
let mut o = OptBuilder::new("opt");
|
||||||
o.s.long = Some("option");
|
o.s.long = Some("option");
|
||||||
o.s.aliases =
|
o.s.aliases = Some(vec![
|
||||||
Some(vec![("als_not_visible", false), ("als2", true), ("als3", true), ("als4", true)]);
|
("als_not_visible", false),
|
||||||
|
("als2", true),
|
||||||
|
("als3", true),
|
||||||
|
("als4", true),
|
||||||
|
]);
|
||||||
assert_eq!(&*format!("{}", o), "--option <opt>");
|
assert_eq!(&*format!("{}", o), "--option <opt>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ use std::mem;
|
||||||
|
|
||||||
// Internal
|
// Internal
|
||||||
use Arg;
|
use Arg;
|
||||||
use args::{ArgSettings, Base, Valued, AnyArg, DispOrder};
|
use args::{AnyArg, ArgSettings, Base, DispOrder, Valued};
|
||||||
use INTERNAL_ERROR_MSG;
|
use INTERNAL_ERROR_MSG;
|
||||||
use map::{self, VecMap};
|
use map::{self, VecMap};
|
||||||
|
|
||||||
|
@ -16,7 +16,8 @@ use map::{self, VecMap};
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
pub struct PosBuilder<'n, 'e>
|
pub struct PosBuilder<'n, 'e>
|
||||||
where 'n: 'e
|
where
|
||||||
|
'n: 'e,
|
||||||
{
|
{
|
||||||
pub b: Base<'n, 'e>,
|
pub b: Base<'n, 'e>,
|
||||||
pub v: Valued<'n, 'e>,
|
pub v: Valued<'n, 'e>,
|
||||||
|
@ -39,7 +40,8 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
|
||||||
index: idx,
|
index: idx,
|
||||||
};
|
};
|
||||||
if a.v.max_vals.is_some() || a.v.min_vals.is_some() ||
|
if a.v.max_vals.is_some() || a.v.min_vals.is_some() ||
|
||||||
(a.v.num_vals.is_some() && a.v.num_vals.unwrap() > 1) {
|
(a.v.num_vals.is_some() && a.v.num_vals.unwrap() > 1)
|
||||||
|
{
|
||||||
pb.b.settings.set(ArgSettings::Multiple);
|
pb.b.settings.set(ArgSettings::Multiple);
|
||||||
}
|
}
|
||||||
pb
|
pb
|
||||||
|
@ -47,7 +49,8 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
|
||||||
|
|
||||||
pub fn from_arg(mut a: Arg<'n, 'e>, idx: u64) -> Self {
|
pub fn from_arg(mut a: Arg<'n, 'e>, idx: u64) -> Self {
|
||||||
if a.v.max_vals.is_some() || a.v.min_vals.is_some() ||
|
if a.v.max_vals.is_some() || a.v.min_vals.is_some() ||
|
||||||
(a.v.num_vals.is_some() && a.v.num_vals.unwrap() > 1) {
|
(a.v.num_vals.is_some() && a.v.num_vals.unwrap() > 1)
|
||||||
|
{
|
||||||
a.b.settings.set(ArgSettings::Multiple);
|
a.b.settings.set(ArgSettings::Multiple);
|
||||||
}
|
}
|
||||||
PosBuilder {
|
PosBuilder {
|
||||||
|
@ -74,11 +77,13 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
|
||||||
if let Some(ref names) = self.v.val_names {
|
if let Some(ref names) = self.v.val_names {
|
||||||
debugln!("PosBuilder:name_no_brackets: val_names={:#?}", names);
|
debugln!("PosBuilder:name_no_brackets: val_names={:#?}", names);
|
||||||
if names.len() > 1 {
|
if names.len() > 1 {
|
||||||
Cow::Owned(names
|
Cow::Owned(
|
||||||
.values()
|
names
|
||||||
.map(|n| format!("<{}>", n))
|
.values()
|
||||||
.collect::<Vec<_>>()
|
.map(|n| format!("<{}>", n))
|
||||||
.join(" "))
|
.collect::<Vec<_>>()
|
||||||
|
.join(" "),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
Cow::Borrowed(names.values().next().expect(INTERNAL_ERROR_MSG))
|
Cow::Borrowed(names.values().next().expect(INTERNAL_ERROR_MSG))
|
||||||
}
|
}
|
||||||
|
@ -92,17 +97,21 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
|
||||||
impl<'n, 'e> Display for PosBuilder<'n, 'e> {
|
impl<'n, 'e> Display for PosBuilder<'n, 'e> {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||||
if let Some(ref names) = self.v.val_names {
|
if let Some(ref names) = self.v.val_names {
|
||||||
write!(f,
|
write!(
|
||||||
|
f,
|
||||||
"{}",
|
"{}",
|
||||||
names
|
names
|
||||||
.values()
|
.values()
|
||||||
.map(|n| format!("<{}>", n))
|
.map(|n| format!("<{}>", n))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(" "))?;
|
.join(" ")
|
||||||
|
)?;
|
||||||
} else {
|
} else {
|
||||||
write!(f, "<{}>", self.b.name)?;
|
write!(f, "<{}>", self.b.name)?;
|
||||||
}
|
}
|
||||||
if self.b.settings.is_set(ArgSettings::Multiple) && (self.v.val_names.is_none() || self.v.val_names.as_ref().unwrap().len() == 1) {
|
if self.b.settings.is_set(ArgSettings::Multiple) &&
|
||||||
|
(self.v.val_names.is_none() || self.v.val_names.as_ref().unwrap().len() == 1)
|
||||||
|
{
|
||||||
write!(f, "...")?;
|
write!(f, "...")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,6 +152,12 @@ impl<'n, 'e> AnyArg<'n, 'e> for PosBuilder<'n, 'e> {
|
||||||
self.v.default_vals_ifs.as_ref().map(|vm| vm.values())
|
self.v.default_vals_ifs.as_ref().map(|vm| vm.values())
|
||||||
}
|
}
|
||||||
fn default_val(&self) -> Option<&'e OsStr> { self.v.default_val }
|
fn default_val(&self) -> Option<&'e OsStr> { self.v.default_val }
|
||||||
|
fn env<'s>(&'s self) -> Option<(&'n OsStr, &'s OsString)> {
|
||||||
|
self.v
|
||||||
|
.env
|
||||||
|
.as_ref()
|
||||||
|
.map(|&(key, ref value)| (key, value))
|
||||||
|
}
|
||||||
fn longest_filter(&self) -> bool { true }
|
fn longest_filter(&self) -> bool { true }
|
||||||
fn aliases(&self) -> Option<Vec<&'e str>> { None }
|
fn aliases(&self) -> Option<Vec<&'e str>> { None }
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,8 @@ use Arg;
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Valued<'a, 'b>
|
pub struct Valued<'a, 'b>
|
||||||
where 'a: 'b
|
where
|
||||||
|
'a: 'b,
|
||||||
{
|
{
|
||||||
pub possible_vals: Option<Vec<&'b str>>,
|
pub possible_vals: Option<Vec<&'b str>>,
|
||||||
pub val_names: Option<VecMap<&'b str>>,
|
pub val_names: Option<VecMap<&'b str>>,
|
||||||
|
@ -20,6 +21,7 @@ pub struct Valued<'a, 'b>
|
||||||
pub val_delim: Option<char>,
|
pub val_delim: Option<char>,
|
||||||
pub default_val: Option<&'b OsStr>,
|
pub default_val: Option<&'b OsStr>,
|
||||||
pub default_vals_ifs: Option<VecMap<(&'a str, Option<&'b OsStr>, &'b OsStr)>>,
|
pub default_vals_ifs: Option<VecMap<(&'a str, Option<&'b OsStr>, &'b OsStr)>>,
|
||||||
|
pub env: Option<(&'a OsStr, OsString)>,
|
||||||
pub terminator: Option<&'b str>,
|
pub terminator: Option<&'b str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +38,7 @@ impl<'n, 'e> Default for Valued<'n, 'e> {
|
||||||
val_delim: None,
|
val_delim: None,
|
||||||
default_val: None,
|
default_val: None,
|
||||||
default_vals_ifs: None,
|
default_vals_ifs: None,
|
||||||
|
env: None,
|
||||||
terminator: None,
|
terminator: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,9 @@ use clap::{App, Arg, ErrorKind};
|
||||||
#[test]
|
#[test]
|
||||||
fn opts() {
|
fn opts() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("-o [opt] 'some opt'")
|
.arg(
|
||||||
.default_value("default"))
|
Arg::from_usage("-o [opt] 'some opt'").default_value("default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec![""]);
|
.get_matches_from_safe(vec![""]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -20,8 +21,9 @@ fn opts() {
|
||||||
#[test]
|
#[test]
|
||||||
fn opt_user_override() {
|
fn opt_user_override() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'")
|
.arg(
|
||||||
.default_value("default"))
|
Arg::from_usage("--opt [FILE] 'some arg'").default_value("default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "--opt", "value"]);
|
.get_matches_from_safe(vec!["", "--opt", "value"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -32,8 +34,7 @@ fn opt_user_override() {
|
||||||
#[test]
|
#[test]
|
||||||
fn positionals() {
|
fn positionals() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("[arg] 'some opt'")
|
.arg(Arg::from_usage("[arg] 'some opt'").default_value("default"))
|
||||||
.default_value("default"))
|
|
||||||
.get_matches_from_safe(vec![""]);
|
.get_matches_from_safe(vec![""]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -44,8 +45,7 @@ fn positionals() {
|
||||||
#[test]
|
#[test]
|
||||||
fn positional_user_override() {
|
fn positional_user_override() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(Arg::from_usage("[arg] 'some arg'").default_value("default"))
|
||||||
.default_value("default"))
|
|
||||||
.get_matches_from_safe(vec!["", "value"]);
|
.get_matches_from_safe(vec!["", "value"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -59,10 +59,11 @@ fn positional_user_override() {
|
||||||
fn osstr_opts() {
|
fn osstr_opts() {
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
let expected = OsStr::new("default");
|
let expected = OsStr::new("default");
|
||||||
|
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("-o [opt] 'some opt'")
|
.arg(
|
||||||
.default_value_os(expected))
|
Arg::from_usage("-o [opt] 'some opt'").default_value_os(expected),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec![""]);
|
.get_matches_from_safe(vec![""]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -76,8 +77,9 @@ fn osstr_opt_user_override() {
|
||||||
let default = OsStr::new("default");
|
let default = OsStr::new("default");
|
||||||
|
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'")
|
.arg(
|
||||||
.default_value_os(default))
|
Arg::from_usage("--opt [FILE] 'some arg'").default_value_os(default),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "--opt", "value"]);
|
.get_matches_from_safe(vec!["", "--opt", "value"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -91,8 +93,9 @@ fn osstr_positionals() {
|
||||||
let expected = OsStr::new("default");
|
let expected = OsStr::new("default");
|
||||||
|
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("[arg] 'some opt'")
|
.arg(
|
||||||
.default_value_os(expected))
|
Arg::from_usage("[arg] 'some opt'").default_value_os(expected),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec![""]);
|
.get_matches_from_safe(vec![""]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -106,8 +109,9 @@ fn osstr_positional_user_override() {
|
||||||
let default = OsStr::new("default");
|
let default = OsStr::new("default");
|
||||||
|
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value_os(default))
|
Arg::from_usage("[arg] 'some arg'").default_value_os(default),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "value"]);
|
.get_matches_from_safe(vec!["", "value"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -115,14 +119,17 @@ fn osstr_positional_user_override() {
|
||||||
assert_eq!(m.value_of("arg").unwrap(), "value");
|
assert_eq!(m.value_of("arg").unwrap(), "value");
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Default if arg is present
|
// --- Default if arg is present
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_no_default() {
|
fn default_if_arg_present_no_default() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(Arg::from_usage("[arg] 'some arg'").default_value_if(
|
||||||
.default_value_if("opt", None, "default"))
|
"opt",
|
||||||
|
None,
|
||||||
|
"default",
|
||||||
|
))
|
||||||
.get_matches_from_safe(vec!["", "--opt", "some"]);
|
.get_matches_from_safe(vec!["", "--opt", "some"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -133,9 +140,12 @@ fn default_if_arg_present_no_default() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_no_default_user_override() {
|
fn default_if_arg_present_no_default_user_override() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(Arg::from_usage("[arg] 'some arg'").default_value_if(
|
||||||
.default_value_if("opt", None, "default"))
|
"opt",
|
||||||
|
None,
|
||||||
|
"default",
|
||||||
|
))
|
||||||
.get_matches_from_safe(vec!["", "--opt", "some", "other"]);
|
.get_matches_from_safe(vec!["", "--opt", "some", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -146,10 +156,12 @@ fn default_if_arg_present_no_default_user_override() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_no_arg_with_default() {
|
fn default_if_arg_present_no_arg_with_default() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_if("opt", None, "default"))
|
.default_value("first")
|
||||||
|
.default_value_if("opt", None, "default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec![""]);
|
.get_matches_from_safe(vec![""]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -160,10 +172,12 @@ fn default_if_arg_present_no_arg_with_default() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_with_default() {
|
fn default_if_arg_present_with_default() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_if("opt", None, "default"))
|
.default_value("first")
|
||||||
|
.default_value_if("opt", None, "default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "--opt", "some"]);
|
.get_matches_from_safe(vec!["", "--opt", "some"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -174,10 +188,12 @@ fn default_if_arg_present_with_default() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_with_default_user_override() {
|
fn default_if_arg_present_with_default_user_override() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_if("opt", None, "default"))
|
.default_value("first")
|
||||||
|
.default_value_if("opt", None, "default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "--opt", "some", "other"]);
|
.get_matches_from_safe(vec!["", "--opt", "some", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -188,10 +204,12 @@ fn default_if_arg_present_with_default_user_override() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_no_arg_with_default_user_override() {
|
fn default_if_arg_present_no_arg_with_default_user_override() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_if("opt", None, "default"))
|
.default_value("first")
|
||||||
|
.default_value_if("opt", None, "default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "other"]);
|
.get_matches_from_safe(vec!["", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -204,9 +222,12 @@ fn default_if_arg_present_no_arg_with_default_user_override() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_with_value_no_default() {
|
fn default_if_arg_present_with_value_no_default() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(Arg::from_usage("[arg] 'some arg'").default_value_if(
|
||||||
.default_value_if("opt", Some("value"), "default"))
|
"opt",
|
||||||
|
Some("value"),
|
||||||
|
"default",
|
||||||
|
))
|
||||||
.get_matches_from_safe(vec!["", "--opt", "value"]);
|
.get_matches_from_safe(vec!["", "--opt", "value"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -217,9 +238,12 @@ fn default_if_arg_present_with_value_no_default() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_with_value_no_default_fail() {
|
fn default_if_arg_present_with_value_no_default_fail() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(Arg::from_usage("[arg] 'some arg'").default_value_if(
|
||||||
.default_value_if("opt", Some("value"), "default"))
|
"opt",
|
||||||
|
Some("value"),
|
||||||
|
"default",
|
||||||
|
))
|
||||||
.get_matches_from_safe(vec!["", "--opt", "other"]);
|
.get_matches_from_safe(vec!["", "--opt", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -230,9 +254,12 @@ fn default_if_arg_present_with_value_no_default_fail() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_with_value_no_default_user_override() {
|
fn default_if_arg_present_with_value_no_default_user_override() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(Arg::from_usage("[arg] 'some arg'").default_value_if(
|
||||||
.default_value_if("opt", Some("some"), "default"))
|
"opt",
|
||||||
|
Some("some"),
|
||||||
|
"default",
|
||||||
|
))
|
||||||
.get_matches_from_safe(vec!["", "--opt", "some", "other"]);
|
.get_matches_from_safe(vec!["", "--opt", "some", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -243,10 +270,12 @@ fn default_if_arg_present_with_value_no_default_user_override() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_with_value_no_arg_with_default() {
|
fn default_if_arg_present_with_value_no_arg_with_default() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_if("opt", Some("some"), "default"))
|
.default_value("first")
|
||||||
|
.default_value_if("opt", Some("some"), "default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec![""]);
|
.get_matches_from_safe(vec![""]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -257,10 +286,12 @@ fn default_if_arg_present_with_value_no_arg_with_default() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_with_value_no_arg_with_default_fail() {
|
fn default_if_arg_present_with_value_no_arg_with_default_fail() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_if("opt", Some("some"), "default"))
|
.default_value("first")
|
||||||
|
.default_value_if("opt", Some("some"), "default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "--opt", "other"]);
|
.get_matches_from_safe(vec!["", "--opt", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -271,10 +302,12 @@ fn default_if_arg_present_with_value_no_arg_with_default_fail() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_with_value_with_default() {
|
fn default_if_arg_present_with_value_with_default() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_if("opt", Some("some"), "default"))
|
.default_value("first")
|
||||||
|
.default_value_if("opt", Some("some"), "default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "--opt", "some"]);
|
.get_matches_from_safe(vec!["", "--opt", "some"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -285,10 +318,12 @@ fn default_if_arg_present_with_value_with_default() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_with_value_with_default_user_override() {
|
fn default_if_arg_present_with_value_with_default_user_override() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_if("opt", Some("some"), "default"))
|
.default_value("first")
|
||||||
|
.default_value_if("opt", Some("some"), "default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "--opt", "some", "other"]);
|
.get_matches_from_safe(vec!["", "--opt", "some", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -299,10 +334,12 @@ fn default_if_arg_present_with_value_with_default_user_override() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_no_arg_with_value_with_default_user_override() {
|
fn default_if_arg_present_no_arg_with_value_with_default_user_override() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_if("opt", Some("some"), "default"))
|
.default_value("first")
|
||||||
|
.default_value_if("opt", Some("some"), "default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "other"]);
|
.get_matches_from_safe(vec!["", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -313,10 +350,12 @@ fn default_if_arg_present_no_arg_with_value_with_default_user_override() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_if_arg_present_no_arg_with_value_with_default_user_override_fail() {
|
fn default_if_arg_present_no_arg_with_value_with_default_user_override_fail() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_if("opt", Some("some"), "default"))
|
.default_value("first")
|
||||||
|
.default_value_if("opt", Some("some"), "default"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["", "--opt", "value", "other"]);
|
.get_matches_from_safe(vec!["", "--opt", "value", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -329,14 +368,13 @@ fn default_if_arg_present_no_arg_with_value_with_default_user_override_fail() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_ifs_arg_present() {
|
fn default_ifs_arg_present() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("--flag 'some arg'"))
|
.arg(Arg::from_usage("--flag 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_ifs(&[
|
.default_value("first")
|
||||||
("opt", Some("some"), "default"),
|
.default_value_ifs(&[("opt", Some("some"), "default"), ("flag", None, "flg")]),
|
||||||
("flag", None, "flg"),
|
)
|
||||||
]))
|
|
||||||
.get_matches_from_safe(vec!["", "--flag"]);
|
.get_matches_from_safe(vec!["", "--flag"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -347,14 +385,13 @@ fn default_ifs_arg_present() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_ifs_arg_present_user_override() {
|
fn default_ifs_arg_present_user_override() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("--flag 'some arg'"))
|
.arg(Arg::from_usage("--flag 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_ifs(&[
|
.default_value("first")
|
||||||
("opt", Some("some"), "default"),
|
.default_value_ifs(&[("opt", Some("some"), "default"), ("flag", None, "flg")]),
|
||||||
("flag", None, "flg"),
|
)
|
||||||
]))
|
|
||||||
.get_matches_from_safe(vec!["", "--flag", "value"]);
|
.get_matches_from_safe(vec!["", "--flag", "value"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -365,14 +402,13 @@ fn default_ifs_arg_present_user_override() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_ifs_arg_present_order() {
|
fn default_ifs_arg_present_order() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("--opt [FILE] 'some arg'"))
|
.arg(Arg::from_usage("--opt [FILE] 'some arg'"))
|
||||||
.arg( Arg::from_usage("--flag 'some arg'"))
|
.arg(Arg::from_usage("--flag 'some arg'"))
|
||||||
.arg( Arg::from_usage("[arg] 'some arg'")
|
.arg(
|
||||||
.default_value("first")
|
Arg::from_usage("[arg] 'some arg'")
|
||||||
.default_value_ifs(&[
|
.default_value("first")
|
||||||
("opt", Some("some"), "default"),
|
.default_value_ifs(&[("opt", Some("some"), "default"), ("flag", None, "flg")]),
|
||||||
("flag", None, "flg"),
|
)
|
||||||
]))
|
|
||||||
.get_matches_from_safe(vec!["", "--opt=some", "--flag"]);
|
.get_matches_from_safe(vec!["", "--opt=some", "--flag"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -386,19 +422,25 @@ fn conditional_reqs_fail() {
|
||||||
.version("1.0")
|
.version("1.0")
|
||||||
.author("F0x06")
|
.author("F0x06")
|
||||||
.about("Arg test")
|
.about("Arg test")
|
||||||
.arg(Arg::with_name("target")
|
.arg(
|
||||||
.takes_value(true)
|
Arg::with_name("target")
|
||||||
.default_value("file")
|
.takes_value(true)
|
||||||
.possible_values(&["file", "stdout"])
|
.default_value("file")
|
||||||
.long("target"))
|
.possible_values(&["file", "stdout"])
|
||||||
.arg(Arg::with_name("input")
|
.long("target"),
|
||||||
.takes_value(true)
|
)
|
||||||
.required(true)
|
.arg(
|
||||||
.long("input"))
|
Arg::with_name("input")
|
||||||
.arg(Arg::with_name("output")
|
.takes_value(true)
|
||||||
.takes_value(true)
|
.required(true)
|
||||||
.required_if("target", "file")
|
.long("input"),
|
||||||
.long("output"))
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("output")
|
||||||
|
.takes_value(true)
|
||||||
|
.required_if("target", "file")
|
||||||
|
.long("output"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["test", "--input", "some"]);
|
.get_matches_from_safe(vec!["test", "--input", "some"]);
|
||||||
|
|
||||||
assert!(m.is_err());
|
assert!(m.is_err());
|
||||||
|
@ -411,19 +453,25 @@ fn conditional_reqs_pass() {
|
||||||
.version("1.0")
|
.version("1.0")
|
||||||
.author("F0x06")
|
.author("F0x06")
|
||||||
.about("Arg test")
|
.about("Arg test")
|
||||||
.arg(Arg::with_name("target")
|
.arg(
|
||||||
.takes_value(true)
|
Arg::with_name("target")
|
||||||
.default_value("file")
|
.takes_value(true)
|
||||||
.possible_values(&["file", "stdout"])
|
.default_value("file")
|
||||||
.long("target"))
|
.possible_values(&["file", "stdout"])
|
||||||
.arg(Arg::with_name("input")
|
.long("target"),
|
||||||
.takes_value(true)
|
)
|
||||||
.required(true)
|
.arg(
|
||||||
.long("input"))
|
Arg::with_name("input")
|
||||||
.arg(Arg::with_name("output")
|
.takes_value(true)
|
||||||
.takes_value(true)
|
.required(true)
|
||||||
.required_if("target", "file")
|
.long("input"),
|
||||||
.long("output"))
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("output")
|
||||||
|
.takes_value(true)
|
||||||
|
.required_if("target", "file")
|
||||||
|
.long("output"),
|
||||||
|
)
|
||||||
.get_matches_from_safe(vec!["test", "--input", "some", "--output", "other"]);
|
.get_matches_from_safe(vec!["test", "--input", "some", "--output", "other"]);
|
||||||
|
|
||||||
assert!(m.is_ok());
|
assert!(m.is_ok());
|
||||||
|
|
263
tests/env.rs
Normal file
263
tests/env.rs
Normal file
|
@ -0,0 +1,263 @@
|
||||||
|
extern crate clap;
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
use std::ffi::OsStr;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn env() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(Arg::from_usage("[arg] 'some opt'").env("CLP_TEST_ENV"))
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 0);
|
||||||
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn env_os() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(
|
||||||
|
Arg::from_usage("[arg] 'some opt'").env_os(OsStr::new("CLP_TEST_ENV")),
|
||||||
|
)
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 0);
|
||||||
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn no_env() {
|
||||||
|
// All the other tests use the presence of the Environment variable...
|
||||||
|
// we need another variable just in case one of the others is running at the same time...
|
||||||
|
env::remove_var("CLP_TEST_ENV_NONE");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(Arg::from_usage("[arg] 'some opt'").env("CLP_TEST_ENV_NONE"))
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(!m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 0);
|
||||||
|
assert_eq!(m.value_of("arg"), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn with_default() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(
|
||||||
|
Arg::from_usage("[arg] 'some opt'")
|
||||||
|
.env("CLP_TEST_ENV")
|
||||||
|
.default_value("default"),
|
||||||
|
)
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 0);
|
||||||
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn opt_user_override() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(
|
||||||
|
Arg::from_usage("--arg [FILE] 'some arg'").env("CLP_TEST_ENV"),
|
||||||
|
)
|
||||||
|
.get_matches_from_safe(vec!["", "--arg", "opt"]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 1);
|
||||||
|
assert_eq!(m.value_of("arg").unwrap(), "opt");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn positionals() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(Arg::from_usage("[arg] 'some opt'").env("CLP_TEST_ENV"))
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 0);
|
||||||
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn positionals_user_override() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(Arg::from_usage("[arg] 'some opt'").env("CLP_TEST_ENV"))
|
||||||
|
.get_matches_from_safe(vec!["", "opt"]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 1);
|
||||||
|
assert_eq!(m.value_of("arg").unwrap(), "opt");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn multiple_one() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(
|
||||||
|
Arg::from_usage("[arg] 'some opt'")
|
||||||
|
.env("CLP_TEST_ENV")
|
||||||
|
.use_delimiter(true)
|
||||||
|
.multiple(true),
|
||||||
|
)
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 0);
|
||||||
|
assert_eq!(m.values_of("arg").unwrap().collect::<Vec<_>>(), vec!["env"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn multiple_three() {
|
||||||
|
env::set_var("CLP_TEST_ENV_MULTI1", "env1,env2,env3");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(
|
||||||
|
Arg::from_usage("[arg] 'some opt'")
|
||||||
|
.env("CLP_TEST_ENV_MULTI1")
|
||||||
|
.use_delimiter(true)
|
||||||
|
.multiple(true),
|
||||||
|
)
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 0);
|
||||||
|
assert_eq!(
|
||||||
|
m.values_of("arg").unwrap().collect::<Vec<_>>(),
|
||||||
|
vec!["env1", "env2", "env3"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn multiple_no_delimiter() {
|
||||||
|
env::set_var("CLP_TEST_ENV_MULTI2", "env1 env2 env3");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(
|
||||||
|
Arg::from_usage("[arg] 'some opt'")
|
||||||
|
.env("CLP_TEST_ENV_MULTI2")
|
||||||
|
.multiple(true),
|
||||||
|
)
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 0);
|
||||||
|
assert_eq!(
|
||||||
|
m.values_of("arg").unwrap().collect::<Vec<_>>(),
|
||||||
|
vec!["env1 env2 env3"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn possible_value() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(
|
||||||
|
Arg::from_usage("[arg] 'some opt'")
|
||||||
|
.env("CLP_TEST_ENV")
|
||||||
|
.possible_value("env"),
|
||||||
|
)
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 0);
|
||||||
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn not_possible_value() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(
|
||||||
|
Arg::from_usage("[arg] 'some opt'")
|
||||||
|
.env("CLP_TEST_ENV")
|
||||||
|
.possible_value("never"),
|
||||||
|
)
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validator() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(
|
||||||
|
Arg::from_usage("[arg] 'some opt'")
|
||||||
|
.env("CLP_TEST_ENV")
|
||||||
|
.validator(|s| if s == "env" {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err("not equal".to_string())
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
let m = r.unwrap();
|
||||||
|
assert!(m.is_present("arg"));
|
||||||
|
assert_eq!(m.occurrences_of("arg"), 0);
|
||||||
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validator_invalid() {
|
||||||
|
env::set_var("CLP_TEST_ENV", "env");
|
||||||
|
|
||||||
|
let r = App::new("df")
|
||||||
|
.arg(
|
||||||
|
Arg::from_usage("[arg] 'some opt'")
|
||||||
|
.env("CLP_TEST_ENV")
|
||||||
|
.validator(|s| if s != "env" {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err("is equal".to_string())
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.get_matches_from_safe(vec![""]);
|
||||||
|
|
||||||
|
assert!(r.is_err());
|
||||||
|
}
|
Loading…
Reference in a new issue