diff --git a/Cargo.lock b/Cargo.lock index ce4457231..f65ba3dff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1861,6 +1861,7 @@ dependencies = [ name = "uu_fold" version = "0.0.6" dependencies = [ + "clap", "uucore", "uucore_procs", ] diff --git a/src/uu/fold/Cargo.toml b/src/uu/fold/Cargo.toml index f99abc691..c5578384e 100644 --- a/src/uu/fold/Cargo.toml +++ b/src/uu/fold/Cargo.toml @@ -15,6 +15,7 @@ edition = "2018" path = "src/fold.rs" [dependencies] +clap = "2.33" uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } diff --git a/src/uu/fold/src/fold.rs b/src/uu/fold/src/fold.rs index fac0ff28e..e19e9db19 100644 --- a/src/uu/fold/src/fold.rs +++ b/src/uu/fold/src/fold.rs @@ -10,48 +10,71 @@ #[macro_use] extern crate uucore; +use clap::{App, Arg}; use std::fs::File; use std::io::{stdin, BufRead, BufReader, Read}; use std::path::Path; const TAB_WIDTH: usize = 8; +static NAME: &str = "fold"; +static VERSION: &str = env!("CARGO_PKG_VERSION"); static SYNTAX: &str = "[OPTION]... [FILE]..."; static SUMMARY: &str = "Writes each file (or standard input if no files are given) to standard output whilst breaking long lines"; -static LONG_HELP: &str = ""; + +mod options { + pub const BYTES: &str = "bytes"; + pub const SPACES: &str = "spaces"; + pub const WIDTH: &str = "width"; + pub const FILE: &str = "file"; +} pub fn uumain(args: impl uucore::Args) -> i32 { let args = args.collect_str(); let (args, obs_width) = handle_obsolete(&args[..]); - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optflag( - "b", - "bytes", - "count using bytes rather than columns (meaning control characters \ + let matches = App::new(executable!()) + .name(NAME) + .version(VERSION) + .usage(SYNTAX) + .about(SUMMARY) + .arg( + Arg::with_name(options::BYTES) + .long(options::BYTES) + .short("b") + .help( + "count using bytes rather than columns (meaning control characters \ such as newline are not treated specially)", + ) + .takes_value(false), ) - .optflag( - "s", - "spaces", - "break lines at word boundaries rather than a hard cut-off", + .arg( + Arg::with_name(options::SPACES) + .long(options::SPACES) + .short("s") + .help("break lines at word boundaries rather than a hard cut-off") + .takes_value(false), ) - .optopt( - "w", - "width", - "set WIDTH as the maximum line width rather than 80", - "WIDTH", + .arg( + Arg::with_name(options::WIDTH) + .long(options::WIDTH) + .short("w") + .help("set WIDTH as the maximum line width rather than 80") + .value_name("WIDTH") + .allow_hyphen_values(true) + .takes_value(true), ) - .parse(args); + .arg(Arg::with_name(options::FILE).hidden(true).multiple(true)) + .get_matches_from(args.clone()); - let bytes = matches.opt_present("b"); - let spaces = matches.opt_present("s"); - let poss_width = if matches.opt_present("w") { - matches.opt_str("w") - } else { - obs_width + let bytes = matches.is_present(options::BYTES); + let spaces = matches.is_present(options::SPACES); + let poss_width = match matches.value_of(options::WIDTH) { + Some(v) => Some(v.to_owned()), + None => obs_width, }; + let width = match poss_width { Some(inp_width) => match inp_width.parse::() { Ok(width) => width, @@ -59,11 +82,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 { }, None => 80, }; - let files = if matches.free.is_empty() { - vec!["-".to_owned()] - } else { - matches.free + + let files = match matches.values_of(options::FILE) { + Some(v) => v.map(|v| v.to_owned()).collect(), + None => vec!["-".to_owned()], }; + fold(files, bytes, spaces, width); 0 diff --git a/tests/by-util/test_fold.rs b/tests/by-util/test_fold.rs index cc92c8ff3..bdc5e2246 100644 --- a/tests/by-util/test_fold.rs +++ b/tests/by-util/test_fold.rs @@ -397,3 +397,13 @@ fn test_bytewise_fold_at_word_boundary_only_whitespace_preserve_final_newline() .succeeds() .stdout_is(" \n \n"); } + +#[test] +fn test_obsolete_syntax() { + new_ucmd!() + .arg("-5") + .arg("-s") + .arg("space_separated_words.txt") + .succeeds() + .stdout_is("test1\n \ntest2\n \ntest3\n \ntest4\n \ntest5\n \ntest6\n "); +} diff --git a/tests/fixtures/fold/space_separated_words.txt b/tests/fixtures/fold/space_separated_words.txt new file mode 100644 index 000000000..13b980632 --- /dev/null +++ b/tests/fixtures/fold/space_separated_words.txt @@ -0,0 +1 @@ +test1 test2 test3 test4 test5 test6 \ No newline at end of file