1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

impl: dryer use of coreopts

This commit is contained in:
Nathan Ross 2016-08-20 08:49:58 -04:00
parent ee3aaa017f
commit edc3bf7c08
3 changed files with 35 additions and 38 deletions

View file

@ -42,38 +42,35 @@ pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = new_coreopts!(SYNTAX, SUMMARY, ""); let mut opts = new_coreopts!(SYNTAX, SUMMARY, "");
opts.optflag("c", opts.optflag("c",
"changes", "changes",
"like verbose but report only when a change is made"); "like verbose but report only when a change is made")
opts.optflag("f", "silent", ""); .optflag("f", "silent", "")
opts.optflag("", "quiet", "suppress most error messages"); .optflag("", "quiet", "suppress most error messages")
opts.optflag("v", .optflag("v",
"verbose", "verbose",
"output a diagnostic for every file processed"); "output a diagnostic for every file processed")
opts.optflag("", "dereference", "affect the referent of each symbolic link (this is the default), rather than the symbolic link itself"); .optflag("", "dereference", "affect the referent of each symbolic link (this is the default), rather than the symbolic link itself")
opts.optflag("h", "no-dereference", "affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)"); .optflag("h", "no-dereference", "affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)")
opts.optopt("", "from", "change the owner and/or group of each file only if its current owner and/or group match those specified here. Either may be omitted, in which case a match is not required for the omitted attribute", "CURRENT_OWNER:CURRENT_GROUP"); .optopt("", "from", "change the owner and/or group of each file only if its current owner and/or group match those specified here. Either may be omitted, in which case a match is not required for the omitted attribute", "CURRENT_OWNER:CURRENT_GROUP")
opts.optopt("", .optopt("",
"reference", "reference",
"use RFILE's owner and group rather than specifying OWNER:GROUP values", "use RFILE's owner and group rather than specifying OWNER:GROUP values",
"RFILE"); "RFILE")
.optflag("",
opts.optflag("",
"no-preserve-root", "no-preserve-root",
"do not treat '/' specially (the default)"); "do not treat '/' specially (the default)")
opts.optflag("", "preserve-root", "fail to operate recursively on '/'"); .optflag("", "preserve-root", "fail to operate recursively on '/'")
opts.optflag("R", .optflag("R",
"recursive", "recursive",
"operate on files and directories recursively"); "operate on files and directories recursively")
opts.optflag("H", .optflag("H",
"", "",
"if a command line argument is a symbolic link to a directory, traverse it"); "if a command line argument is a symbolic link to a directory, traverse it")
opts.optflag("L", .optflag("L",
"", "",
"traverse every symbolic link to a directory encountered"); "traverse every symbolic link to a directory encountered")
opts.optflag("P", "", "do not traverse any symbolic links (default)"); .optflag("P", "", "do not traverse any symbolic links (default)");
let matches = opts.parse(args.clone());
let mut bit_flag = FTS_PHYSICAL; let mut bit_flag = FTS_PHYSICAL;
let mut preserve_root = false; let mut preserve_root = false;
@ -100,6 +97,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
} }
} }
let matches = opts.parse(args);
let recursive = matches.opt_present("recursive"); let recursive = matches.opt_present("recursive");
if recursive { if recursive {
if bit_flag == FTS_PHYSICAL { if bit_flag == FTS_PHYSICAL {

View file

@ -291,7 +291,7 @@ fn cut_fields<R: Read>(reader: R, ranges: &[Range], opts: &FieldOptions) -> i32
match opts.out_delimeter { match opts.out_delimeter {
Some(ref o_delim) => { Some(ref o_delim) => {
return cut_fields_delimiter(reader, ranges, &opts.delimiter, return cut_fields_delimiter(reader, ranges, &opts.delimiter,
opts.only_delimited, newline_char, o_delim); opts.only_delimited, newline_char, o_delim)
} }
None => () None => ()
} }
@ -417,18 +417,17 @@ fn cut_files(mut filenames: Vec<String>, mode: Mode) -> i32 {
} }
pub fn uumain(args: Vec<String>) -> i32 { pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP); let matches = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP)
opts.optopt("b", "bytes", "filter byte columns from the input source", "sequence"); .optopt("b", "bytes", "filter byte columns from the input source", "sequence")
opts.optopt("c", "characters", "alias for character mode", "sequence"); .optopt("c", "characters", "alias for character mode", "sequence")
opts.optopt("d", "delimiter", "specify the delimiter character that separates fields in the input source. Defaults to Tab.", "delimiter"); .optopt("d", "delimiter", "specify the delimiter character that separates fields in the input source. Defaults to Tab.", "delimiter")
opts.optopt("f", "fields", "filter field columns from the input source", "sequence"); .optopt("f", "fields", "filter field columns from the input source", "sequence")
opts.optflag("n", "", "legacy option - has no effect."); .optflag("n", "", "legacy option - has no effect.")
opts.optflag("", "complement", "invert the filter - instead of displaying only the filtered columns, display all but those columns"); .optflag("", "complement", "invert the filter - instead of displaying only the filtered columns, display all but those columns")
opts.optflag("s", "only-delimited", "in field mode, only print lines which contain the delimiter"); .optflag("s", "only-delimited", "in field mode, only print lines which contain the delimiter")
opts.optflag("z", "zero-terminated", "instead of filtering columns based on line, filter columns based on \\0 (NULL character)"); .optflag("z", "zero-terminated", "instead of filtering columns based on line, filter columns based on \\0 (NULL character)")
opts.optopt("", "output-delimiter", "in field mode, replace the delimiter in output lines with this option's argument", "new delimiter"); .optopt("", "output-delimiter", "in field mode, replace the delimiter in output lines with this option's argument", "new delimiter")
let matches = opts.parse(args); .parse(args);
let complement = matches.opt_present("complement"); let complement = matches.opt_present("complement");
let mode_parse = match (matches.opt_str("bytes"), let mode_parse = match (matches.opt_str("bytes"),

View file

@ -19,8 +19,8 @@ static SYNTAX: &'static str = "[user]";
static SUMMARY: &'static str = "display current group names"; static SUMMARY: &'static str = "display current group names";
pub fn uumain(args: Vec<String>) -> i32 { pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = new_coreopts!(SYNTAX, SUMMARY, ""); let matches = new_coreopts!(SYNTAX, SUMMARY, "")
let matches = opts.parse(args); .parse(args);
if matches.free.is_empty() { if matches.free.is_empty() {
println!("{}", get_groups().unwrap().iter().map(|&g| gid2grp(g).unwrap()).collect::<Vec<_>>().join(" ")); println!("{}", get_groups().unwrap().iter().map(|&g| gid2grp(g).unwrap()).collect::<Vec<_>>().join(" "));