Skip to content

Commit

Permalink
Merge pull request #616 from jbcrail/use-getopts-cargo-3
Browse files Browse the repository at this point in the history
Switch to external getopts cargo (part 3).
  • Loading branch information
Heather authored and Heather committed May 22, 2015
2 parents bc3eb2a + 496d588 commit ca53d2b
Show file tree
Hide file tree
Showing 22 changed files with 477 additions and 492 deletions.
55 changes: 29 additions & 26 deletions src/cp/cp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![crate_name = "cp"]
#![feature(rustc_private, path_ext)]
#![feature(path_ext)]

/*
* This file is part of the uutils coreutils package.
Expand All @@ -11,14 +11,16 @@
*/

extern crate getopts;
#[macro_use] extern crate log;

use getopts::{getopts, optflag, usage};
use std::fs;
use std::fs::{PathExt};
use std::io::{ErrorKind, Result};
use getopts::Options;
use std::fs::{self, PathExt};
use std::io::{ErrorKind, Result, Write};
use std::path::Path;

#[path = "../common/util.rs"]
#[macro_use]
mod util;

#[derive(Clone, Eq, PartialEq)]
pub enum Mode {
Copy,
Expand All @@ -30,20 +32,20 @@ static NAME: &'static str = "cp";
static VERSION: &'static str = "1.0.0";

pub fn uumain(args: Vec<String>) -> i32 {
let opts = [
optflag("h", "help", "display this help and exit"),
optflag("", "version", "output version information and exit"),
];
let matches = match getopts(&args[1..], &opts) {
let mut opts = Options::new();

opts.optflag("h", "help", "display this help and exit");
opts.optflag("", "version", "output version information and exit");

let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(e) => {
error!("error: {}", e);
show_error!("{}", e);
panic!()
},
};

let progname = &args[0];
let usage = usage("Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.", &opts);
let usage = opts.usage("Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.");
let mode = if matches.opt_present("version") {
Mode::Version
} else if matches.opt_present("help") {
Expand All @@ -54,7 +56,7 @@ pub fn uumain(args: Vec<String>) -> i32 {

match mode {
Mode::Copy => copy(matches),
Mode::Help => help(&progname, &usage),
Mode::Help => help(&usage),
Mode::Version => version(),
}

Expand All @@ -65,25 +67,26 @@ fn version() {
println!("{} {}", NAME, VERSION);
}

fn help(progname: &String, usage: &String) {
let msg = format!("Usage: {0} SOURCE DEST\n \
fn help(usage: &String) {
let msg = format!("{0} {1}\n\n\
Usage: {0} SOURCE DEST\n \
or: {0} SOURCE... DIRECTORY\n \
or: {0} -t DIRECTORY SOURCE\n\
\n\
{1}", progname, usage);
{2}", NAME, VERSION, usage);
println!("{}", msg);
}

fn copy(matches: getopts::Matches) {
let sources: Vec<String> = if matches.free.is_empty() {
error!("error: Missing SOURCE argument. Try --help.");
show_error!("Missing SOURCE argument. Try --help.");
panic!()
} else {
// All but the last argument:
matches.free[..matches.free.len() - 1].iter().map(|arg| arg.clone()).collect()
};
let dest = if matches.free.len() < 2 {
error!("error: Missing DEST argument. Try --help.");
show_error!("Missing DEST argument. Try --help.");
panic!()
} else {
// Only the last argument:
Expand All @@ -98,34 +101,34 @@ fn copy(matches: getopts::Matches) {
match err.kind() {
ErrorKind::NotFound => false,
_ => {
error!("error: {}", err);
show_error!("{}", err);
panic!()
}
}
});

if same_file {
error!("error: \"{}\" and \"{}\" are the same file",
show_error!("\"{}\" and \"{}\" are the same file",
source.display(),
dest.display());
panic!();
}

if let Err(err) = fs::copy(source, dest) {
error!("error: {}", err);
show_error!("{}", err);
panic!();
}
} else {
if !fs::metadata(dest).unwrap().is_dir() {
error!("error: TARGET must be a directory");
show_error!("TARGET must be a directory");
panic!();
}

for src in sources.iter() {
let source = Path::new(&src);

if !fs::metadata(source).unwrap().is_file() {
error!("error: \"{}\" is not a file", source.display());
show_error!("\"{}\" is not a file", source.display());
continue;
}

Expand All @@ -138,7 +141,7 @@ fn copy(matches: getopts::Matches) {
let io_result = fs::copy(source, full_dest);

if let Err(err) = io_result {
error!("error: {}", err);
show_error!("{}", err);
panic!()
}
}
Expand Down
37 changes: 19 additions & 18 deletions src/cut/cut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![crate_name = "cut"]
#![feature(collections, path_ext, rustc_private)]
#![feature(collections, path_ext)]

/*
* This file is part of the uutils coreutils package.
Expand All @@ -16,7 +16,6 @@ extern crate libc;
use std::fs::{File, PathExt};
use std::io::{stdout, stdin, BufRead, BufReader, Read, Stdout, Write};
use std::path::Path;
use getopts::{optopt, optflag, getopts, usage};

use ranges::Range;
use searcher::Searcher;
Expand Down Expand Up @@ -405,20 +404,20 @@ fn cut_files(mut filenames: Vec<String>, mode: Mode) -> i32 {
}

pub fn uumain(args: Vec<String>) -> i32 {
let opts = [
optopt("b", "bytes", "select only these bytes", "LIST"),
optopt("c", "characters", "select only these characters", "LIST"),
optopt("d", "delimiter", "use DELIM instead of TAB for field delimiter", "DELIM"),
optopt("f", "fields", "select only these fields; also print any line that contains no delimiter character, unless the -s option is specified", "LIST"),
optflag("n", "", "(ignored)"),
optflag("", "complement", "complement the set of selected bytes, characters or fields"),
optflag("s", "only-delimited", "do not print lines not containing delimiters"),
optopt("", "output-delimiter", "use STRING as the output delimiter the default is to use the input delimiter", "STRING"),
optflag("", "help", "display this help and exit"),
optflag("", "version", "output version information and exit"),
];

let matches = match getopts(&args[1..], &opts) {
let mut opts = getopts::Options::new();

opts.optopt("b", "bytes", "select only these bytes", "LIST");
opts.optopt("c", "characters", "select only these characters", "LIST");
opts.optopt("d", "delimiter", "use DELIM instead of TAB for field delimiter", "DELIM");
opts.optopt("f", "fields", "select only these fields; also print any line that contains no delimiter character, unless the -s option is specified", "LIST");
opts.optflag("n", "", "(ignored)");
opts.optflag("", "complement", "complement the set of selected bytes, characters or fields");
opts.optflag("s", "only-delimited", "do not print lines not containing delimiters");
opts.optopt("", "output-delimiter", "use STRING as the output delimiter the default is to use the input delimiter", "STRING");
opts.optflag("", "help", "display this help and exit");
opts.optflag("", "version", "output version information and exit");

let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => {
show_error!("Invalid options\n{}", f);
Expand All @@ -427,10 +426,12 @@ pub fn uumain(args: Vec<String>) -> i32 {
};

if matches.opt_present("help") {
println!("{} {}", NAME, VERSION);
println!("");
println!("Usage:");
println!(" {0} OPTION... [FILE]...", args[0]);
println!(" {0} OPTION... [FILE]...", NAME);
println!("");
println!("{}", &usage("Print selected parts of lines from each FILE to standard output.", &opts)[..]);
println!("{}", opts.usage("Print selected parts of lines from each FILE to standard output."));
println!("");
println!("Use one, and only one of -b, -c or -f. Each LIST is made up of one");
println!("range, or many ranges separated by commas. Selected input is written");
Expand Down
139 changes: 69 additions & 70 deletions src/du/du.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![crate_name = "du"]
#![feature(collections, fs_time, metadata_ext, rustc_private, std_misc)]
#![feature(fs_time, metadata_ext, std_misc)]

/*
* This file is part of the uutils coreutils package.
Expand Down Expand Up @@ -115,73 +115,72 @@ fn du(path: &PathBuf, mut my_stat: Stat, options: Arc<Options>, depth: usize) ->
}

pub fn uumain(args: Vec<String>) -> i32 {
let program = &args[0];
let opts = [
// In task
getopts::optflag("a", "all", " write counts for all files, not just directories"),
// In main
getopts::optflag("", "apparent-size", "print apparent sizes, rather than disk usage;
let mut opts = getopts::Options::new();

// In task
opts.optflag("a", "all", " write counts for all files, not just directories");
// In main
opts.optflag("", "apparent-size", "print apparent sizes, rather than disk usage;
although the apparent size is usually smaller, it may be larger due to holes
in ('sparse') files, internal fragmentation, indirect blocks, and the like"),
// In main
getopts::optopt("B", "block-size", "scale sizes by SIZE before printing them.
in ('sparse') files, internal fragmentation, indirect blocks, and the like");
// In main
opts.optopt("B", "block-size", "scale sizes by SIZE before printing them.
E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below.",
"SIZE"),
// In main
getopts::optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'"),
// In main
getopts::optflag("c", "total", "produce a grand total"),
// In task
// getopts::optflag("D", "dereference-args", "dereference only symlinks that are listed
// on the command line"),
// In main
// getopts::optopt("", "files0-from", "summarize disk usage of the NUL-terminated file
// names specified in file F;
// If F is - then read names from standard input", "F"),
// // In task
// getopts::optflag("H", "", "equivalent to --dereference-args (-D)"),
// In main
getopts::optflag("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)"),
// In main
getopts::optflag("", "si", "like -h, but use powers of 1000 not 1024"),
// In main
getopts::optflag("k", "", "like --block-size=1K"),
// In task
getopts::optflag("l", "count-links", "count sizes many times if hard linked"),
// // In main
getopts::optflag("m", "", "like --block-size=1M"),
// // In task
// getopts::optflag("L", "dereference", "dereference all symbolic links"),
// // In task
// getopts::optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"),
// // In main
getopts::optflag("0", "null", "end each output line with 0 byte rather than newline"),
// In main
getopts::optflag("S", "separate-dirs", "do not include size of subdirectories"),
// In main
getopts::optflag("s", "summarize", "display only a total for each argument"),
// // In task
// getopts::optflag("x", "one-file-system", "skip directories on different file systems"),
// // In task
// getopts::optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"),
// // In task
// getopts::optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"),
// In main
getopts::optopt("d", "max-depth", "print the total for a directory (or file, with --all)
"SIZE");
// In main
opts.optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'");
// In main
opts.optflag("c", "total", "produce a grand total");
// In task
// opts.optflag("D", "dereference-args", "dereference only symlinks that are listed
// on the command line"),
// In main
// opts.optopt("", "files0-from", "summarize disk usage of the NUL-terminated file
// names specified in file F;
// If F is - then read names from standard input", "F"),
// // In task
// opts.optflag("H", "", "equivalent to --dereference-args (-D)"),
// In main
opts.optflag("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)");
// In main
opts.optflag("", "si", "like -h, but use powers of 1000 not 1024");
// In main
opts.optflag("k", "", "like --block-size=1K");
// In task
opts.optflag("l", "count-links", "count sizes many times if hard linked");
// // In main
opts.optflag("m", "", "like --block-size=1M");
// // In task
// opts.optflag("L", "dereference", "dereference all symbolic links"),
// // In task
// opts.optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"),
// // In main
opts.optflag("0", "null", "end each output line with 0 byte rather than newline");
// In main
opts.optflag("S", "separate-dirs", "do not include size of subdirectories");
// In main
opts.optflag("s", "summarize", "display only a total for each argument");
// // In task
// opts.optflag("x", "one-file-system", "skip directories on different file systems"),
// // In task
// opts.optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"),
// // In task
// opts.optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"),
// In main
opts.optopt("d", "max-depth", "print the total for a directory (or file, with --all)
only if it is N or fewer levels below the command
line argument; --max-depth=0 is the same as --summarize", "N"),
// In main
getopts::optflagopt("", "time", "show time of the last modification of any file in the
line argument; --max-depth=0 is the same as --summarize", "N");
// In main
opts.optflagopt("", "time", "show time of the last modification of any file in the
directory, or any of its subdirectories. If WORD is given, show time as WORD instead of modification time:
atime, access, use, ctime or status", "WORD"),
// In main
getopts::optopt("", "time-style", "show times using style STYLE:
full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", "STYLE"),
getopts::optflag("", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit"),
];

let matches = match getopts::getopts(args.tail(), &opts) {
atime, access, use, ctime or status", "WORD");
// In main
opts.optopt("", "time-style", "show times using style STYLE:
full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", "STYLE");
opts.optflag("", "help", "display this help and exit");
opts.optflag("V", "version", "output version information and exit");

let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => {
show_error!("Invalid options\n{}", f);
Expand All @@ -206,12 +205,12 @@ POSIXLY_CORRECT is set).
SIZE is an integer and optional unit (example: 10M is 10*1024*1024).
Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (pow‐
ers of 1000).",
program = program,
program = NAME,
version = VERSION,
usage = getopts::usage("Summarize disk usage of each FILE, recursively for directories.", &opts));
usage = opts.usage("Summarize disk usage of each FILE, recursively for directories."));
return 0;
} else if matches.opt_present("version") {
println!("{} version: {}", program, VERSION);
println!("{} version: {}", NAME, VERSION);
return 0;
}

Expand All @@ -233,7 +232,7 @@ ers of 1000).",

let options = Options {
all: matches.opt_present("all"),
program_name: program.to_string(),
program_name: NAME.to_string(),
max_depth: max_depth,
total: matches.opt_present("total"),
separate_dirs: matches.opt_present("S"),
Expand Down Expand Up @@ -320,7 +319,7 @@ Valid arguments are:
- 'full-iso'
- 'long-iso'
- 'iso'
Try '{} --help' for more information.", s, program);
Try '{} --help' for more information.", s, NAME);
return 1;
}
}
Expand Down Expand Up @@ -358,7 +357,7 @@ Try '{} --help' for more information.", s, program);
show_error!("invalid argument 'modified' for '--time'
Valid arguments are:
- 'accessed', 'created', 'modified'
Try '{} --help' for more information.", program);
Try '{} --help' for more information.", NAME);
return 1;
}
},
Expand Down
Loading

0 comments on commit ca53d2b

Please sign in to comment.