Skip to content

Commit

Permalink
use clap_complete::generate
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Starke committed Sep 8, 2023
1 parent d9a1d29 commit 997cf50
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/apps/mactime2/bodyfile/bodyfile_reader.rs
Original file line number Diff line number Diff line change
@@ -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<JoinHandle<()>>,
Expand Down Expand Up @@ -52,10 +52,10 @@ impl StreamWorker<String> for BodyfileReader {
}

impl StreamReader<String, ()> for BodyfileReader {
fn new (worker: JoinHandle<()>, rx: Receiver<String>) -> Self {
fn new(worker: JoinHandle<()>, rx: Receiver<String>) -> Self {
Self {
worker: Some(worker),
rx: Some(rx)
rx: Some(rx),
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/apps/mactime2/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)]
Expand Down
6 changes: 3 additions & 3 deletions src/bin/cleanhive/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputPath>,

#[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,
}

Expand Down
14 changes: 8 additions & 6 deletions src/common/parse_cli.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -55,12 +55,14 @@ where
.get_matches();

if let Some(generator) = matches.get_one::<Shell>("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<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}

0 comments on commit 997cf50

Please sign in to comment.