mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 23:02:38 +00:00
refactor(mktemp): move to clap
This commit is contained in:
parent
bfc9e13f11
commit
66b503a77e
3 changed files with 91 additions and 93 deletions
34
Cargo.lock
generated
34
Cargo.lock
generated
|
@ -1,5 +1,3 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "advapi32-sys"
|
||||
version = "0.2.0"
|
||||
|
@ -436,7 +434,7 @@ dependencies = [
|
|||
"serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_cbor 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.61 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tinytemplate 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
@ -618,10 +616,10 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.1.15"
|
||||
version = "0.1.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
@ -967,7 +965,7 @@ name = "rand"
|
|||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"getrandom 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"getrandom 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -1002,7 +1000,7 @@ name = "rand_core"
|
|||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"getrandom 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"getrandom 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1159,12 +1157,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
dependencies = [
|
||||
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.60"
|
||||
version = "1.0.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"itoa 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -1225,7 +1223,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.56"
|
||||
version = "1.0.57"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -1336,7 +1334,7 @@ version = "1.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.61 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1822,7 +1820,7 @@ dependencies = [
|
|||
name = "uu_mktemp"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"uucore 0.0.4",
|
||||
|
@ -2377,7 +2375,7 @@ version = "0.0.4"
|
|||
dependencies = [
|
||||
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2424,7 +2422,7 @@ dependencies = [
|
|||
"log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasm-bindgen-shared 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
@ -2444,7 +2442,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
dependencies = [
|
||||
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasm-bindgen-backend 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasm-bindgen-shared 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
@ -2577,7 +2575,7 @@ dependencies = [
|
|||
"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
|
||||
"checksum generic-array 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fceb69994e330afed50c93524be68c42fa898c2d9fd4ee8da03bd7363acd26f2"
|
||||
"checksum getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
|
||||
"checksum getrandom 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
|
||||
"checksum getrandom 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
|
||||
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
|
||||
"checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
|
||||
"checksum half 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d36fab90f82edc3c747f9d438e06cf0a491055896f2a279638bb5beed6c40177"
|
||||
|
@ -2650,14 +2648,14 @@ dependencies = [
|
|||
"checksum serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)" = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800"
|
||||
"checksum serde_cbor 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622"
|
||||
"checksum serde_derive 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)" = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df"
|
||||
"checksum serde_json 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)" = "1500e84d27fe482ed1dc791a56eddc2f230046a040fa908c08bda1d9fb615779"
|
||||
"checksum serde_json 1.0.61 (registry+https://github.com/rust-lang/crates.io-index)" = "4fceb2595057b6891a4ee808f70054bd2d12f0e97f1cbb78689b59f676df325a"
|
||||
"checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d"
|
||||
"checksum sha2 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d963c78ce367df26d7ea8b8cc655c651b42e8a1e584e869c1e17dae3ccb116a"
|
||||
"checksum sha3 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26405905b6a56a94c60109cfda62610507ac14a65be531f5767dec5c5a8dd6a0"
|
||||
"checksum smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6"
|
||||
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
|
||||
"checksum syn 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)" = "a9802ddde94170d186eeee5005b798d9c159fa970403f1be19976d0cfb939b72"
|
||||
"checksum syn 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)" = "4211ce9909eb971f111059df92c45640aad50a619cf55cd76476be803c4c68e6"
|
||||
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
|
||||
"checksum synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a761d12e6d8dcb4dcf952a7a89b475e3a9d69e4a69307e01a470977642914bd"
|
||||
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
|
||||
|
|
|
@ -15,7 +15,7 @@ edition = "2018"
|
|||
path = "src/mktemp.rs"
|
||||
|
||||
[dependencies]
|
||||
getopts = "0.2.18"
|
||||
clap = "2.33"
|
||||
rand = "0.5"
|
||||
tempfile = "3.0"
|
||||
uucore = { version=">=0.0.4", package="uucore", path="../../uucore" }
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
#[macro_use]
|
||||
extern crate uucore;
|
||||
|
||||
use clap::{App, Arg};
|
||||
|
||||
use std::env;
|
||||
use std::iter;
|
||||
use std::mem::forget;
|
||||
|
@ -21,76 +23,89 @@ use tempfile::Builder;
|
|||
|
||||
mod tempdir;
|
||||
|
||||
static NAME: &str = "mktemp";
|
||||
static ABOUT: &str = "create a temporary file or directory.";
|
||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
static DEFAULT_TEMPLATE: &str = "tmp.XXXXXXXXXX";
|
||||
|
||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||
let args = args.collect_str();
|
||||
static OPT_DIRECTORY: &str = "directory";
|
||||
static OPT_DRY_RUN: &str = "dry-run";
|
||||
static OPT_QUIET: &str = "quiet";
|
||||
static OPT_SUFFIX: &str = "suffix";
|
||||
static OPT_TMPDIR: &str = "tmpdir";
|
||||
|
||||
let mut opts = getopts::Options::new();
|
||||
opts.optflag("d", "directory", "Make a directory instead of a file");
|
||||
opts.optflag(
|
||||
"u",
|
||||
"dry-run",
|
||||
"do not create anything; merely print a name (unsafe)",
|
||||
);
|
||||
opts.optflag("q", "quiet", "Fail silently if an error occurs.");
|
||||
opts.optopt(
|
||||
"",
|
||||
"suffix",
|
||||
"append SUFF to TEMPLATE; SUFF must not contain a path separator. \
|
||||
static ARG_TEMPLATE: &str = "template";
|
||||
|
||||
fn get_usage() -> String {
|
||||
format!("{0} [OPTION]... [TEMPLATE]", executable!())
|
||||
}
|
||||
|
||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||
let usage = get_usage();
|
||||
|
||||
let matches = App::new(executable!())
|
||||
.version(VERSION)
|
||||
.about(ABOUT)
|
||||
.usage(&usage[..])
|
||||
.arg(
|
||||
Arg::with_name(OPT_DIRECTORY)
|
||||
.short("d")
|
||||
.long(OPT_DIRECTORY)
|
||||
.help("Make a directory instead of a file"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name(OPT_DRY_RUN)
|
||||
.short("u")
|
||||
.long(OPT_DRY_RUN)
|
||||
.help("do not create anything; merely print a name (unsafe)"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name(OPT_QUIET)
|
||||
.short("q")
|
||||
.long("quiet")
|
||||
.help("Fail silently if an error occurs."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name(OPT_SUFFIX)
|
||||
.long(OPT_SUFFIX)
|
||||
.help(
|
||||
"append SUFF to TEMPLATE; SUFF must not contain a path separator. \
|
||||
This option is implied if TEMPLATE does not end with X.",
|
||||
"SUFF",
|
||||
);
|
||||
opts.optopt(
|
||||
"p",
|
||||
"tmpdir",
|
||||
"interpret TEMPLATE relative to DIR; if DIR is not specified, use \
|
||||
)
|
||||
.value_name("SUFF"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name(OPT_TMPDIR)
|
||||
.short("p")
|
||||
.long(OPT_TMPDIR)
|
||||
.help(
|
||||
"interpret TEMPLATE relative to DIR; if DIR is not specified, use \
|
||||
$TMPDIR 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",
|
||||
"DIR",
|
||||
);
|
||||
)
|
||||
.value_name("DIR"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name(ARG_TEMPLATE)
|
||||
.multiple(false)
|
||||
.takes_value(true)
|
||||
.max_values(1)
|
||||
.default_value(DEFAULT_TEMPLATE),
|
||||
)
|
||||
.get_matches_from(args);
|
||||
// deprecated option of GNU coreutils
|
||||
// opts.optflag("t", "", "Generate a template (using the supplied prefix and TMPDIR if set) \
|
||||
// .arg(
|
||||
// Arg::with_name(("t", "", "Generate a template (using the supplied prefix and TMPDIR if set) \
|
||||
// to create a filename template");
|
||||
opts.optflag("", "help", "Print this help and exit");
|
||||
opts.optflag("", "version", "print the version and exit");
|
||||
|
||||
// >> early return options
|
||||
let matches = match opts.parse(&args[1..]) {
|
||||
Ok(m) => m,
|
||||
Err(f) => crash!(1, "Invalid options\n{}", f),
|
||||
};
|
||||
let template = matches.value_of(ARG_TEMPLATE).unwrap();
|
||||
|
||||
if matches.opt_present("help") {
|
||||
print_help(&opts);
|
||||
return 0;
|
||||
}
|
||||
if matches.opt_present("version") {
|
||||
println!("{} {}", NAME, VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if 1 < matches.free.len() {
|
||||
crash!(1, "Too many templates");
|
||||
}
|
||||
// <<
|
||||
|
||||
let make_dir = matches.opt_present("directory");
|
||||
let dry_run = matches.opt_present("dry-run");
|
||||
let suffix_opt = matches.opt_str("suffix");
|
||||
let suppress_file_err = matches.opt_present("quiet");
|
||||
|
||||
let template = if matches.free.is_empty() {
|
||||
DEFAULT_TEMPLATE
|
||||
} else {
|
||||
&matches.free[0][..]
|
||||
};
|
||||
let make_dir = matches.is_present(OPT_DIRECTORY);
|
||||
let dry_run = matches.is_present(OPT_DRY_RUN);
|
||||
let suppress_file_err = matches.is_present(OPT_QUIET);
|
||||
|
||||
let (prefix, rand, suffix) = match parse_template(template) {
|
||||
Some((p, r, s)) => match suffix_opt {
|
||||
Some((p, r, s)) => match matches.value_of(OPT_SUFFIX) {
|
||||
Some(suf) => {
|
||||
if s.is_empty() {
|
||||
(p, r, suf)
|
||||
|
@ -101,9 +116,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
)
|
||||
}
|
||||
}
|
||||
None => (p, r, s.to_owned()),
|
||||
None => (p, r, s),
|
||||
},
|
||||
None => ("", 0, "".to_owned()),
|
||||
None => ("", 0, ""),
|
||||
};
|
||||
|
||||
if rand < 3 {
|
||||
|
@ -114,7 +129,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
crash!(1, "suffix cannot contain any path separators");
|
||||
}
|
||||
|
||||
let tmpdir = match matches.opt_str("tmpdir") {
|
||||
let tmpdir = match matches.value_of(OPT_TMPDIR) {
|
||||
Some(s) => {
|
||||
if PathBuf::from(prefix).is_absolute() {
|
||||
show_info!(
|
||||
|
@ -135,21 +150,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
}
|
||||
}
|
||||
|
||||
fn print_help(opts: &getopts::Options) {
|
||||
let usage = format!(
|
||||
" Create a temporary file or directory, safely, and print its name.
|
||||
TEMPLATE must contain at least 3 consecutive 'X's in last component.
|
||||
If TEMPLATE is not specified, use {}, and --tmpdir is implied",
|
||||
DEFAULT_TEMPLATE
|
||||
);
|
||||
|
||||
println!("{} {}", NAME, VERSION);
|
||||
println!("SYNOPSIS");
|
||||
println!(" {} [OPTION]... [FILE]", NAME);
|
||||
println!("Usage:");
|
||||
print!("{}", opts.usage(&usage[..]));
|
||||
}
|
||||
|
||||
fn parse_template(temp: &str) -> Option<(&str, usize, &str)> {
|
||||
let right = match temp.rfind('X') {
|
||||
Some(r) => r + 1,
|
||||
|
|
Loading…
Reference in a new issue