mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
fold: move to clap, add tests (#2015)
This commit is contained in:
parent
f498a970d9
commit
c965effe07
5 changed files with 63 additions and 26 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1861,6 +1861,7 @@ dependencies = [
|
||||||
name = "uu_fold"
|
name = "uu_fold"
|
||||||
version = "0.0.6"
|
version = "0.0.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"clap",
|
||||||
"uucore",
|
"uucore",
|
||||||
"uucore_procs",
|
"uucore_procs",
|
||||||
]
|
]
|
||||||
|
|
|
@ -15,6 +15,7 @@ edition = "2018"
|
||||||
path = "src/fold.rs"
|
path = "src/fold.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = "2.33"
|
||||||
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
|
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
||||||
|
|
|
@ -10,48 +10,71 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{stdin, BufRead, BufReader, Read};
|
use std::io::{stdin, BufRead, BufReader, Read};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
const TAB_WIDTH: usize = 8;
|
const TAB_WIDTH: usize = 8;
|
||||||
|
|
||||||
|
static NAME: &str = "fold";
|
||||||
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
static SYNTAX: &str = "[OPTION]... [FILE]...";
|
static SYNTAX: &str = "[OPTION]... [FILE]...";
|
||||||
static SUMMARY: &str = "Writes each file (or standard input if no files are given)
|
static SUMMARY: &str = "Writes each file (or standard input if no files are given)
|
||||||
to standard output whilst breaking long lines";
|
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 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let args = args.collect_str();
|
let args = args.collect_str();
|
||||||
|
|
||||||
let (args, obs_width) = handle_obsolete(&args[..]);
|
let (args, obs_width) = handle_obsolete(&args[..]);
|
||||||
let matches = app!(SYNTAX, SUMMARY, LONG_HELP)
|
let matches = App::new(executable!())
|
||||||
.optflag(
|
.name(NAME)
|
||||||
"b",
|
.version(VERSION)
|
||||||
"bytes",
|
.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 \
|
"count using bytes rather than columns (meaning control characters \
|
||||||
such as newline are not treated specially)",
|
such as newline are not treated specially)",
|
||||||
)
|
)
|
||||||
.optflag(
|
.takes_value(false),
|
||||||
"s",
|
|
||||||
"spaces",
|
|
||||||
"break lines at word boundaries rather than a hard cut-off",
|
|
||||||
)
|
)
|
||||||
.optopt(
|
.arg(
|
||||||
"w",
|
Arg::with_name(options::SPACES)
|
||||||
"width",
|
.long(options::SPACES)
|
||||||
"set WIDTH as the maximum line width rather than 80",
|
.short("s")
|
||||||
"WIDTH",
|
.help("break lines at word boundaries rather than a hard cut-off")
|
||||||
|
.takes_value(false),
|
||||||
)
|
)
|
||||||
.parse(args);
|
.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),
|
||||||
|
)
|
||||||
|
.arg(Arg::with_name(options::FILE).hidden(true).multiple(true))
|
||||||
|
.get_matches_from(args.clone());
|
||||||
|
|
||||||
let bytes = matches.opt_present("b");
|
let bytes = matches.is_present(options::BYTES);
|
||||||
let spaces = matches.opt_present("s");
|
let spaces = matches.is_present(options::SPACES);
|
||||||
let poss_width = if matches.opt_present("w") {
|
let poss_width = match matches.value_of(options::WIDTH) {
|
||||||
matches.opt_str("w")
|
Some(v) => Some(v.to_owned()),
|
||||||
} else {
|
None => obs_width,
|
||||||
obs_width
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let width = match poss_width {
|
let width = match poss_width {
|
||||||
Some(inp_width) => match inp_width.parse::<usize>() {
|
Some(inp_width) => match inp_width.parse::<usize>() {
|
||||||
Ok(width) => width,
|
Ok(width) => width,
|
||||||
|
@ -59,11 +82,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
},
|
},
|
||||||
None => 80,
|
None => 80,
|
||||||
};
|
};
|
||||||
let files = if matches.free.is_empty() {
|
|
||||||
vec!["-".to_owned()]
|
let files = match matches.values_of(options::FILE) {
|
||||||
} else {
|
Some(v) => v.map(|v| v.to_owned()).collect(),
|
||||||
matches.free
|
None => vec!["-".to_owned()],
|
||||||
};
|
};
|
||||||
|
|
||||||
fold(files, bytes, spaces, width);
|
fold(files, bytes, spaces, width);
|
||||||
|
|
||||||
0
|
0
|
||||||
|
|
|
@ -397,3 +397,13 @@ fn test_bytewise_fold_at_word_boundary_only_whitespace_preserve_final_newline()
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_is(" \n \n");
|
.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…
Add table
Add a link
Reference in a new issue