1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-14 19:16:17 +00:00

ln: refactor argument handling

This commit is contained in:
Terts Diepraam 2021-06-07 14:51:58 +02:00
parent 9ae3c7634c
commit 448caa3d1c

View file

@ -78,17 +78,19 @@ fn get_long_usage() -> String {
static ABOUT: &str = "change file owner and group"; static ABOUT: &str = "change file owner and group";
static OPT_B: &str = "b"; mod options {
static OPT_BACKUP: &str = "backup"; pub const B: &str = "b";
static OPT_FORCE: &str = "force"; pub const BACKUP: &str = "backup";
static OPT_INTERACTIVE: &str = "interactive"; pub const FORCE: &str = "force";
static OPT_NO_DEREFERENCE: &str = "no-dereference"; pub const INTERACTIVE: &str = "interactive";
static OPT_SYMBOLIC: &str = "symbolic"; pub const NO_DEREFERENCE: &str = "no-dereference";
static OPT_SUFFIX: &str = "suffix"; pub const SYMBOLIC: &str = "symbolic";
static OPT_TARGET_DIRECTORY: &str = "target-directory"; pub const SUFFIX: &str = "suffix";
static OPT_NO_TARGET_DIRECTORY: &str = "no-target-directory"; pub const TARGET_DIRECTORY: &str = "target-directory";
static OPT_RELATIVE: &str = "relative"; pub const NO_TARGET_DIRECTORY: &str = "no-target-directory";
static OPT_VERBOSE: &str = "verbose"; pub const RELATIVE: &str = "relative";
pub const VERBOSE: &str = "verbose";
}
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
@ -101,47 +103,42 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.about(ABOUT) .about(ABOUT)
.usage(&usage[..]) .usage(&usage[..])
.after_help(&long_usage[..]) .after_help(&long_usage[..])
.arg(Arg::with_name(OPT_B).short(OPT_B).help( .arg(Arg::with_name(options::B).short(options::B).help(
"make a backup of each file that would otherwise be overwritten or \ "make a backup of each file that would otherwise be overwritten or \
removed", removed",
)) ))
.arg( .arg(
Arg::with_name(OPT_BACKUP) Arg::with_name(options::BACKUP)
.long(OPT_BACKUP) .long(options::BACKUP)
.help( .help(
"make a backup of each file that would otherwise be overwritten \ "make a backup of each file that would otherwise be overwritten \
or removed", or removed",
) )
.takes_value(true) .takes_value(true)
.possible_value("simple") .possible_values(&[
.possible_value("never") "simple", "never", "numbered", "t", "existing", "nil", "none", "off",
.possible_value("numbered") ])
.possible_value("t")
.possible_value("existing")
.possible_value("nil")
.possible_value("none")
.possible_value("off")
.value_name("METHOD"), .value_name("METHOD"),
) )
// TODO: opts.arg( // TODO: opts.arg(
// Arg::with_name(("d", "directory", "allow users with appropriate privileges to attempt \ // Arg::with_name(("d", "directory", "allow users with appropriate privileges to attempt \
// to make hard links to directories"); // to make hard links to directories");
.arg( .arg(
Arg::with_name(OPT_FORCE) Arg::with_name(options::FORCE)
.short("f") .short("f")
.long(OPT_FORCE) .long(options::FORCE)
.help("remove existing destination files"), .help("remove existing destination files"),
) )
.arg( .arg(
Arg::with_name(OPT_INTERACTIVE) Arg::with_name(options::INTERACTIVE)
.short("i") .short("i")
.long(OPT_INTERACTIVE) .long(options::INTERACTIVE)
.help("prompt whether to remove existing destination files"), .help("prompt whether to remove existing destination files"),
) )
.arg( .arg(
Arg::with_name(OPT_NO_DEREFERENCE) Arg::with_name(options::NO_DEREFERENCE)
.short("n") .short("n")
.long(OPT_NO_DEREFERENCE) .long(options::NO_DEREFERENCE)
.help( .help(
"treat LINK_executable!() as a normal file if it is a \ "treat LINK_executable!() as a normal file if it is a \
symbolic link to a directory", symbolic link to a directory",
@ -153,43 +150,45 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
// TODO: opts.arg( // TODO: opts.arg(
// Arg::with_name(("P", "physical", "make hard links directly to symbolic links"); // Arg::with_name(("P", "physical", "make hard links directly to symbolic links");
.arg( .arg(
Arg::with_name(OPT_SYMBOLIC) Arg::with_name(options::SYMBOLIC)
.short("s") .short("s")
.long("symbolic") .long("symbolic")
.help("make symbolic links instead of hard links"), .help("make symbolic links instead of hard links")
// override added for https://github.com/uutils/coreutils/issues/2359
.overrides_with(options::SYMBOLIC),
) )
.arg( .arg(
Arg::with_name(OPT_SUFFIX) Arg::with_name(options::SUFFIX)
.short("S") .short("S")
.long(OPT_SUFFIX) .long(options::SUFFIX)
.help("override the usual backup suffix") .help("override the usual backup suffix")
.value_name("SUFFIX") .value_name("SUFFIX")
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
Arg::with_name(OPT_TARGET_DIRECTORY) Arg::with_name(options::TARGET_DIRECTORY)
.short("t") .short("t")
.long(OPT_TARGET_DIRECTORY) .long(options::TARGET_DIRECTORY)
.help("specify the DIRECTORY in which to create the links") .help("specify the DIRECTORY in which to create the links")
.value_name("DIRECTORY") .value_name("DIRECTORY")
.conflicts_with(OPT_NO_TARGET_DIRECTORY), .conflicts_with(options::NO_TARGET_DIRECTORY),
) )
.arg( .arg(
Arg::with_name(OPT_NO_TARGET_DIRECTORY) Arg::with_name(options::NO_TARGET_DIRECTORY)
.short("T") .short("T")
.long(OPT_NO_TARGET_DIRECTORY) .long(options::NO_TARGET_DIRECTORY)
.help("treat LINK_executable!() as a normal file always"), .help("treat LINK_executable!() as a normal file always"),
) )
.arg( .arg(
Arg::with_name(OPT_RELATIVE) Arg::with_name(options::RELATIVE)
.short("r") .short("r")
.long(OPT_RELATIVE) .long(options::RELATIVE)
.help("create symbolic links relative to link location"), .help("create symbolic links relative to link location"),
) )
.arg( .arg(
Arg::with_name(OPT_VERBOSE) Arg::with_name(options::VERBOSE)
.short("v") .short("v")
.long(OPT_VERBOSE) .long(options::VERBOSE)
.help("print name of each linked file"), .help("print name of each linked file"),
) )
.arg( .arg(
@ -209,18 +208,18 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.map(PathBuf::from) .map(PathBuf::from)
.collect(); .collect();
let overwrite_mode = if matches.is_present(OPT_FORCE) { let overwrite_mode = if matches.is_present(options::FORCE) {
OverwriteMode::Force OverwriteMode::Force
} else if matches.is_present(OPT_INTERACTIVE) { } else if matches.is_present(options::INTERACTIVE) {
OverwriteMode::Interactive OverwriteMode::Interactive
} else { } else {
OverwriteMode::NoClobber OverwriteMode::NoClobber
}; };
let backup_mode = if matches.is_present(OPT_B) { let backup_mode = if matches.is_present(options::B) {
BackupMode::ExistingBackup BackupMode::ExistingBackup
} else if matches.is_present(OPT_BACKUP) { } else if matches.is_present(options::BACKUP) {
match matches.value_of(OPT_BACKUP) { match matches.value_of(options::BACKUP) {
None => BackupMode::ExistingBackup, None => BackupMode::ExistingBackup,
Some(mode) => match mode { Some(mode) => match mode {
"simple" | "never" => BackupMode::SimpleBackup, "simple" | "never" => BackupMode::SimpleBackup,
@ -234,8 +233,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
BackupMode::NoBackup BackupMode::NoBackup
}; };
let backup_suffix = if matches.is_present(OPT_SUFFIX) { let backup_suffix = if matches.is_present(options::SUFFIX) {
matches.value_of(OPT_SUFFIX).unwrap() matches.value_of(options::SUFFIX).unwrap()
} else { } else {
"~" "~"
}; };
@ -243,14 +242,16 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let settings = Settings { let settings = Settings {
overwrite: overwrite_mode, overwrite: overwrite_mode,
backup: backup_mode, backup: backup_mode,
force: matches.is_present(OPT_FORCE), force: matches.is_present(options::FORCE),
suffix: backup_suffix.to_string(), suffix: backup_suffix.to_string(),
symbolic: matches.is_present(OPT_SYMBOLIC), symbolic: matches.is_present(options::SYMBOLIC),
relative: matches.is_present(OPT_RELATIVE), relative: matches.is_present(options::RELATIVE),
target_dir: matches.value_of(OPT_TARGET_DIRECTORY).map(String::from), target_dir: matches
no_target_dir: matches.is_present(OPT_NO_TARGET_DIRECTORY), .value_of(options::TARGET_DIRECTORY)
no_dereference: matches.is_present(OPT_NO_DEREFERENCE), .map(String::from),
verbose: matches.is_present(OPT_VERBOSE), no_target_dir: matches.is_present(options::NO_TARGET_DIRECTORY),
no_dereference: matches.is_present(options::NO_DEREFERENCE),
verbose: matches.is_present(options::VERBOSE),
}; };
exec(&paths[..], &settings) exec(&paths[..], &settings)