mirror of
https://github.com/uutils/coreutils
synced 2024-07-23 19:04:18 +00:00
fold: move to clap, add tests (#2015)
This commit is contained in:
parent
f498a970d9
commit
c965effe07
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1861,6 +1861,7 @@ dependencies = [
|
|||
name = "uu_fold"
|
||||
version = "0.0.6"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"uucore",
|
||||
"uucore_procs",
|
||||
]
|
||||
|
|
|
@ -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" }
|
||||
|
||||
|
|
|
@ -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::<usize>() {
|
||||
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
|
||||
|
|
|
@ -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 ");
|
||||
}
|
||||
|
|
1
tests/fixtures/fold/space_separated_words.txt
vendored
Normal file
1
tests/fixtures/fold/space_separated_words.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
test1 test2 test3 test4 test5 test6
|
Loading…
Reference in a new issue