mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 20:47:46 +00:00
Merge pull request #2460 from miDeb/completions-errors
coreutils: better errors for invalid args for completions
This commit is contained in:
commit
010919a9a9
1 changed files with 26 additions and 18 deletions
|
@ -6,6 +6,7 @@
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
|
|
||||||
use clap::App;
|
use clap::App;
|
||||||
|
use clap::Arg;
|
||||||
use clap::Shell;
|
use clap::Shell;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::collections::hash_map::HashMap;
|
use std::collections::hash_map::HashMap;
|
||||||
|
@ -122,31 +123,38 @@ fn main() {
|
||||||
|
|
||||||
/// Prints completions for the utility in the first parameter for the shell in the second parameter to stdout
|
/// Prints completions for the utility in the first parameter for the shell in the second parameter to stdout
|
||||||
fn gen_completions<T: uucore::Args>(
|
fn gen_completions<T: uucore::Args>(
|
||||||
mut args: impl Iterator<Item = OsString>,
|
args: impl Iterator<Item = OsString>,
|
||||||
util_map: UtilityMap<T>,
|
util_map: UtilityMap<T>,
|
||||||
) -> ! {
|
) -> ! {
|
||||||
let utility = args
|
let all_utilities: Vec<_> = std::iter::once("coreutils")
|
||||||
.next()
|
.chain(util_map.keys().copied())
|
||||||
.expect("expected utility as the first parameter")
|
.collect();
|
||||||
.to_str()
|
|
||||||
.expect("utility name was not valid utf-8")
|
let matches = App::new("completion")
|
||||||
.to_owned();
|
.about("Prints completions to stdout")
|
||||||
let shell = args
|
.arg(
|
||||||
.next()
|
Arg::with_name("utility")
|
||||||
.expect("expected shell as the second parameter")
|
.possible_values(&all_utilities)
|
||||||
.to_str()
|
.required(true),
|
||||||
.expect("shell name was not valid utf-8")
|
)
|
||||||
.to_owned();
|
.arg(
|
||||||
|
Arg::with_name("shell")
|
||||||
|
.possible_values(&Shell::variants())
|
||||||
|
.required(true),
|
||||||
|
)
|
||||||
|
.get_matches_from(std::iter::once(OsString::from("completion")).chain(args));
|
||||||
|
|
||||||
|
let utility = matches.value_of("utility").unwrap();
|
||||||
|
let shell = matches.value_of("shell").unwrap();
|
||||||
|
|
||||||
let mut app = if utility == "coreutils" {
|
let mut app = if utility == "coreutils" {
|
||||||
gen_coreutils_app(util_map)
|
gen_coreutils_app(util_map)
|
||||||
} else if let Some((_, app)) = util_map.get(utility.as_str()) {
|
|
||||||
app()
|
|
||||||
} else {
|
} else {
|
||||||
eprintln!("{} is not a valid utility", utility);
|
util_map.get(utility).unwrap().1()
|
||||||
process::exit(1)
|
|
||||||
};
|
};
|
||||||
let shell: Shell = shell.parse().unwrap();
|
let shell: Shell = shell.parse().unwrap();
|
||||||
let bin_name = std::env::var("PROG_PREFIX").unwrap_or_default() + &utility;
|
let bin_name = std::env::var("PROG_PREFIX").unwrap_or_default() + utility;
|
||||||
|
|
||||||
app.gen_completions_to(bin_name, shell, &mut io::stdout());
|
app.gen_completions_to(bin_name, shell, &mut io::stdout());
|
||||||
io::stdout().flush().unwrap();
|
io::stdout().flush().unwrap();
|
||||||
process::exit(0);
|
process::exit(0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue