From 997cf509f30031f11fb9384db5f295b5d672f8d0 Mon Sep 17 00:00:00 2001 From: Jan Starke Date: Fri, 8 Sep 2023 12:19:40 +0200 Subject: [PATCH] use clap_complete::generate --- src/apps/mactime2/bodyfile/bodyfile_reader.rs | 10 +++++----- src/apps/mactime2/cli.rs | 13 ++++++------- src/bin/cleanhive/cli.rs | 6 +++--- src/common/parse_cli.rs | 14 ++++++++------ 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/apps/mactime2/bodyfile/bodyfile_reader.rs b/src/apps/mactime2/bodyfile/bodyfile_reader.rs index 200f2ad..9c0e257 100644 --- a/src/apps/mactime2/bodyfile/bodyfile_reader.rs +++ b/src/apps/mactime2/bodyfile/bodyfile_reader.rs @@ -1,10 +1,10 @@ use encoding_rs_io::DecodeReaderBytesBuilder; use std::io::{BufRead, BufReader, Read}; use std::sync::mpsc::{Receiver, Sender}; -use std::thread::{JoinHandle}; +use std::thread::JoinHandle; -use crate::apps::mactime2::filter::{Provider, Joinable}; -use crate::apps::mactime2::stream::{StreamWorker, StreamReader}; +use crate::apps::mactime2::filter::{Joinable, Provider}; +use crate::apps::mactime2::stream::{StreamReader, StreamWorker}; pub struct BodyfileReader { worker: Option>, @@ -52,10 +52,10 @@ impl StreamWorker for BodyfileReader { } impl StreamReader for BodyfileReader { - fn new (worker: JoinHandle<()>, rx: Receiver) -> Self { + fn new(worker: JoinHandle<()>, rx: Receiver) -> Self { Self { worker: Some(worker), - rx: Some(rx) + rx: Some(rx), } } } diff --git a/src/apps/mactime2/cli.rs b/src/apps/mactime2/cli.rs index 9270ad5..41012d6 100644 --- a/src/apps/mactime2/cli.rs +++ b/src/apps/mactime2/cli.rs @@ -17,13 +17,12 @@ const BODYFILE_HELP: &str = "path to input file or '-' for stdin"; #[clap(name="mactime2", author, version, about, long_about = None)] pub struct Cli { - #[clap(short('b'), num_args=1, value_parser, value_hint=ValueHint::FilePath, default_value="-", help=BODYFILE_HELP, display_order(100))] + #[clap(short('b'), value_parser, value_hint=ValueHint::FilePath, default_value="-", help=BODYFILE_HELP, display_order(100))] pub(crate) input_file: Input, /// output format, if not specified, default value is 'txt' #[clap( short('F'), - num_args = 1, long("format"), value_enum, display_order(600) @@ -32,27 +31,27 @@ pub struct Cli { /// output as CSV instead of TXT. This is a conveniance option, which is identical to `--format=csv` /// and will be removed in a future release. If you specified `--format` and `-d`, the latter will be ignored. - #[clap(short('d'), num_args = 0, display_order(610))] + #[clap(short('d'), display_order(610))] pub(crate) csv_format: bool, /// output as JSON instead of TXT. This is a conveniance option, which is identical to `--format=json` /// and will be removed in a future release. If you specified `--format` and `-j`, the latter will be ignored. - #[clap(short('j'), num_args = 0, display_order(620))] + #[clap(short('j'), display_order(620))] pub(crate) json_format: bool, /// name of offset of source timezone (or 'list' to display all possible values - #[clap(short('f'), num_args = 1, long("from-timezone"), display_order(300), default_value_t=TzArgument::Tz(Tz::UTC))] + #[clap(short('f'), long("from-timezone"), display_order(300), default_value_t=TzArgument::Tz(Tz::UTC))] pub src_zone: TzArgument, /// name of offset of destination timezone (or 'list' to display all possible values - #[clap(short('t'), num_args = 1, long("to-timezone"), display_order(400), default_value_t=TzArgument::Tz(Tz::UTC))] + #[clap(short('t'), long("to-timezone"), display_order(400), default_value_t=TzArgument::Tz(Tz::UTC))] pub dst_zone: TzArgument, // /// convert only, but do not sort // #[clap(short('c'), long("convert-only"), display_order(450))] // pub(crate) dont_sort: bool, /// strict mode: do not only warn, but abort if an error occurs - #[clap(long("strict"), num_args = 0, display_order(500))] + #[clap(long("strict"), display_order(500))] pub(crate) strict_mode: bool, #[clap(flatten)] diff --git a/src/bin/cleanhive/cli.rs b/src/bin/cleanhive/cli.rs index 5b622dc..5712654 100644 --- a/src/bin/cleanhive/cli.rs +++ b/src/bin/cleanhive/cli.rs @@ -8,18 +8,18 @@ use log::LevelFilter; #[clap(name=env!("CARGO_BIN_NAME"), author, version)] pub (crate) struct Cli { /// name of the file to dump - #[clap(num_args=1, value_parser, value_hint=ValueHint::FilePath)] + #[clap(value_parser, value_hint=ValueHint::FilePath)] pub(crate) hive_file: Input, /// transaction LOG file(s). This argument can be specified one or two times. - #[clap(short('L'), long("log"), num_args=0.., value_parser, value_hint=ValueHint::FilePath)] + #[clap(short('L'), long("log"), value_parser, value_hint=ValueHint::FilePath)] pub(crate) logfiles: Vec, #[clap(flatten)] verbose: clap_verbosity_flag::Verbosity, /// name of the file to which the cleaned hive will be written. - #[clap(short('O'), long("output"), default_value="-", num_args=1, value_hint=ValueHint::FilePath, value_parser)] + #[clap(short('O'), long("output"), default_value="-", value_hint=ValueHint::FilePath, value_parser)] pub(crate) dst_hive: Output, } diff --git a/src/common/parse_cli.rs b/src/common/parse_cli.rs index 8c9081b..ffbbec9 100644 --- a/src/common/parse_cli.rs +++ b/src/common/parse_cli.rs @@ -1,7 +1,7 @@ use std::process::exit; -use clap::{value_parser, Arg, ArgAction, Parser}; -use clap_complete::{Generator, Shell}; +use clap::{value_parser, Arg, ArgAction, Parser, Command}; +use clap_complete::{generate, Generator, Shell}; use log::LevelFilter; use simplelog::{SimpleLogger, Config}; @@ -55,12 +55,14 @@ where .get_matches(); if let Some(generator) = matches.get_one::("autocomplete") { - let bin_name = cmd.get_name(); - let mut cmd = P::command().bin_name(bin_name); - //let _ = cmd.get_subcommands_mut().map(|s|s.set_bin_name(bin_name)); + let mut cmd = P::command(); - generator.generate(&P::command().bin_name(cmd.get_name()), &mut std::io::stdout()); + print_completions(*generator, &mut cmd); exit(0); } } } + +fn print_completions(gen: G, cmd: &mut Command) { + generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); +} \ No newline at end of file