2016-09-05 19:29:40 +00:00
|
|
|
// Std
|
2016-09-05 21:03:45 +00:00
|
|
|
use std::cmp;
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::fmt::Display;
|
|
|
|
use std::io::{self, Cursor, Read, Write};
|
|
|
|
use std::usize;
|
2016-04-01 06:00:11 +00:00
|
|
|
|
2016-09-05 19:29:40 +00:00
|
|
|
// Internal
|
2016-04-01 06:00:11 +00:00
|
|
|
use app::{App, AppSettings};
|
|
|
|
use app::parser::Parser;
|
2016-09-05 19:29:40 +00:00
|
|
|
use args::{AnyArg, ArgSettings, DispOrder};
|
|
|
|
use errors::{Error, Result as ClapResult};
|
2016-05-30 08:07:44 +00:00
|
|
|
use fmt::{Format, Colorizer};
|
2016-04-01 06:00:11 +00:00
|
|
|
|
2016-09-05 21:03:45 +00:00
|
|
|
// Third Party
|
|
|
|
use unicode_width::UnicodeWidthStr;
|
2016-08-29 05:36:06 +00:00
|
|
|
#[cfg(feature = "wrap_help")]
|
2016-06-30 03:19:08 +00:00
|
|
|
use term_size;
|
2016-09-05 19:29:40 +00:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
|
|
|
use vec_map::VecMap;
|
2016-09-05 21:03:45 +00:00
|
|
|
|
2016-08-26 11:11:55 +00:00
|
|
|
#[cfg(not(feature = "wrap_help"))]
|
2016-06-30 03:19:08 +00:00
|
|
|
mod term_size {
|
|
|
|
pub fn dimensions() -> Option<(usize, usize)> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 06:00:11 +00:00
|
|
|
|
2016-04-10 00:35:24 +00:00
|
|
|
fn str_width(s: &str) -> usize {
|
|
|
|
UnicodeWidthStr::width(s)
|
|
|
|
}
|
|
|
|
|
2016-04-01 06:00:11 +00:00
|
|
|
const TAB: &'static str = " ";
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
// These are just convenient traits to make the code easier to read.
|
2016-04-01 06:00:11 +00:00
|
|
|
trait ArgWithDisplay<'b, 'c>: 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 {
|
|
|
|
fn as_base(&self) -> &ArgWithDisplay<'b, 'c>;
|
|
|
|
}
|
|
|
|
impl<'b, 'c, T> ArgWithOrder<'b, 'c> for T
|
|
|
|
where T: ArgWithDisplay<'b, 'c> + DispOrder
|
|
|
|
{
|
|
|
|
fn as_base(&self) -> &ArgWithDisplay<'b, 'c> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
fn as_arg_trait<'a, 'b, T: ArgWithOrder<'a, 'b>>(x: &T) -> &ArgWithOrder<'a, 'b> {
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
2016-04-01 06:00:11 +00:00
|
|
|
impl<'b, 'c> DispOrder for App<'b, 'c> {
|
|
|
|
fn disp_ord(&self) -> usize {
|
|
|
|
999
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 06:45:44 +00:00
|
|
|
macro_rules! color {
|
2016-05-30 08:07:44 +00:00
|
|
|
($_self:ident, $s:expr, $c:ident) => {
|
2016-04-18 06:45:44 +00:00
|
|
|
if $_self.color {
|
2016-05-30 08:07:44 +00:00
|
|
|
write!($_self.writer, "{}", $_self.cizer.$c($s))
|
2016-04-18 06:45:44 +00:00
|
|
|
} else {
|
2016-05-30 08:07:44 +00:00
|
|
|
write!($_self.writer, "{}", $s)
|
2016-04-18 06:45:44 +00:00
|
|
|
}
|
|
|
|
};
|
2016-05-30 08:07:44 +00:00
|
|
|
($_self:ident, $fmt_s:expr, $v:expr, $c:ident) => {
|
2016-04-18 06:45:44 +00:00
|
|
|
if $_self.color {
|
2016-05-30 08:07:44 +00:00
|
|
|
write!($_self.writer, "{}", $_self.cizer.$c(format!($fmt_s, $v)))
|
2016-04-18 06:45:44 +00:00
|
|
|
} else {
|
2016-05-30 08:07:44 +00:00
|
|
|
write!($_self.writer, $fmt_s, $v)
|
2016-04-18 06:45:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-04-03 04:20:55 +00:00
|
|
|
|
2016-05-14 20:25:00 +00:00
|
|
|
/// `clap` Help Writer.
|
2016-04-03 04:20:55 +00:00
|
|
|
///
|
2016-05-14 20:25:00 +00:00
|
|
|
/// Wraps a writer stream providing different methods to generate help for `clap` objects.
|
2016-04-01 06:00:11 +00:00
|
|
|
pub struct Help<'a> {
|
|
|
|
writer: &'a mut Write,
|
|
|
|
next_line_help: bool,
|
|
|
|
hide_pv: bool,
|
2016-06-14 01:45:24 +00:00
|
|
|
term_w: usize,
|
2016-04-18 06:45:44 +00:00
|
|
|
color: bool,
|
2016-05-30 08:07:44 +00:00
|
|
|
cizer: Colorizer,
|
2016-10-19 22:54:36 +00:00
|
|
|
longest: usize,
|
|
|
|
force_next_line: bool,
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
// Public Functions
|
2016-04-01 06:00:11 +00:00
|
|
|
impl<'a> Help<'a> {
|
2016-05-14 20:25:00 +00:00
|
|
|
/// Create a new `Help` instance.
|
2016-09-05 19:29:40 +00:00
|
|
|
pub fn new(w: &'a mut Write,
|
|
|
|
next_line_help: bool,
|
|
|
|
hide_pv: bool,
|
|
|
|
color: bool,
|
|
|
|
cizer: Colorizer,
|
2016-09-10 17:46:02 +00:00
|
|
|
term_w: Option<usize>,
|
|
|
|
max_w: Option<usize>)
|
2016-09-05 19:29:40 +00:00
|
|
|
-> Self {
|
2016-05-30 08:07:44 +00:00
|
|
|
debugln!("fn=Help::new;");
|
2016-04-01 06:00:11 +00:00
|
|
|
Help {
|
|
|
|
writer: w,
|
|
|
|
next_line_help: next_line_help,
|
|
|
|
hide_pv: hide_pv,
|
2016-06-14 01:45:24 +00:00
|
|
|
term_w: match term_w {
|
2016-09-05 19:29:40 +00:00
|
|
|
Some(width) => if width == 0 { usize::MAX } else { width },
|
2016-09-10 17:46:02 +00:00
|
|
|
None => cmp::min(term_size::dimensions().map_or(120, |(w, _)| w), match max_w {
|
|
|
|
None | Some(0) => usize::MAX,
|
|
|
|
Some(mw) => mw,
|
|
|
|
}),
|
2016-06-14 01:45:24 +00:00
|
|
|
},
|
2016-04-18 06:45:44 +00:00
|
|
|
color: color,
|
2016-05-30 08:07:44 +00:00
|
|
|
cizer: cizer,
|
2016-10-19 22:54:36 +00:00
|
|
|
longest: 0,
|
|
|
|
force_next_line: false,
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Reads help settings from an App
|
|
|
|
/// and write its help to the wrapped stream.
|
2016-04-01 06:00:11 +00:00
|
|
|
pub fn write_app_help(w: &'a mut Write, app: &App) -> ClapResult<()> {
|
2016-05-30 08:07:44 +00:00
|
|
|
debugln!("fn=Help::write_app_help;");
|
2016-04-10 00:35:24 +00:00
|
|
|
Self::write_parser_help(w, &app.p)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads help settings from a Parser
|
|
|
|
/// and write its help to the wrapped stream.
|
|
|
|
pub fn write_parser_help(w: &'a mut Write, parser: &Parser) -> ClapResult<()> {
|
2016-05-30 08:07:44 +00:00
|
|
|
debugln!("fn=Help::write_parser_help;");
|
|
|
|
Self::_write_parser_help(w, parser, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads help settings from a Parser
|
|
|
|
/// and write its help to the wrapped stream which will be stderr. This method prevents
|
|
|
|
/// formatting when required.
|
|
|
|
pub fn write_parser_help_to_stderr(w: &'a mut Write, parser: &Parser) -> ClapResult<()> {
|
|
|
|
debugln!("fn=Help::write_parser_help;");
|
|
|
|
Self::_write_parser_help(w, parser, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn _write_parser_help(w: &'a mut Write, parser: &Parser, stderr: bool) -> ClapResult<()> {
|
|
|
|
debugln!("fn=Help::write_parser_help;");
|
2016-04-10 00:35:24 +00:00
|
|
|
let nlh = parser.is_set(AppSettings::NextLineHelp);
|
|
|
|
let hide_v = parser.is_set(AppSettings::HidePossibleValuesInHelp);
|
2016-04-18 06:45:44 +00:00
|
|
|
let color = parser.is_set(AppSettings::ColoredHelp);
|
2016-05-30 08:07:44 +00:00
|
|
|
let cizer = Colorizer {
|
|
|
|
use_stderr: stderr,
|
|
|
|
when: parser.color(),
|
|
|
|
};
|
2016-09-10 17:46:02 +00:00
|
|
|
Self::new(w, nlh, hide_v, color, cizer, parser.meta.term_w, parser.meta.max_w).write_help(parser)
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes the parser help to the wrapped stream.
|
|
|
|
pub fn write_help(&mut self, parser: &Parser) -> ClapResult<()> {
|
2016-05-30 08:07:44 +00:00
|
|
|
debugln!("fn=Help::write_help;");
|
2016-04-03 04:20:55 +00:00
|
|
|
if let Some(h) = parser.meta.help_str {
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(write!(self.writer, "{}", h).map_err(Error::from));
|
2016-08-20 21:33:58 +00:00
|
|
|
} else if let Some(tmpl) = parser.meta.template {
|
2016-04-03 04:20:55 +00:00
|
|
|
try!(self.write_templated_help(&parser, tmpl));
|
|
|
|
} else {
|
|
|
|
try!(self.write_default_help(&parser));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
// Methods to write AnyArg help.
|
2016-04-01 06:00:11 +00:00
|
|
|
impl<'a> Help<'a> {
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes help for each argument in the order they were declared to the wrapped stream.
|
2016-04-01 06:00:11 +00:00
|
|
|
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>>
|
|
|
|
{
|
2016-10-19 22:54:36 +00:00
|
|
|
// The shortest an arg can legally be is 2 (i.e. '-x')
|
|
|
|
self.longest = 2;
|
2016-04-01 06:00:11 +00:00
|
|
|
let mut arg_v = Vec::with_capacity(10);
|
|
|
|
for arg in args.filter(|arg| {
|
|
|
|
!(arg.is_set(ArgSettings::Hidden)) || arg.is_set(ArgSettings::NextLineHelp)
|
|
|
|
}) {
|
|
|
|
if arg.longest_filter() {
|
2016-10-19 22:54:36 +00:00
|
|
|
self.longest = cmp::max(self.longest, arg.to_string().len());
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
if !arg.is_set(ArgSettings::Hidden) {
|
|
|
|
arg_v.push(arg)
|
|
|
|
}
|
|
|
|
}
|
2016-05-30 08:07:44 +00:00
|
|
|
let mut first = true;
|
2016-04-01 06:00:11 +00:00
|
|
|
for arg in arg_v {
|
2016-10-04 17:06:43 +00:00
|
|
|
if first {
|
2016-05-30 08:07:44 +00:00
|
|
|
first = false;
|
2016-10-04 17:06:43 +00:00
|
|
|
} else {
|
|
|
|
try!(self.writer.write(b"\n"));
|
|
|
|
}
|
2016-10-19 22:54:36 +00:00
|
|
|
try!(self.write_arg(arg.as_base()));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Sorts arguments by length and display order and write their help to the wrapped stream.
|
2016-04-01 06:00:11 +00:00
|
|
|
fn write_args<'b: 'd, 'c: 'd, 'd, I: 'd>(&mut self, args: I) -> io::Result<()>
|
|
|
|
where I: Iterator<Item = &'d ArgWithOrder<'b, 'c>>
|
|
|
|
{
|
2016-05-30 08:07:44 +00:00
|
|
|
debugln!("fn=write_args;");
|
2016-10-19 22:54:36 +00:00
|
|
|
// The shortest an arg can legally be is 2 (i.e. '-x')
|
|
|
|
self.longest = 2;
|
2016-04-01 06:00:11 +00:00
|
|
|
let mut ord_m = VecMap::new();
|
2016-10-19 22:54:36 +00:00
|
|
|
// Determine the longest
|
2016-04-01 06:00:11 +00:00
|
|
|
for arg in args.filter(|arg| {
|
2016-10-19 22:54:36 +00:00
|
|
|
// If it's NextLineHelp, but we don't care to compute how long because it may be
|
|
|
|
// NextLineHelp on purpose *because* it's so long and would throw off all other
|
|
|
|
// args alignment
|
|
|
|
!arg.is_set(ArgSettings::Hidden) || arg.is_set(ArgSettings::NextLineHelp)
|
2016-04-01 06:00:11 +00:00
|
|
|
}) {
|
|
|
|
if arg.longest_filter() {
|
2016-10-19 22:54:36 +00:00
|
|
|
debugln!("Longest...{}", self.longest);
|
|
|
|
self.longest = cmp::max(self.longest, arg.to_string().len());
|
|
|
|
debugln!("New Longest...{}", self.longest);
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
2016-10-19 22:54:36 +00:00
|
|
|
let btm = ord_m.entry(arg.disp_ord()).or_insert(BTreeMap::new());
|
|
|
|
btm.insert(arg.name(), arg);
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
2016-05-30 08:07:44 +00:00
|
|
|
let mut first = true;
|
2016-10-04 17:06:43 +00:00
|
|
|
for btm in ord_m.values() {
|
2016-10-04 13:54:45 +00:00
|
|
|
for arg in btm.values() {
|
2016-10-04 17:06:43 +00:00
|
|
|
if first {
|
2016-05-30 08:07:44 +00:00
|
|
|
first = false;
|
2016-10-04 17:06:43 +00:00
|
|
|
} else {
|
|
|
|
try!(self.writer.write(b"\n"));
|
2016-05-30 08:07:44 +00:00
|
|
|
}
|
2016-10-19 22:54:36 +00:00
|
|
|
try!(self.write_arg(arg.as_base()));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes help for an argument to the wrapped stream.
|
2016-04-01 06:00:11 +00:00
|
|
|
fn write_arg<'b, 'c>(&mut self,
|
2016-10-19 22:54:36 +00:00
|
|
|
arg: &ArgWithDisplay<'b, 'c>)
|
2016-04-01 06:00:11 +00:00
|
|
|
-> io::Result<()> {
|
2016-05-30 08:07:44 +00:00
|
|
|
debugln!("fn=write_arg;");
|
2016-04-01 06:00:11 +00:00
|
|
|
try!(self.short(arg));
|
2016-10-19 22:54:36 +00:00
|
|
|
try!(self.long(arg));
|
|
|
|
let spec_vals = try!(self.val(arg));
|
|
|
|
try!(self.help(arg, &*spec_vals));
|
2016-04-01 06:00:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes argument's short command to the wrapped stream.
|
2016-04-01 06:00:11 +00:00
|
|
|
fn short<'b, 'c>(&mut self, arg: &ArgWithDisplay<'b, 'c>) -> io::Result<()> {
|
|
|
|
debugln!("fn=short;");
|
|
|
|
try!(write!(self.writer, "{}", TAB));
|
|
|
|
if let Some(s) = arg.short() {
|
2016-05-30 08:07:44 +00:00
|
|
|
color!(self, "-{}", s, good)
|
2016-04-01 06:00:11 +00:00
|
|
|
} else if arg.has_switch() {
|
|
|
|
write!(self.writer, "{}", TAB)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes argument's long command to the wrapped stream.
|
2016-10-19 22:54:36 +00:00
|
|
|
fn long<'b, 'c>(&mut self, arg: &ArgWithDisplay<'b, 'c>) -> io::Result<()> {
|
2016-04-01 06:00:11 +00:00
|
|
|
debugln!("fn=long;");
|
|
|
|
if !arg.has_switch() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
if arg.takes_value() {
|
|
|
|
if let Some(l) = arg.long() {
|
2016-04-18 06:45:44 +00:00
|
|
|
if arg.short().is_some() {
|
|
|
|
try!(write!(self.writer, ", "));
|
|
|
|
}
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(color!(self, "--{}", l, good))
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
try!(write!(self.writer, " "));
|
2016-07-26 00:56:22 +00:00
|
|
|
} else if let Some(l) = arg.long() {
|
|
|
|
if arg.short().is_some() {
|
|
|
|
try!(write!(self.writer, ", "));
|
|
|
|
}
|
|
|
|
try!(color!(self, "--{}", l, good));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes argument's possible values to the wrapped stream.
|
2016-10-19 22:54:36 +00:00
|
|
|
fn val<'b, 'c>(&mut self, arg: &ArgWithDisplay<'b, 'c>) -> Result<String, io::Error> {
|
2016-10-20 23:35:58 +00:00
|
|
|
debugln!("fn=val;arg={}", arg);
|
2016-10-19 22:54:36 +00:00
|
|
|
if arg.takes_value() {
|
|
|
|
if let Some(vec) = arg.val_names() {
|
|
|
|
let mut it = vec.iter().peekable();
|
|
|
|
while let Some((_, val)) = it.next() {
|
|
|
|
try!(color!(self, "<{}>", val, good));
|
|
|
|
if it.peek().is_some() {
|
|
|
|
try!(write!(self.writer, " "));
|
|
|
|
}
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
2016-10-19 22:54:36 +00:00
|
|
|
let num = vec.len();
|
|
|
|
if arg.is_set(ArgSettings::Multiple) && num == 1 {
|
|
|
|
try!(color!(self, "...", good));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
2016-10-19 22:54:36 +00:00
|
|
|
} else if let Some(num) = arg.num_vals() {
|
|
|
|
let mut it = (0..num).peekable();
|
|
|
|
while let Some(_) = it.next() {
|
|
|
|
try!(color!(self, "<{}>", arg.name(), good));
|
|
|
|
if it.peek().is_some() {
|
|
|
|
try!(write!(self.writer, " "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if arg.has_switch() {
|
|
|
|
try!(color!(self, "<{}>", arg.name(), good));
|
|
|
|
} else {
|
|
|
|
try!(color!(self, "{}", arg, good));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-28 03:42:31 +00:00
|
|
|
|
|
|
|
let spec_vals = self.spec_vals(arg);
|
|
|
|
let h = arg.help().unwrap_or("");
|
2016-10-19 22:54:36 +00:00
|
|
|
let h_w = str_width(h) + str_width(&*spec_vals);
|
2016-08-28 03:42:31 +00:00
|
|
|
let nlh = self.next_line_help || arg.is_set(ArgSettings::NextLineHelp);
|
2016-10-19 22:54:36 +00:00
|
|
|
let taken = self.longest + 12;
|
|
|
|
self.force_next_line = !nlh && self.term_w >= taken &&
|
|
|
|
(taken as f32 / self.term_w as f32) > 0.40 &&
|
|
|
|
h_w > (self.term_w - taken);
|
2016-08-28 03:42:31 +00:00
|
|
|
|
2016-10-19 22:54:36 +00:00
|
|
|
debug!("Has switch...");
|
2016-04-01 06:00:11 +00:00
|
|
|
if arg.has_switch() {
|
2016-10-19 22:54:36 +00:00
|
|
|
sdebugln!("Yes");
|
|
|
|
debugln!("force_next_line...{:?}", self.force_next_line);
|
|
|
|
debugln!("nlh...{:?}", nlh);
|
|
|
|
debugln!("taken...{}", taken);
|
|
|
|
debugln!("help_width > (width - taken)...{} > ({} - {})", h_w, self.term_w, taken);
|
2016-10-20 23:35:58 +00:00
|
|
|
debugln!("longest...{}", self.longest);
|
2016-10-19 22:54:36 +00:00
|
|
|
debug!("next_line...");
|
|
|
|
if !(nlh || self.force_next_line) {
|
|
|
|
sdebugln!("No");
|
2016-04-01 06:00:11 +00:00
|
|
|
let self_len = arg.to_string().len();
|
|
|
|
// subtract ourself
|
2016-10-19 22:54:36 +00:00
|
|
|
let mut spcs = self.longest - self_len;
|
2016-04-01 06:00:11 +00:00
|
|
|
// Since we're writing spaces from the tab point we first need to know if we
|
|
|
|
// had a long and short, or just short
|
|
|
|
if arg.long().is_some() {
|
|
|
|
// Only account 4 after the val
|
|
|
|
spcs += 4;
|
|
|
|
} else {
|
|
|
|
// Only account for ', --' + 4 after the val
|
|
|
|
spcs += 8;
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
write_nspaces!(self.writer, spcs);
|
2016-10-19 22:54:36 +00:00
|
|
|
} else {
|
|
|
|
sdebugln!("Yes");
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
2016-10-19 22:54:36 +00:00
|
|
|
} else if !(nlh || self.force_next_line) {
|
|
|
|
sdebugln!("No, and not next_line");
|
|
|
|
write_nspaces!(self.writer, self.longest + 4 - (arg.to_string().len()));
|
|
|
|
} else {
|
|
|
|
sdebugln!("No");
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
2016-10-19 22:54:36 +00:00
|
|
|
Ok(spec_vals)
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 00:56:22 +00:00
|
|
|
fn write_before_after_help(&mut self, h: &str) -> io::Result<()> {
|
2016-06-14 02:03:27 +00:00
|
|
|
debugln!("fn=before_help;");
|
|
|
|
let mut help = String::new();
|
|
|
|
// determine if our help fits or needs to wrap
|
2016-10-19 22:54:36 +00:00
|
|
|
debugln!("Term width...{}", self.term_w);
|
|
|
|
let too_long = str_width(h) >= self.term_w;
|
2016-06-14 02:03:27 +00:00
|
|
|
|
|
|
|
debug!("Too long...");
|
2016-09-13 03:19:26 +00:00
|
|
|
if too_long || h.contains("{n}") {
|
2016-06-14 02:03:27 +00:00
|
|
|
sdebugln!("Yes");
|
|
|
|
help.push_str(h);
|
|
|
|
debugln!("help: {}", help);
|
|
|
|
debugln!("help width: {}", str_width(&*help));
|
|
|
|
// Determine how many newlines we need to insert
|
2016-10-19 22:54:36 +00:00
|
|
|
debugln!("Usable space: {}", self.term_w);
|
2016-06-14 02:03:27 +00:00
|
|
|
let longest_w = {
|
|
|
|
let mut lw = 0;
|
|
|
|
for l in help.split(' ').map(|s| str_width(s)) {
|
|
|
|
if l > lw {
|
|
|
|
lw = l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lw
|
|
|
|
};
|
2016-09-07 21:53:03 +00:00
|
|
|
help = help.replace("{n}", "\n");
|
2016-10-19 22:54:36 +00:00
|
|
|
wrap_help(&mut help, longest_w, self.term_w);
|
2016-06-14 02:03:27 +00:00
|
|
|
} else {
|
|
|
|
sdebugln!("No");
|
|
|
|
}
|
|
|
|
let help = if !help.is_empty() {
|
|
|
|
&*help
|
|
|
|
} else {
|
|
|
|
help.push_str(h);
|
|
|
|
&*help
|
|
|
|
};
|
2016-10-04 13:54:45 +00:00
|
|
|
if help.contains('\n') {
|
2016-10-04 17:02:17 +00:00
|
|
|
if let Some(part) = help.lines().next() {
|
2016-06-14 02:03:27 +00:00
|
|
|
try!(write!(self.writer, "{}", part));
|
|
|
|
}
|
2016-10-04 17:02:17 +00:00
|
|
|
for part in help.lines().skip(1) {
|
2016-06-14 02:03:27 +00:00
|
|
|
try!(write!(self.writer, "\n{}", part));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try!(write!(self.writer, "{}", help));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes argument's help to the wrapped stream.
|
2016-10-19 22:54:36 +00:00
|
|
|
fn help<'b, 'c>(&mut self, arg: &ArgWithDisplay<'b, 'c>, spec_vals: &str) -> io::Result<()> {
|
2016-04-01 06:00:11 +00:00
|
|
|
debugln!("fn=help;");
|
|
|
|
let mut help = String::new();
|
|
|
|
let h = arg.help().unwrap_or("");
|
2016-08-28 03:42:31 +00:00
|
|
|
let nlh = self.next_line_help || arg.is_set(ArgSettings::NextLineHelp);
|
|
|
|
debugln!("Next Line...{:?}", nlh);
|
|
|
|
|
2016-10-19 22:54:36 +00:00
|
|
|
let spcs = if nlh || self.force_next_line {
|
2016-09-05 23:51:14 +00:00
|
|
|
12 // "tab" * 3
|
2016-04-01 06:00:11 +00:00
|
|
|
} else {
|
2016-10-19 22:54:36 +00:00
|
|
|
self.longest + 12
|
2016-04-01 06:00:11 +00:00
|
|
|
};
|
2016-08-28 03:42:31 +00:00
|
|
|
|
2016-10-19 22:54:36 +00:00
|
|
|
let too_long = spcs + str_width(h) + str_width(&*spec_vals) >= self.term_w;
|
2016-04-01 06:00:11 +00:00
|
|
|
|
2016-09-05 23:51:14 +00:00
|
|
|
// Is help on next line, if so then indent
|
2016-10-19 22:54:36 +00:00
|
|
|
if nlh || self.force_next_line {
|
2016-09-05 23:51:14 +00:00
|
|
|
try!(write!(self.writer, "\n{}{}{}", TAB, TAB, TAB));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug!("Too long...");
|
2016-10-19 22:54:36 +00:00
|
|
|
if too_long && spcs <= self.term_w || h.contains("{n}") {
|
2016-04-01 06:00:11 +00:00
|
|
|
sdebugln!("Yes");
|
|
|
|
help.push_str(h);
|
|
|
|
help.push_str(&*spec_vals);
|
2016-10-20 23:35:58 +00:00
|
|
|
debugln!("help...{}", help);
|
|
|
|
debugln!("help width...{}", str_width(&*help));
|
2016-04-01 06:00:11 +00:00
|
|
|
// Determine how many newlines we need to insert
|
2016-10-19 22:54:36 +00:00
|
|
|
let avail_chars = self.term_w - spcs;
|
2016-10-20 23:35:58 +00:00
|
|
|
debugln!("Usable space...{}", avail_chars);
|
2016-04-01 06:00:11 +00:00
|
|
|
let longest_w = {
|
|
|
|
let mut lw = 0;
|
2016-04-10 00:35:24 +00:00
|
|
|
for l in help.split(' ').map(|s| str_width(s)) {
|
2016-04-01 06:00:11 +00:00
|
|
|
if l > lw {
|
|
|
|
lw = l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lw
|
|
|
|
};
|
2016-09-07 21:53:03 +00:00
|
|
|
help = help.replace("{n}", "\n");
|
2016-08-25 22:31:55 +00:00
|
|
|
wrap_help(&mut help, longest_w, avail_chars);
|
2016-04-01 06:00:11 +00:00
|
|
|
} else {
|
|
|
|
sdebugln!("No");
|
|
|
|
}
|
|
|
|
let help = if !help.is_empty() {
|
|
|
|
&*help
|
2016-04-03 04:20:55 +00:00
|
|
|
} else if spec_vals.is_empty() {
|
|
|
|
h
|
|
|
|
} else {
|
2016-04-01 06:00:11 +00:00
|
|
|
help.push_str(h);
|
|
|
|
help.push_str(&*spec_vals);
|
|
|
|
&*help
|
|
|
|
};
|
2016-10-04 13:54:45 +00:00
|
|
|
if help.contains('\n') {
|
2016-10-04 17:02:17 +00:00
|
|
|
if let Some(part) = help.lines().next() {
|
2016-04-01 06:00:11 +00:00
|
|
|
try!(write!(self.writer, "{}", part));
|
|
|
|
}
|
2016-10-04 17:02:17 +00:00
|
|
|
for part in help.lines().skip(1) {
|
2016-04-03 04:20:55 +00:00
|
|
|
try!(write!(self.writer, "\n"));
|
2016-10-19 22:54:36 +00:00
|
|
|
if nlh || self.force_next_line {
|
2016-09-05 23:51:14 +00:00
|
|
|
try!(write!(self.writer, "{}{}{}", TAB, TAB, TAB));
|
2016-04-01 06:00:11 +00:00
|
|
|
} else if arg.has_switch() {
|
2016-10-19 22:54:36 +00:00
|
|
|
write_nspaces!(self.writer, self.longest + 12);
|
2016-04-01 06:00:11 +00:00
|
|
|
} else {
|
2016-10-19 22:54:36 +00:00
|
|
|
write_nspaces!(self.writer, self.longest + 8);
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
2016-04-03 04:20:55 +00:00
|
|
|
try!(write!(self.writer, "{}", part));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try!(write!(self.writer, "{}", help));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spec_vals(&self, a: &ArgWithDisplay) -> String {
|
2016-10-20 23:35:58 +00:00
|
|
|
debugln!("fn=spec_vals;a={}", a);
|
2016-10-19 22:54:36 +00:00
|
|
|
let mut spec_vals = vec![];
|
2016-08-20 21:33:58 +00:00
|
|
|
if let Some(pv) = a.default_val() {
|
2016-10-19 22:54:36 +00:00
|
|
|
debugln!("Found default value...[{}]", pv);
|
|
|
|
spec_vals.push(format!(" [default: {}]",
|
|
|
|
if self.color {
|
|
|
|
self.cizer.good(pv)
|
|
|
|
} else {
|
|
|
|
Format::None(pv)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if let Some(ref aliases) = a.aliases() {
|
|
|
|
debugln!("Found aliases...{:?}", aliases);
|
|
|
|
spec_vals.push(format!(" [aliases: {}]",
|
2016-09-05 19:29:40 +00:00
|
|
|
if self.color {
|
|
|
|
aliases.iter()
|
|
|
|
.map(|v| format!("{}", self.cizer.good(v)))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")
|
|
|
|
} else {
|
|
|
|
aliases.join(", ")
|
2016-10-19 22:54:36 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
if !self.hide_pv && !a.is_set(ArgSettings::HidePossibleValues) {
|
|
|
|
if let Some(ref pv) = a.possible_vals() {
|
|
|
|
debugln!("Found possible vals...{:?}", pv);
|
|
|
|
spec_vals.push(if self.color {
|
2016-05-06 21:35:53 +00:00
|
|
|
format!(" [values: {}]",
|
|
|
|
pv.iter()
|
2016-09-05 19:29:40 +00:00
|
|
|
.map(|v| format!("{}", self.cizer.good(v)))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "))
|
2016-05-06 21:35:53 +00:00
|
|
|
} else {
|
|
|
|
format!(" [values: {}]", pv.join(", "))
|
2016-10-19 22:54:36 +00:00
|
|
|
});
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-19 22:54:36 +00:00
|
|
|
spec_vals.join(" ")
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
// Methods to write Parser help.
|
2016-04-01 06:00:11 +00:00
|
|
|
impl<'a> Help<'a> {
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes help for all arguments (options, flags, args, subcommands)
|
|
|
|
/// including titles of a Parser Object to the wrapped stream.
|
2016-06-08 04:10:56 +00:00
|
|
|
#[cfg_attr(feature = "lints", allow(useless_let_if_seq))]
|
2016-04-01 06:00:11 +00:00
|
|
|
pub fn write_all_args(&mut self, parser: &Parser) -> ClapResult<()> {
|
|
|
|
|
2016-04-09 18:25:21 +00:00
|
|
|
let flags = parser.has_flags();
|
|
|
|
let pos = parser.has_positionals();
|
|
|
|
let opts = parser.has_opts();
|
|
|
|
let subcmds = parser.has_subcommands();
|
2016-04-01 06:00:11 +00:00
|
|
|
|
|
|
|
let unified_help = parser.is_set(AppSettings::UnifiedHelpMessage);
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
let mut first = true;
|
|
|
|
|
2016-04-01 06:00:11 +00:00
|
|
|
if unified_help && (flags || opts) {
|
|
|
|
let opts_flags = parser.iter_flags()
|
2016-09-05 19:29:40 +00:00
|
|
|
.map(as_arg_trait)
|
|
|
|
.chain(parser.iter_opts().map(as_arg_trait));
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(color!(self, "OPTIONS:\n", warning));
|
2016-04-01 06:00:11 +00:00
|
|
|
try!(self.write_args(opts_flags));
|
2016-05-30 09:39:29 +00:00
|
|
|
first = false;
|
2016-04-01 06:00:11 +00:00
|
|
|
} else {
|
|
|
|
if flags {
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(color!(self, "FLAGS:\n", warning));
|
2016-04-01 06:00:11 +00:00
|
|
|
try!(self.write_args(parser.iter_flags()
|
2016-09-05 19:29:40 +00:00
|
|
|
.map(as_arg_trait)));
|
2016-05-30 09:39:29 +00:00
|
|
|
first = false;
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
if opts {
|
2016-04-03 04:20:55 +00:00
|
|
|
if !first {
|
2016-05-30 09:39:29 +00:00
|
|
|
try!(self.writer.write(b"\n\n"));
|
2016-04-03 04:20:55 +00:00
|
|
|
}
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(color!(self, "OPTIONS:\n", warning));
|
2016-04-01 06:00:11 +00:00
|
|
|
try!(self.write_args(parser.iter_opts().map(as_arg_trait)));
|
2016-05-30 09:39:29 +00:00
|
|
|
first = false;
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pos {
|
2016-04-03 04:20:55 +00:00
|
|
|
if !first {
|
2016-05-30 09:39:29 +00:00
|
|
|
try!(self.writer.write(b"\n\n"));
|
2016-04-03 04:20:55 +00:00
|
|
|
}
|
2016-05-30 09:39:29 +00:00
|
|
|
try!(color!(self, "ARGS:\n", warning));
|
2016-04-01 06:00:11 +00:00
|
|
|
try!(self.write_args_unsorted(parser.iter_positionals().map(as_arg_trait)));
|
2016-05-30 09:39:29 +00:00
|
|
|
first = false;
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if subcmds {
|
2016-05-30 09:39:29 +00:00
|
|
|
if !first {
|
|
|
|
try!(self.writer.write(b"\n\n"));
|
|
|
|
}
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(color!(self, "SUBCOMMANDS:\n", warning));
|
2016-04-03 04:20:55 +00:00
|
|
|
try!(self.write_subcommands(&parser));
|
|
|
|
}
|
2016-04-01 06:00:11 +00:00
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2016-04-01 06:00:11 +00:00
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes help for subcommands of a Parser Object to the wrapped stream.
|
|
|
|
fn write_subcommands(&mut self, parser: &Parser) -> io::Result<()> {
|
2016-09-13 01:48:22 +00:00
|
|
|
debugln!("fn=write_subcommands;");
|
2016-10-19 22:54:36 +00:00
|
|
|
// The shortest an arg can legally be is 2 (i.e. '-x')
|
|
|
|
self.longest = 2;
|
2016-04-03 04:20:55 +00:00
|
|
|
let mut ord_m = VecMap::new();
|
|
|
|
for sc in parser.subcommands.iter().filter(|s| !s.p.is_set(AppSettings::Hidden)) {
|
|
|
|
let btm = ord_m.entry(sc.p.meta.disp_ord).or_insert(BTreeMap::new());
|
2016-10-19 22:54:36 +00:00
|
|
|
self.longest = cmp::max(self.longest, sc.p.meta.name.len());
|
2016-06-10 01:30:52 +00:00
|
|
|
btm.insert(sc.p.meta.name.clone(), sc.clone());
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 08:07:44 +00:00
|
|
|
let mut first = true;
|
2016-10-04 17:06:43 +00:00
|
|
|
for btm in ord_m.values() {
|
2016-10-04 13:54:45 +00:00
|
|
|
for sc in btm.values() {
|
2016-10-04 17:06:43 +00:00
|
|
|
if first {
|
2016-05-30 08:07:44 +00:00
|
|
|
first = false;
|
2016-10-04 17:06:43 +00:00
|
|
|
} else {
|
|
|
|
try!(self.writer.write(b"\n"));
|
2016-05-30 08:07:44 +00:00
|
|
|
}
|
2016-10-19 22:54:36 +00:00
|
|
|
try!(self.write_arg(sc));
|
2016-04-03 04:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-01 06:00:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes version of a Parser Object to the wrapped stream.
|
2016-04-01 06:00:11 +00:00
|
|
|
fn write_version(&mut self, parser: &Parser) -> io::Result<()> {
|
|
|
|
try!(write!(self.writer, "{}", parser.meta.version.unwrap_or("".into())));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes binary name of a Parser Object to the wrapped stream.
|
2016-04-01 06:00:11 +00:00
|
|
|
fn write_bin_name(&mut self, parser: &Parser) -> io::Result<()> {
|
|
|
|
if let Some(bn) = parser.meta.bin_name.as_ref() {
|
|
|
|
if bn.contains(' ') {
|
|
|
|
// Incase we're dealing with subcommands i.e. git mv is translated to git-mv
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(color!(self, bn.replace(" ", "-"), good))
|
2016-04-01 06:00:11 +00:00
|
|
|
} else {
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(color!(self, &parser.meta.name[..], good))
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(color!(self, &parser.meta.name[..], good))
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Writes default help for a Parser Object to the wrapped stream.
|
2016-04-01 06:00:11 +00:00
|
|
|
pub fn write_default_help(&mut self, parser: &Parser) -> ClapResult<()> {
|
2016-05-30 08:07:44 +00:00
|
|
|
debugln!("fn=write_default_help;");
|
|
|
|
if let Some(h) = parser.meta.pre_help {
|
2016-06-14 02:03:27 +00:00
|
|
|
try!(self.write_before_after_help(h));
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(self.writer.write(b"\n\n"));
|
|
|
|
}
|
2016-04-01 06:00:11 +00:00
|
|
|
|
|
|
|
// Print the version
|
|
|
|
try!(self.write_bin_name(&parser));
|
|
|
|
try!(self.writer.write(b" "));
|
|
|
|
try!(self.write_version(&parser));
|
|
|
|
try!(self.writer.write(b"\n"));
|
|
|
|
if let Some(author) = parser.meta.author {
|
|
|
|
try!(write!(self.writer, "{}\n", author));
|
|
|
|
}
|
|
|
|
if let Some(about) = parser.meta.about {
|
|
|
|
try!(write!(self.writer, "{}\n", about));
|
|
|
|
}
|
|
|
|
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(color!(self, "\nUSAGE:", warning));
|
2016-05-06 21:35:53 +00:00
|
|
|
try!(write!(self.writer,
|
|
|
|
"\n{}{}\n\n",
|
2016-04-03 04:20:55 +00:00
|
|
|
TAB,
|
|
|
|
parser.create_usage_no_title(&[])));
|
2016-04-01 06:00:11 +00:00
|
|
|
|
2016-04-09 18:25:21 +00:00
|
|
|
let flags = parser.has_flags();
|
|
|
|
let pos = parser.has_positionals();
|
|
|
|
let opts = parser.has_opts();
|
|
|
|
let subcmds = parser.has_subcommands();
|
2016-04-01 06:00:11 +00:00
|
|
|
|
|
|
|
if flags || opts || pos || subcmds {
|
|
|
|
try!(self.write_all_args(&parser));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(h) = parser.meta.more_help {
|
2016-05-30 08:07:44 +00:00
|
|
|
if flags || opts || pos || subcmds {
|
|
|
|
try!(self.writer.write(b"\n\n"));
|
|
|
|
}
|
2016-06-14 02:03:27 +00:00
|
|
|
try!(self.write_before_after_help(h));
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.writer.flush().map_err(Error::from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:20:55 +00:00
|
|
|
/// Possible results for a copying function that stops when a given
|
|
|
|
/// byte was found.
|
|
|
|
enum CopyUntilResult {
|
|
|
|
DelimiterFound(usize),
|
|
|
|
DelimiterNotFound(usize),
|
|
|
|
ReaderEmpty,
|
|
|
|
ReadError(io::Error),
|
|
|
|
WriteError(io::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Copies the contents of a reader into a writer until a delimiter byte is found.
|
|
|
|
/// On success, the total number of bytes that were
|
|
|
|
/// copied from reader to writer is returned.
|
|
|
|
fn copy_until<R: Read, W: Write>(r: &mut R, w: &mut W, delimiter_byte: u8) -> CopyUntilResult {
|
|
|
|
|
|
|
|
let mut count = 0;
|
|
|
|
for wb in r.bytes() {
|
|
|
|
match wb {
|
|
|
|
Ok(b) => {
|
|
|
|
if b == delimiter_byte {
|
|
|
|
return CopyUntilResult::DelimiterFound(count);
|
|
|
|
}
|
|
|
|
match w.write(&[b]) {
|
|
|
|
Ok(c) => count += c,
|
|
|
|
Err(e) => return CopyUntilResult::WriteError(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => return CopyUntilResult::ReadError(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if count > 0 {
|
|
|
|
CopyUntilResult::DelimiterNotFound(count)
|
|
|
|
} else {
|
|
|
|
CopyUntilResult::ReaderEmpty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Copies the contents of a reader into a writer until a {tag} is found,
|
|
|
|
/// copying the tag content to a buffer and returning its size.
|
2016-05-30 08:07:44 +00:00
|
|
|
/// In addition to errors, there are three possible outputs:
|
2016-04-03 04:20:55 +00:00
|
|
|
/// - None: The reader was consumed.
|
|
|
|
/// - 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.
|
|
|
|
fn copy_and_capture<R: Read, W: Write>(r: &mut R,
|
|
|
|
w: &mut W,
|
|
|
|
tag_buffer: &mut Cursor<Vec<u8>>)
|
|
|
|
-> Option<io::Result<usize>> {
|
|
|
|
use self::CopyUntilResult::*;
|
|
|
|
|
|
|
|
// Find the opening byte.
|
|
|
|
match copy_until(r, w, b'{') {
|
|
|
|
|
|
|
|
// The end of the reader was reached without finding the opening tag.
|
|
|
|
// (either with or without having copied data to the writer)
|
|
|
|
// Return None indicating that we are done.
|
2016-05-06 21:35:53 +00:00
|
|
|
ReaderEmpty |
|
|
|
|
DelimiterNotFound(_) => None,
|
2016-04-03 04:20:55 +00:00
|
|
|
|
|
|
|
// Something went wrong.
|
|
|
|
ReadError(e) | WriteError(e) => Some(Err(e)),
|
|
|
|
|
|
|
|
// The opening byte was found.
|
|
|
|
// (either with or without having copied data to the writer)
|
|
|
|
DelimiterFound(_) => {
|
|
|
|
|
|
|
|
// Lets reset the buffer first and find out how long it is.
|
|
|
|
tag_buffer.set_position(0);
|
|
|
|
let buffer_size = tag_buffer.get_ref().len();
|
|
|
|
|
|
|
|
// Find the closing byte,limiting the reader to the length of the buffer.
|
|
|
|
let mut rb = r.take(buffer_size as u64);
|
|
|
|
match copy_until(&mut rb, tag_buffer, b'}') {
|
|
|
|
|
|
|
|
// We were already at the end of the reader.
|
|
|
|
// Return None indicating that we are done.
|
|
|
|
ReaderEmpty => None,
|
|
|
|
|
|
|
|
// The closing tag was found.
|
|
|
|
// Return the tag_length.
|
|
|
|
DelimiterFound(tag_length) => Some(Ok(tag_length)),
|
|
|
|
|
|
|
|
// The end of the reader was found without finding the closing tag.
|
|
|
|
// Write the opening byte and captured text to the writer.
|
|
|
|
// Return 0 indicating that nothing was caputred but the reader still contains data.
|
|
|
|
DelimiterNotFound(not_tag_length) => {
|
|
|
|
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)),
|
|
|
|
_ => Some(Ok(0)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadError(e) | WriteError(e) => Some(Err(e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Methods to write Parser help using templates.
|
|
|
|
impl<'a> Help<'a> {
|
|
|
|
/// Write help to stream for the parser in the format defined by the template.
|
|
|
|
///
|
|
|
|
/// Tags arg given inside curly brackets:
|
|
|
|
/// Valid tags are:
|
|
|
|
/// * `{bin}` - Binary name.
|
|
|
|
/// * `{version}` - Version number.
|
|
|
|
/// * `{author}` - Author information.
|
|
|
|
/// * `{usage}` - Automatically generated or given usage string.
|
|
|
|
/// * `{all-args}` - Help for all arguments (options, flags, positionals arguments,
|
|
|
|
/// and subcommands) including titles.
|
|
|
|
/// * `{unified}` - Unified help for options and flags.
|
|
|
|
/// * `{flags}` - Help for flags.
|
|
|
|
/// * `{options}` - Help for options.
|
|
|
|
/// * `{positionals}` - Help for positionals arguments.
|
|
|
|
/// * `{subcommands}` - Help for subcommands.
|
2016-05-02 18:25:05 +00:00
|
|
|
/// * `{after-help}` - Info to be displayed after the help message.
|
|
|
|
/// * `{before-help}` - Info to be displayed before the help message.
|
2016-04-03 04:20:55 +00:00
|
|
|
///
|
|
|
|
/// The template system is, on purpose, very simple. Therefore the tags have to writen
|
|
|
|
/// in the lowercase and without spacing.
|
|
|
|
fn write_templated_help(&mut self, parser: &Parser, template: &str) -> ClapResult<()> {
|
2016-05-30 08:07:44 +00:00
|
|
|
debugln!("fn=write_templated_help;");
|
2016-04-03 04:20:55 +00:00
|
|
|
let mut tmplr = Cursor::new(&template);
|
|
|
|
let mut tag_buf = Cursor::new(vec![0u8; 15]);
|
|
|
|
|
|
|
|
// The strategy is to copy the template from the the reader to wrapped stream
|
|
|
|
// until a tag is found. Depending on its value, the appropriate content is copied
|
|
|
|
// to the wrapped stream.
|
|
|
|
// The copy from template is then resumed, repeating this sequence until reading
|
|
|
|
// the complete template.
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let tag_length = match copy_and_capture(&mut tmplr, &mut self.writer, &mut tag_buf) {
|
|
|
|
None => return Ok(()),
|
|
|
|
Some(Err(e)) => return Err(Error::from(e)),
|
|
|
|
Some(Ok(val)) if val > 0 => val,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
2016-09-05 19:29:40 +00:00
|
|
|
debugln!("iter;tag_buf={};", unsafe {
|
|
|
|
String::from_utf8_unchecked(tag_buf.get_ref()[0..tag_length]
|
|
|
|
.iter()
|
|
|
|
.map(|&i| i)
|
|
|
|
.collect::<Vec<_>>())
|
2016-05-30 09:39:29 +00:00
|
|
|
});
|
2016-04-03 04:20:55 +00:00
|
|
|
match &tag_buf.get_ref()[0..tag_length] {
|
|
|
|
b"?" => {
|
|
|
|
try!(self.writer.write(b"Could not decode tag name"));
|
|
|
|
}
|
|
|
|
b"bin" => {
|
|
|
|
try!(self.write_bin_name(&parser));
|
|
|
|
}
|
|
|
|
b"version" => {
|
|
|
|
try!(write!(self.writer,
|
|
|
|
"{}",
|
|
|
|
parser.meta.version.unwrap_or("unknown version")));
|
|
|
|
}
|
|
|
|
b"author" => {
|
|
|
|
try!(write!(self.writer,
|
|
|
|
"{}",
|
|
|
|
parser.meta.author.unwrap_or("unknown author")));
|
|
|
|
}
|
|
|
|
b"about" => {
|
|
|
|
try!(write!(self.writer,
|
|
|
|
"{}",
|
|
|
|
parser.meta.about.unwrap_or("unknown about")));
|
|
|
|
}
|
|
|
|
b"usage" => {
|
|
|
|
try!(write!(self.writer, "{}", parser.create_usage_no_title(&[])));
|
|
|
|
}
|
|
|
|
b"all-args" => {
|
|
|
|
try!(self.write_all_args(&parser));
|
|
|
|
}
|
|
|
|
b"unified" => {
|
|
|
|
let opts_flags = parser.iter_flags()
|
2016-09-05 19:29:40 +00:00
|
|
|
.map(as_arg_trait)
|
|
|
|
.chain(parser.iter_opts().map(as_arg_trait));
|
2016-04-03 04:20:55 +00:00
|
|
|
try!(self.write_args(opts_flags));
|
|
|
|
}
|
|
|
|
b"flags" => {
|
|
|
|
try!(self.write_args(parser.iter_flags()
|
2016-09-05 19:29:40 +00:00
|
|
|
.map(as_arg_trait)));
|
2016-04-03 04:20:55 +00:00
|
|
|
}
|
|
|
|
b"options" => {
|
|
|
|
try!(self.write_args(parser.iter_opts()
|
2016-09-05 19:29:40 +00:00
|
|
|
.map(as_arg_trait)));
|
2016-04-03 04:20:55 +00:00
|
|
|
}
|
|
|
|
b"positionals" => {
|
|
|
|
try!(self.write_args(parser.iter_positionals()
|
2016-09-05 19:29:40 +00:00
|
|
|
.map(as_arg_trait)));
|
2016-04-03 04:20:55 +00:00
|
|
|
}
|
|
|
|
b"subcommands" => {
|
|
|
|
try!(self.write_subcommands(&parser));
|
|
|
|
}
|
|
|
|
b"after-help" => {
|
|
|
|
try!(write!(self.writer,
|
|
|
|
"{}",
|
|
|
|
parser.meta.more_help.unwrap_or("unknown after-help")));
|
|
|
|
}
|
2016-05-02 18:25:05 +00:00
|
|
|
b"before-help" => {
|
|
|
|
try!(write!(self.writer,
|
|
|
|
"{}",
|
|
|
|
parser.meta.pre_help.unwrap_or("unknown before-help")));
|
|
|
|
}
|
2016-04-03 04:20:55 +00:00
|
|
|
// Unknown tag, write it back.
|
2016-08-20 21:33:58 +00:00
|
|
|
r => {
|
2016-04-03 04:20:55 +00:00
|
|
|
try!(self.writer.write(b"{"));
|
|
|
|
try!(self.writer.write(r));
|
|
|
|
try!(self.writer.write(b"}"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 22:31:55 +00:00
|
|
|
fn wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) {
|
2016-09-05 19:29:40 +00:00
|
|
|
debugln!("fn=wrap_help;longest_w={},avail_chars={}",
|
|
|
|
longest_w,
|
|
|
|
avail_chars);
|
2016-08-25 22:31:55 +00:00
|
|
|
debug!("Enough space to wrap...");
|
|
|
|
if longest_w < avail_chars {
|
|
|
|
sdebugln!("Yes");
|
|
|
|
let mut prev_space = 0;
|
|
|
|
let mut j = 0;
|
|
|
|
for (idx, g) in (&*help.clone()).grapheme_indices(true) {
|
|
|
|
debugln!("iter;idx={},g={}", idx, g);
|
2016-09-07 12:38:26 +00:00
|
|
|
if g == "\n" {
|
|
|
|
debugln!("Newline found...");
|
|
|
|
debugln!("Still space...{:?}", str_width(&help[j..idx]) < avail_chars);
|
|
|
|
if str_width(&help[j..idx]) < avail_chars {
|
2016-09-07 21:53:03 +00:00
|
|
|
j = idx;
|
2016-09-07 12:38:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if g != " " {
|
|
|
|
if idx != help.len() - 1 || str_width(&help[j..idx]) < avail_chars {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
debugln!("Reached the end of the line and we're over...");
|
2016-09-07 21:53:03 +00:00
|
|
|
} else if str_width(&help[j..idx]) < avail_chars {
|
2016-09-07 12:38:26 +00:00
|
|
|
debugln!("Space found with room...");
|
2016-08-25 22:31:55 +00:00
|
|
|
prev_space = idx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
debugln!("Adding Newline...");
|
2016-09-07 21:53:03 +00:00
|
|
|
j = prev_space;
|
2016-09-07 12:38:26 +00:00
|
|
|
debugln!("prev_space={},j={}", prev_space, j);
|
2016-08-25 22:31:55 +00:00
|
|
|
debugln!("removing: {}", j);
|
|
|
|
debugln!("char at {}: {}", j, &help[j..j]);
|
|
|
|
help.remove(j);
|
2016-09-07 12:38:26 +00:00
|
|
|
help.insert(j, '\n');
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
2016-08-25 22:31:55 +00:00
|
|
|
} else {
|
|
|
|
sdebugln!("No");
|
2016-04-01 06:00:11 +00:00
|
|
|
}
|
|
|
|
}
|