1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Merge pull request #1821 from deantvv/expand-clap

expand: replace getopts with clap
This commit is contained in:
Sylvestre Ledru 2021-03-17 22:01:31 +01:00 committed by GitHub
commit a690ffde41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 33 deletions

View file

@ -15,7 +15,7 @@ edition = "2018"
path = "src/expand.rs" path = "src/expand.rs"
[dependencies] [dependencies]
getopts = "0.2.18" clap = "2.33"
unicode-width = "0.1.5" unicode-width = "0.1.5"
uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } uucore = { version=">=0.0.7", 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" }

View file

@ -12,19 +12,32 @@
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use clap::{App, Arg, ArgMatches};
use std::fs::File; use std::fs::File;
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
use std::iter::repeat; use std::iter::repeat;
use std::str::from_utf8; use std::str::from_utf8;
use unicode_width::UnicodeWidthChar; use unicode_width::UnicodeWidthChar;
static SYNTAX: &str = "[OPTION]... [FILE]..."; static VERSION: &str = env!("CARGO_PKG_VERSION");
static SUMMARY: &str = "Convert tabs in each FILE to spaces, writing to standard output. static ABOUT: &str = "Convert tabs in each FILE to spaces, writing to standard output.
With no FILE, or when FILE is -, read standard input."; With no FILE, or when FILE is -, read standard input.";
pub mod options {
pub static TABS: &str = "tabs";
pub static INITIAL: &str = "initial";
pub static NO_UTF8: &str = "no-utf8";
pub static FILES: &str = "FILES";
}
static LONG_HELP: &str = ""; static LONG_HELP: &str = "";
static DEFAULT_TABSTOP: usize = 8; static DEFAULT_TABSTOP: usize = 8;
fn get_usage() -> String {
format!("{0} [OPTION]... [FILE]...", executable!())
}
fn tabstops_parse(s: String) -> Vec<usize> { fn tabstops_parse(s: String) -> Vec<usize> {
let words = s.split(','); let words = s.split(',');
@ -58,14 +71,14 @@ struct Options {
} }
impl Options { impl Options {
fn new(matches: getopts::Matches) -> Options { fn new(matches: &ArgMatches) -> Options {
let tabstops = match matches.opt_str("t") { let tabstops = match matches.value_of(options::TABS) {
Some(s) => tabstops_parse(s.to_string()),
None => vec![DEFAULT_TABSTOP], None => vec![DEFAULT_TABSTOP],
Some(s) => tabstops_parse(s),
}; };
let iflag = matches.opt_present("i"); let iflag = matches.is_present(options::INITIAL);
let uflag = !matches.opt_present("U"); let uflag = !matches.is_present(options::NO_UTF8);
// avoid allocations when dumping out long sequences of spaces // avoid allocations when dumping out long sequences of spaces
// by precomputing the longest string of spaces we will ever need // by precomputing the longest string of spaces we will ever need
@ -80,10 +93,9 @@ impl Options {
.unwrap(); // length of tabstops is guaranteed >= 1 .unwrap(); // length of tabstops is guaranteed >= 1
let tspaces = repeat(' ').take(nspaces).collect(); let tspaces = repeat(' ').take(nspaces).collect();
let files = if matches.free.is_empty() { let files: Vec<String> = match matches.values_of(options::FILES) {
vec!["-".to_owned()] Some(s) => s.map(|v| v.to_string()).collect(),
} else { None => vec!["-".to_owned()],
matches.free
}; };
Options { Options {
@ -97,31 +109,40 @@ impl Options {
} }
pub fn uumain(args: impl uucore::Args) -> i32 { pub fn uumain(args: impl uucore::Args) -> i32 {
let args = args.collect_str(); let usage = get_usage();
let matches = App::new(executable!())
let matches = app!(SYNTAX, SUMMARY, LONG_HELP) .version(VERSION)
.optflag("i", "initial", "do not convert tabs after non blanks") .about(ABOUT)
.optopt( .usage(&usage[..])
"t", .after_help(LONG_HELP)
"tabs", .arg(
"have tabs NUMBER characters apart, not 8", Arg::with_name(options::INITIAL)
"NUMBER", .long(options::INITIAL)
.short("i")
.help("do not convert tabs after non blanks"),
) )
.optopt( .arg(
"t", Arg::with_name(options::TABS)
"tabs", .long(options::TABS)
"use comma separated list of explicit tab positions", .short("t")
"LIST", .value_name("N, LIST")
.takes_value(true)
.help("have tabs N characters apart, not 8 or use comma separated list of explicit tab positions"),
) )
.optflag( .arg(
"U", Arg::with_name(options::NO_UTF8)
"no-utf8", .long(options::NO_UTF8)
"interpret input file as 8-bit ASCII rather than UTF-8", .short("U")
.help("interpret input file as 8-bit ASCII rather than UTF-8"),
).arg(
Arg::with_name(options::FILES)
.multiple(true)
.hidden(true)
.takes_value(true)
) )
.parse(args); .get_matches_from(args);
expand(Options::new(matches));
expand(Options::new(&matches));
0 0
} }

View file

@ -46,3 +46,13 @@ fn test_with_space() {
assert!(result.success); assert!(result.success);
assert!(result.stdout.contains(" return")); assert!(result.stdout.contains(" return"));
} }
#[test]
fn test_with_multiple_files() {
let (_, mut ucmd) = at_and_ucmd!();
let result = ucmd.arg("with-spaces.txt").arg("with-tab.txt").run();
assert!(result.success);
assert!(result.stdout.contains(" return"));
assert!(result.stdout.contains(" "));
}