1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #2152 from deantvv/link-clap

link: replace getopts with clap
This commit is contained in:
Sylvestre Ledru 2021-05-02 09:33:11 +02:00 committed by GitHub
commit 2d0f4daf5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 13 deletions

1
Cargo.lock generated
View file

@ -2052,6 +2052,7 @@ dependencies = [
name = "uu_link"
version = "0.0.6"
dependencies = [
"clap",
"libc",
"uucore",
"uucore_procs",

View file

@ -18,6 +18,7 @@ path = "src/link.rs"
libc = "0.2.42"
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
clap = "2.33"
[[bin]]
name = "link"

View file

@ -8,14 +8,21 @@
#[macro_use]
extern crate uucore;
use clap::{App, Arg};
use std::fs::hard_link;
use std::io::Error;
use std::path::Path;
use uucore::InvalidEncodingHandling;
static SYNTAX: &str = "[OPTIONS] FILE1 FILE2";
static SUMMARY: &str = "Create a link named FILE2 to FILE1";
static LONG_HELP: &str = "";
static VERSION: &str = env!("CARGO_PKG_VERSION");
static ABOUT: &str = "Call the link function to create a link named FILE2 to an existing FILE1.";
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 {
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 {
let matches = app!(SYNTAX, SUMMARY, LONG_HELP).parse(
args.collect_str(InvalidEncodingHandling::Ignore)
.accept_any(),
);
if matches.free.len() != 2 {
crash!(1, "{}", msg_wrong_number_of_arguments!(2));
}
let usage = get_usage();
let matches = App::new(executable!())
.version(VERSION)
.about(ABOUT)
.usage(&usage[..])
.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 new = Path::new(&matches.free[1]);
let files: Vec<_> = matches
.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) {
Ok(_) => 0,