mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
Merge pull request #1635 from sylvestre/clap-sync
refactor(sync): Move to clap + add tests
This commit is contained in:
commit
d268bda28e
4 changed files with 26 additions and 53 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -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",
|
||||
"uucore_procs 0.0.4",
|
||||
|
|
|
@ -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", path="../../uucore", features=["wide"] }
|
||||
uucore_procs = { version=">=0.0.4", package="uucore_procs", path="../../uucore_procs" }
|
||||
|
|
|
@ -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() }
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue