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"
[dependencies]
clap = { version = "3.2", features = ["wrap_help", "cargo"] }
clap = { version = "4.0", features = ["wrap_help", "cargo"] }
rand = "0.8"
tempfile = "3"
uucore = { version=">=0.0.16", package="uucore", path="../../uucore" }

View file

@ -8,7 +8,7 @@
// 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::error::{FromIo, UError, UResult, UUsageError};
use uucore::format_usage;
@ -197,12 +197,12 @@ impl Options {
}
};
Self {
directory: matches.contains_id(OPT_DIRECTORY),
dry_run: matches.contains_id(OPT_DRY_RUN),
quiet: matches.contains_id(OPT_QUIET),
directory: matches.get_flag(OPT_DIRECTORY),
dry_run: matches.get_flag(OPT_DRY_RUN),
quiet: matches.get_flag(OPT_QUIET),
tmpdir,
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,
}
}
@ -340,7 +340,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = match uu_app().try_get_matches_from(&args) {
Ok(m) => m,
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(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())
.version(crate_version!())
.about(ABOUT)
@ -398,19 +403,22 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(OPT_DIRECTORY)
.short('d')
.long(OPT_DIRECTORY)
.help("Make a directory instead of a file"),
.help("Make a directory instead of a file")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(OPT_DRY_RUN)
.short('u')
.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::new(OPT_QUIET)
.short('q')
.long("quiet")
.help("Fail silently if an error occurs."),
.help("Fail silently if an error occurs.")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(OPT_SUFFIX)
@ -427,23 +435,23 @@ pub fn uu_app<'a>() -> Command<'a> {
.long(OPT_TMPDIR)
.help(
"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 \
be an absolute name; unlike with -t, TEMPLATE may contain \
slashes, but mktemp creates only the final component",
$TMPDIR ($TMP on windows) if set, else /tmp. With this option, \
TEMPLATE must not be an absolute name; unlike with -t, TEMPLATE \
may contain slashes, but mktemp creates only the final component",
)
.value_name("DIR")
.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::new(ARG_TEMPLATE)
.multiple_occurrences(false)
.takes_value(true)
.max_values(1)
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]",
)
.action(ArgAction::SetTrue),
)
.arg(Arg::new(ARG_TEMPLATE).num_args(..=1))
}
pub fn dry_exec(tmpdir: &str, prefix: &str, rand: usize, suffix: &str) -> UResult<()> {