use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::commands::{
completions as cmd_completions, convert as cmd_convert, init as cmd_init,
install as cmd_install, uninstall as cmd_uninstall,
};
use crate::shell::Shell;
use crate::ui;
#[derive(Parser, Debug)]
#[command(
name = "wincd",
version,
about,
long_about,
propagate_version = true,
args_conflicts_with_subcommands = true
)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Command>,
pub path: Option<String>,
#[arg(short = 'w', long = "to-windows")]
pub to_windows: bool,
#[arg(short = 'm', long = "mixed")]
pub mixed: bool,
#[arg(short = 'p', long = "parent")]
pub parent: bool,
#[arg(short = 'f', long = "force")]
pub force: bool,
#[arg(short = 'v', long = "verbose")]
pub verbose: bool,
#[arg(long = "init", value_name = "SHELL", hide = true)]
pub init_legacy: Option<String>,
#[arg(long = "setup", hide = true)]
pub setup_legacy: bool,
#[arg(long = "uninstall", hide = true)]
pub uninstall_legacy: bool,
#[arg(long = "no-color", global = true)]
pub no_color: bool,
}
#[derive(Subcommand, Debug)]
pub enum Command {
Convert {
path: Option<String>,
#[arg(short = 'w', long = "to-windows")]
to_windows: bool,
#[arg(short = 'm', long = "mixed")]
mixed: bool,
#[arg(short = 'p', long = "parent")]
parent: bool,
#[arg(short = 'f', long = "force")]
force: bool,
#[arg(short = 'v', long = "verbose")]
verbose: bool,
},
Init {
shell: Shell,
},
Install {
#[arg(long = "shell")]
shell: Option<Shell>,
#[arg(long = "force")]
force: bool,
#[arg(short = 'y', long = "yes")]
yes: bool,
},
Uninstall {
#[arg(long = "shell")]
shell: Option<Shell>,
#[arg(short = 'y', long = "yes")]
yes: bool,
#[arg(long = "all-shells")]
all_shells: bool,
#[arg(long = "keep-binary")]
keep_binary: bool,
},
Completions {
shell: Shell,
},
}
pub fn run() -> i32 {
let cli = Cli::parse();
ui::init_color(cli.no_color);
let result: Result<i32> = match cli.command {
Some(Command::Convert {
path,
to_windows,
mixed,
parent,
force,
verbose,
}) => cmd_convert::run(cmd_convert::ConvertArgs {
path,
to_windows,
mixed,
parent,
force,
verbose,
}),
Some(Command::Init { shell }) => cmd_init::run(shell).map(|_| 0),
Some(Command::Install { shell, force, yes }) => {
cmd_install::run(cmd_install::InstallArgs { shell, force, yes }).map(|_| 0)
}
Some(Command::Uninstall {
shell,
yes,
all_shells,
keep_binary,
}) => cmd_uninstall::run(cmd_uninstall::UninstallArgs {
shell,
yes,
all_shells,
keep_binary,
})
.map(|_| 0),
Some(Command::Completions { shell }) => cmd_completions::run(shell).map(|_| 0),
None => {
if let Some(s) = cli.init_legacy {
let sh: Shell = match s.parse() {
Ok(sh) => sh,
Err(e) => {
ui::err(e);
return 1;
}
};
cmd_init::run(sh).map(|_| 0)
} else if cli.setup_legacy {
cmd_install::run(cmd_install::InstallArgs::default()).map(|_| 0)
} else if cli.uninstall_legacy {
cmd_uninstall::run(cmd_uninstall::UninstallArgs::default()).map(|_| 0)
} else {
cmd_convert::run(cmd_convert::ConvertArgs {
path: cli.path,
to_windows: cli.to_windows,
mixed: cli.mixed,
parent: cli.parent,
force: cli.force,
verbose: cli.verbose,
})
}
}
};
match result {
Ok(code) => code,
Err(e) => {
ui::err(format!("{:#}", e));
1
}
}
}