od: fix command-line parsing of file names

This commit is contained in:
Wim Hueskes 2016-08-13 22:51:21 +02:00
parent e8eab8d3e8
commit 9e33c3a48c
5 changed files with 23 additions and 5 deletions

View file

@ -134,12 +134,11 @@ pub fn uumain(args: Vec<String>) -> i32 {
}
};
// Gather up file names - args which don't start with '-'
let mut inputs = args[1..]
// Gather up file names
let mut inputs = matches.free
.iter()
.filter_map(|w| match w as &str {
"--" => Some(InputSource::Stdin),
o if o.starts_with("-") => None,
"-" => Some(InputSource::Stdin),
x => Some(InputSource::FileName(x)),
})
.collect::<Vec<_>>();

1
tests/fixtures/od/-f vendored Normal file
View file

@ -0,0 +1 @@
minus lowercase f

1
tests/fixtures/od/c vendored Normal file
View file

@ -0,0 +1 @@
lowercase c

1
tests/fixtures/od/x vendored Normal file
View file

@ -0,0 +1 @@
lowercase x

View file

@ -117,7 +117,7 @@ fn test_from_mixed() {
}
}
let result = new_ucmd!().arg("--endian=little").arg(file1.as_os_str()).arg("--").arg(file3.as_os_str()).run_piped_stdin(data2.as_bytes());
let result = new_ucmd!().arg("--endian=little").arg(file1.as_os_str()).arg("-").arg(file3.as_os_str()).run_piped_stdin(data2.as_bytes());
assert_empty_stderr!(result);
assert!(result.success);
@ -548,3 +548,19 @@ fn test_ascii_dump(){
0000026
"));
}
#[test]
fn test_filename_parsing(){
// files "a" and "x" both exists, but are no filenames in the commandline below
// "-f" must be treated as a filename, it contains the text: minus lowercase f
// so "-f" should not be interpreted as a formatting option.
let result = new_ucmd!().arg("--format").arg("a").arg("-A").arg("x").arg("--").arg("-f").run();
assert_empty_stderr!(result);
assert!(result.success);
assert_eq!(result.stdout, unindent("
000000 m i n u s sp l o w e r c a s e sp
000010 f nl
000012
"));
}