Skip to content

Commit 4c8fa96

Browse files
committed
Move printing completions to main
1 parent 3dea549 commit 4c8fa96

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

wutag_cli/src/main.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ mod config;
44
mod fmt;
55
mod opt;
66

7-
use clap::Parser;
7+
use clap::{CommandFactory, Parser};
88

99
use app::App;
1010
use config::Config;
11-
use opt::Opts;
11+
use opt::{Command, CompletionsOpts, Opts, Shell, APP_NAME};
12+
use std::io;
1213
use thiserror::Error as ThisError;
1314

1415
#[derive(Debug, ThisError)]
@@ -29,10 +30,38 @@ pub enum Error {
2930

3031
pub type Result<T> = std::result::Result<T, Error>;
3132

33+
fn print_completions(opts: &CompletionsOpts) -> Result<()> {
34+
use clap_complete::{
35+
generate,
36+
shells::{Bash, Elvish, Fish, PowerShell, Zsh},
37+
};
38+
39+
let mut app = Opts::command();
40+
41+
match opts.shell {
42+
Shell::Bash => generate(Bash, &mut app, APP_NAME, &mut io::stdout()),
43+
Shell::Elvish => generate(Elvish, &mut app, APP_NAME, &mut io::stdout()),
44+
Shell::Fish => generate(Fish, &mut app, APP_NAME, &mut io::stdout()),
45+
Shell::PowerShell => generate(PowerShell, &mut app, APP_NAME, &mut io::stdout()),
46+
Shell::Zsh => generate(Zsh, &mut app, APP_NAME, &mut io::stdout()),
47+
}
48+
Ok(())
49+
}
50+
3251
fn main() {
3352
let config = Config::load_default_location().unwrap_or_default();
53+
let opts = Opts::parse();
54+
55+
if let Command::PrintCompletions(opts) = &opts.cmd {
56+
if let Err(e) = print_completions(opts) {
57+
eprintln!("Execution failed, reason: {}", e);
58+
std::process::exit(1);
59+
} else {
60+
std::process::exit(0);
61+
}
62+
}
3463

35-
if let Err(e) = App::run(Opts::parse(), config) {
64+
if let Err(e) = App::run(opts, config) {
3665
eprintln!("Execution failed, reason: {}", e);
3766
}
3867
}

0 commit comments

Comments
 (0)