Skip to content

Commit

Permalink
Move printing completions to main
Browse files Browse the repository at this point in the history
  • Loading branch information
vv9k committed Sep 21, 2022
1 parent 3dea549 commit 4c8fa96
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions wutag_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ mod config;
mod fmt;
mod opt;

use clap::Parser;
use clap::{CommandFactory, Parser};

use app::App;
use config::Config;
use opt::Opts;
use opt::{Command, CompletionsOpts, Opts, Shell, APP_NAME};
use std::io;
use thiserror::Error as ThisError;

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

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

fn print_completions(opts: &CompletionsOpts) -> Result<()> {
use clap_complete::{
generate,
shells::{Bash, Elvish, Fish, PowerShell, Zsh},
};

let mut app = Opts::command();

match opts.shell {
Shell::Bash => generate(Bash, &mut app, APP_NAME, &mut io::stdout()),
Shell::Elvish => generate(Elvish, &mut app, APP_NAME, &mut io::stdout()),
Shell::Fish => generate(Fish, &mut app, APP_NAME, &mut io::stdout()),
Shell::PowerShell => generate(PowerShell, &mut app, APP_NAME, &mut io::stdout()),
Shell::Zsh => generate(Zsh, &mut app, APP_NAME, &mut io::stdout()),
}
Ok(())
}

fn main() {
let config = Config::load_default_location().unwrap_or_default();
let opts = Opts::parse();

if let Command::PrintCompletions(opts) = &opts.cmd {
if let Err(e) = print_completions(opts) {
eprintln!("Execution failed, reason: {}", e);
std::process::exit(1);
} else {
std::process::exit(0);
}
}

if let Err(e) = App::run(Opts::parse(), config) {
if let Err(e) = App::run(opts, config) {
eprintln!("Execution failed, reason: {}", e);
}
}

0 comments on commit 4c8fa96

Please sign in to comment.