diff --git a/Cargo.lock b/Cargo.lock index 931470178..1ab7b0fdf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2096,7 +2096,7 @@ dependencies = [ name = "uu_sync" version = "0.0.1" dependencies = [ - "getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "uucore 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)", "uucore_procs 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)", diff --git a/src/uu/sync/Cargo.toml b/src/uu/sync/Cargo.toml index d6dab2788..746554528 100644 --- a/src/uu/sync/Cargo.toml +++ b/src/uu/sync/Cargo.toml @@ -15,7 +15,7 @@ edition = "2018" path = "src/sync.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "0.2.42" uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary", features=["wide"] } uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" } diff --git a/src/uu/sync/src/sync.rs b/src/uu/sync/src/sync.rs index bb6d8cd80..c1b6cb8ad 100644 --- a/src/uu/sync/src/sync.rs +++ b/src/uu/sync/src/sync.rs @@ -7,17 +7,14 @@ /* Last synced with: sync (GNU coreutils) 8.13 */ -extern crate getopts; +extern crate clap; extern crate libc; -#[cfg(windows)] #[macro_use] extern crate uucore; -#[cfg(not(windows))] -extern crate uucore; - -static NAME: &str = "sync"; +use clap::App; +static ABOUT: &str = "Synchronize cached writes to persistent storage"; static VERSION: &str = env!("CARGO_PKG_VERSION"); #[cfg(unix)] @@ -118,57 +115,23 @@ mod platform { } } +fn get_usage() -> String { + format!("{0} [OPTION]... FILE...", executable!()) +} + pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let usage = get_usage(); - let mut opts = getopts::Options::new(); - - opts.optflag("h", "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, - _ => { - help(&opts); - return 1; - } - }; - - if matches.opt_present("h") { - help(&opts); - return 0; - } - - if matches.opt_present("V") { - version(); - return 0; - } + let _matches = App::new(executable!()) + .version(VERSION) + .about(ABOUT) + .usage(&usage[..]) + .get_matches_from(args); sync(); 0 } -fn version() { - println!("{} (uutils) {}", NAME, VERSION); - println!("The MIT License"); - println!(); - println!("Author -- Alexander Fomin."); -} - -fn help(opts: &getopts::Options) { - let msg = format!( - "{0} {1} - -Usage: - {0} [OPTION] - -Force changed blocks to disk, update the super block.", - NAME, VERSION - ); - - print!("{}", opts.usage(&msg)); -} - fn sync() -> isize { unsafe { platform::do_sync() } } diff --git a/tests/by-util/test_sync.rs b/tests/by-util/test_sync.rs index 651491045..e4eb72eac 100644 --- a/tests/by-util/test_sync.rs +++ b/tests/by-util/test_sync.rs @@ -1 +1,11 @@ -// ToDO: add tests +use crate::common::util::*; + +#[test] +fn test_sync_default() { + new_ucmd!().run(); +} + +#[test] +fn test_sync_incorrect_arg() { + new_ucmd!().arg("--foo").fails(); +}