mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
Merge pull request #2152 from deantvv/link-clap
link: replace getopts with clap
This commit is contained in:
commit
2d0f4daf5b
3 changed files with 33 additions and 13 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2052,6 +2052,7 @@ dependencies = [
|
||||||
name = "uu_link"
|
name = "uu_link"
|
||||||
version = "0.0.6"
|
version = "0.0.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"clap",
|
||||||
"libc",
|
"libc",
|
||||||
"uucore",
|
"uucore",
|
||||||
"uucore_procs",
|
"uucore_procs",
|
||||||
|
|
|
@ -18,6 +18,7 @@ path = "src/link.rs"
|
||||||
libc = "0.2.42"
|
libc = "0.2.42"
|
||||||
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
|
uucore = { version=">=0.0.8", 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" }
|
||||||
|
clap = "2.33"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "link"
|
name = "link"
|
||||||
|
|
|
@ -8,14 +8,21 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
use std::fs::hard_link;
|
use std::fs::hard_link;
|
||||||
use std::io::Error;
|
use std::io::Error;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use uucore::InvalidEncodingHandling;
|
|
||||||
|
|
||||||
static SYNTAX: &str = "[OPTIONS] FILE1 FILE2";
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
static SUMMARY: &str = "Create a link named FILE2 to FILE1";
|
static ABOUT: &str = "Call the link function to create a link named FILE2 to an existing FILE1.";
|
||||||
static LONG_HELP: &str = "";
|
|
||||||
|
pub mod options {
|
||||||
|
pub static FILES: &str = "FILES";
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_usage() -> String {
|
||||||
|
format!("{0} FILE1 FILE2", executable!())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn normalize_error_message(e: Error) -> String {
|
pub fn normalize_error_message(e: Error) -> String {
|
||||||
match e.raw_os_error() {
|
match e.raw_os_error() {
|
||||||
|
@ -25,16 +32,27 @@ pub fn normalize_error_message(e: Error) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let matches = app!(SYNTAX, SUMMARY, LONG_HELP).parse(
|
let usage = get_usage();
|
||||||
args.collect_str(InvalidEncodingHandling::Ignore)
|
let matches = App::new(executable!())
|
||||||
.accept_any(),
|
.version(VERSION)
|
||||||
);
|
.about(ABOUT)
|
||||||
if matches.free.len() != 2 {
|
.usage(&usage[..])
|
||||||
crash!(1, "{}", msg_wrong_number_of_arguments!(2));
|
.arg(
|
||||||
}
|
Arg::with_name(options::FILES)
|
||||||
|
.hidden(true)
|
||||||
|
.required(true)
|
||||||
|
.min_values(2)
|
||||||
|
.max_values(2)
|
||||||
|
.takes_value(true),
|
||||||
|
)
|
||||||
|
.get_matches_from(args);
|
||||||
|
|
||||||
let old = Path::new(&matches.free[0]);
|
let files: Vec<_> = matches
|
||||||
let new = Path::new(&matches.free[1]);
|
.values_of_os(options::FILES)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.collect();
|
||||||
|
let old = Path::new(files[0]);
|
||||||
|
let new = Path::new(files[1]);
|
||||||
|
|
||||||
match hard_link(old, new) {
|
match hard_link(old, new) {
|
||||||
Ok(_) => 0,
|
Ok(_) => 0,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue