Skip to content

Commit 05abb53

Browse files
committed
fix: fixes bug where args are printed out of order with templates
1 parent 638bf11 commit 05abb53

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/app/help.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub struct Help<'a> {
8888
impl<'a> Help<'a> {
8989
/// Create a new `Help` instance.
9090
pub fn new(w: &'a mut Write, next_line_help: bool, hide_pv: bool, color: bool) -> Self {
91+
debugln!("fn=Help::new;");
9192
Help {
9293
writer: w,
9394
next_line_help: next_line_help,
@@ -100,12 +101,14 @@ impl<'a> Help<'a> {
100101
/// Reads help settings from an App
101102
/// and write its help to the wrapped stream.
102103
pub fn write_app_help(w: &'a mut Write, app: &App) -> ClapResult<()> {
104+
debugln!("fn=Help::write_app_help;");
103105
Self::write_parser_help(w, &app.p)
104106
}
105107

106108
/// Reads help settings from a Parser
107109
/// and write its help to the wrapped stream.
108110
pub fn write_parser_help(w: &'a mut Write, parser: &Parser) -> ClapResult<()> {
111+
debugln!("fn=Help::write_parser_help;");
109112
let nlh = parser.is_set(AppSettings::NextLineHelp);
110113
let hide_v = parser.is_set(AppSettings::HidePossibleValuesInHelp);
111114
let color = parser.is_set(AppSettings::ColoredHelp);
@@ -114,6 +117,7 @@ impl<'a> Help<'a> {
114117

115118
/// Writes the parser help to the wrapped stream.
116119
pub fn write_help(&mut self, parser: &Parser) -> ClapResult<()> {
120+
debugln!("fn=Help::write_help;");
117121
if let Some(h) = parser.meta.help_str {
118122
try!(write!(self.writer, "{}", h).map_err(Error::from));
119123
} else if let Some(ref tmpl) = parser.meta.template {
@@ -131,6 +135,7 @@ impl<'a> Help<'a> {
131135
fn write_args_unsorted<'b: 'd, 'c: 'd, 'd, I: 'd>(&mut self, args: I) -> io::Result<()>
132136
where I: Iterator<Item = &'d ArgWithOrder<'b, 'c>>
133137
{
138+
debugln!("fn=write_args_unsorted;");
134139
let mut longest = 0;
135140
let mut arg_v = Vec::with_capacity(10);
136141
for arg in args.filter(|arg| {
@@ -159,6 +164,7 @@ impl<'a> Help<'a> {
159164
fn write_args<'b: 'd, 'c: 'd, 'd, I: 'd>(&mut self, args: I) -> io::Result<()>
160165
where I: Iterator<Item = &'d ArgWithOrder<'b, 'c>>
161166
{
167+
debugln!("fn=write_args;");
162168
let mut longest = 0;
163169
let mut ord_m = VecMap::new();
164170
for arg in args.filter(|arg| {
@@ -179,7 +185,7 @@ impl<'a> Help<'a> {
179185
try!(self.writer.write(b"\n"));
180186
} else {
181187
first = false;
182-
};
188+
}
183189
try!(self.write_arg(arg.as_base(), longest));
184190
}
185191
}
@@ -191,7 +197,7 @@ impl<'a> Help<'a> {
191197
arg: &ArgWithDisplay<'b, 'c>,
192198
longest: usize)
193199
-> io::Result<()> {
194-
debugln!("fn=write_to;");
200+
debugln!("fn=write_arg;");
195201
try!(self.short(arg));
196202
try!(self.long(arg, longest));
197203
try!(self.val(arg, longest));
@@ -505,6 +511,7 @@ impl<'a> Help<'a> {
505511

506512
/// Writes help for subcommands of a Parser Object to the wrapped stream.
507513
fn write_subcommands(&mut self, parser: &Parser) -> io::Result<()> {
514+
debugln!("exec=write_subcommands;");
508515
let mut longest = 0;
509516

510517
let mut ord_m = VecMap::new();
@@ -518,10 +525,12 @@ impl<'a> Help<'a> {
518525
for (_, btm) in ord_m.into_iter() {
519526
for (_, sc) in btm.into_iter() {
520527
if !first {
528+
debugln!("Writing newline...");
521529
try!(self.writer.write(b"\n"));
522530
} else {
523531
first = false;
524532
}
533+
debugln!("Writing sc...{}", sc);
525534
try!(self.write_arg(sc, longest));
526535
}
527536
}
@@ -551,6 +560,7 @@ impl<'a> Help<'a> {
551560

552561
/// Writes default help for a Parser Object to the wrapped stream.
553562
pub fn write_default_help(&mut self, parser: &Parser) -> ClapResult<()> {
563+
debugln!("fn=write_default_help;");
554564
if let Some(h) = parser.meta.pre_help {
555565
try!(write!(self.writer, "{}", h));
556566
try!(self.writer.write(b"\n\n"));
@@ -720,6 +730,7 @@ impl<'a> Help<'a> {
720730
/// The template system is, on purpose, very simple. Therefore the tags have to writen
721731
/// in the lowercase and without spacing.
722732
fn write_templated_help(&mut self, parser: &Parser, template: &str) -> ClapResult<()> {
733+
debugln!("fn=write_templated_help;");
723734
let mut tmplr = Cursor::new(&template);
724735
let mut tag_buf = Cursor::new(vec![0u8; 15]);
725736

@@ -737,6 +748,12 @@ impl<'a> Help<'a> {
737748
_ => continue,
738749
};
739750

751+
debugln!("iter;tag_buf={};", unsafe {
752+
String::from_utf8_unchecked(tag_buf.get_ref()[0..tag_length]
753+
.iter()
754+
.map(|&i|i)
755+
.collect::<Vec<_>>())
756+
});
740757
match &tag_buf.get_ref()[0..tag_length] {
741758
b"?" => {
742759
try!(self.writer.write(b"Could not decode tag name"));
@@ -780,8 +797,8 @@ impl<'a> Help<'a> {
780797
.map(as_arg_trait)));
781798
}
782799
b"positionals" => {
783-
try!(self.write_args(parser.iter_positionals()
784-
.map(as_arg_trait)));
800+
try!(self.write_args_unsorted(parser.iter_positionals()
801+
.map(as_arg_trait)));
785802
}
786803
b"subcommands" => {
787804
try!(self.write_subcommands(&parser));
@@ -803,7 +820,6 @@ impl<'a> Help<'a> {
803820
try!(self.writer.write(b"}"));
804821
}
805822
}
806-
807823
}
808824
}
809825
}

0 commit comments

Comments
 (0)