1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

mktemp: update to clap 4

This commit is contained in:
Terts Diepraam 2022-09-29 22:43:07 +02:00
parent 1d6c5d14b2
commit 1e9e790b7c
2 changed files with 30 additions and 22 deletions

View file

@ -15,7 +15,7 @@ edition = "2021"
path = "src/mktemp.rs" path = "src/mktemp.rs"
[dependencies] [dependencies]
clap = { version = "3.2", features = ["wrap_help", "cargo"] } clap = { version = "4.0", features = ["wrap_help", "cargo"] }
rand = "0.8" rand = "0.8"
tempfile = "3" tempfile = "3"
uucore = { version=">=0.0.16", package="uucore", path="../../uucore" } uucore = { version=">=0.0.16", package="uucore", path="../../uucore" }

View file

@ -8,7 +8,7 @@
// spell-checker:ignore (paths) GPGHome findxs // spell-checker:ignore (paths) GPGHome findxs
use clap::{crate_version, Arg, ArgMatches, Command}; use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
use uucore::display::{println_verbatim, Quotable}; use uucore::display::{println_verbatim, Quotable};
use uucore::error::{FromIo, UError, UResult, UUsageError}; use uucore::error::{FromIo, UError, UResult, UUsageError};
use uucore::format_usage; use uucore::format_usage;
@ -197,12 +197,12 @@ impl Options {
} }
}; };
Self { Self {
directory: matches.contains_id(OPT_DIRECTORY), directory: matches.get_flag(OPT_DIRECTORY),
dry_run: matches.contains_id(OPT_DRY_RUN), dry_run: matches.get_flag(OPT_DRY_RUN),
quiet: matches.contains_id(OPT_QUIET), quiet: matches.get_flag(OPT_QUIET),
tmpdir, tmpdir,
suffix: matches.get_one::<String>(OPT_SUFFIX).map(String::from), suffix: matches.get_one::<String>(OPT_SUFFIX).map(String::from),
treat_as_template: matches.contains_id(OPT_T), treat_as_template: matches.get_flag(OPT_T),
template, template,
} }
} }
@ -340,7 +340,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = match uu_app().try_get_matches_from(&args) { let matches = match uu_app().try_get_matches_from(&args) {
Ok(m) => m, Ok(m) => m,
Err(e) => { Err(e) => {
if e.kind == clap::error::ErrorKind::TooManyValues && e.info[0] == "<template>..." { if e.kind() == clap::error::ErrorKind::TooManyValues
&& e.context().any(|(kind, val)| {
kind == clap::error::ContextKind::InvalidArg
&& val == &clap::error::ContextValue::String("[template]".into())
})
{
return Err(UUsageError::new(1, "too many templates")); return Err(UUsageError::new(1, "too many templates"));
} }
return Err(e.into()); return Err(e.into());
@ -388,7 +393,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} }
} }
pub fn uu_app<'a>() -> Command<'a> { pub fn uu_app() -> Command {
Command::new(uucore::util_name()) Command::new(uucore::util_name())
.version(crate_version!()) .version(crate_version!())
.about(ABOUT) .about(ABOUT)
@ -398,19 +403,22 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(OPT_DIRECTORY) Arg::new(OPT_DIRECTORY)
.short('d') .short('d')
.long(OPT_DIRECTORY) .long(OPT_DIRECTORY)
.help("Make a directory instead of a file"), .help("Make a directory instead of a file")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_DRY_RUN) Arg::new(OPT_DRY_RUN)
.short('u') .short('u')
.long(OPT_DRY_RUN) .long(OPT_DRY_RUN)
.help("do not create anything; merely print a name (unsafe)"), .help("do not create anything; merely print a name (unsafe)")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_QUIET) Arg::new(OPT_QUIET)
.short('q') .short('q')
.long("quiet") .long("quiet")
.help("Fail silently if an error occurs."), .help("Fail silently if an error occurs.")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(OPT_SUFFIX) Arg::new(OPT_SUFFIX)
@ -427,23 +435,23 @@ pub fn uu_app<'a>() -> Command<'a> {
.long(OPT_TMPDIR) .long(OPT_TMPDIR)
.help( .help(
"interpret TEMPLATE relative to DIR; if DIR is not specified, use \ "interpret TEMPLATE relative to DIR; if DIR is not specified, use \
$TMPDIR ($TMP on windows) if set, else /tmp. With this option, TEMPLATE must not \ $TMPDIR ($TMP on windows) if set, else /tmp. With this option, \
be an absolute name; unlike with -t, TEMPLATE may contain \ TEMPLATE must not be an absolute name; unlike with -t, TEMPLATE \
slashes, but mktemp creates only the final component", may contain slashes, but mktemp creates only the final component",
) )
.value_name("DIR") .value_name("DIR")
.value_hint(clap::ValueHint::DirPath), .value_hint(clap::ValueHint::DirPath),
) )
.arg(Arg::new(OPT_T).short('t').help(
"Generate a template (using the supplied prefix and TMPDIR (TMP on windows) if set) \
to create a filename template [deprecated]",
))
.arg( .arg(
Arg::new(ARG_TEMPLATE) Arg::new(OPT_T)
.multiple_occurrences(false) .short('t')
.takes_value(true) .help(
.max_values(1) "Generate a template (using the supplied prefix and TMPDIR \
(TMP on windows) if set) to create a filename template [deprecated]",
)
.action(ArgAction::SetTrue),
) )
.arg(Arg::new(ARG_TEMPLATE).num_args(..=1))
} }
pub fn dry_exec(tmpdir: &str, prefix: &str, rand: usize, suffix: &str) -> UResult<()> { pub fn dry_exec(tmpdir: &str, prefix: &str, rand: usize, suffix: &str) -> UResult<()> {