diff options
| author | Fuwn <[email protected]> | 2021-03-29 22:51:27 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-03-29 22:51:27 +0000 |
| commit | fa29e724932d2b1ab36ebcd8a3735e2efcf790fc (patch) | |
| tree | d6d4ae58dc3360590294118bab9289881c76124b | |
| parent | feature: Allow `Makefile` to accept arguments (diff) | |
| download | whirl-fa29e724932d2b1ab36ebcd8a3735e2efcf790fc.tar.xz whirl-fa29e724932d2b1ab36ebcd8a3735e2efcf790fc.zip | |
format: Refactor CLI, use pure `clap` instead of StructOpt
| -rw-r--r-- | src/cli.rs | 58 | ||||
| -rw-r--r-- | src/main.rs | 36 |
2 files changed, 64 insertions, 30 deletions
@@ -1,25 +1,39 @@ use structopt::StructOpt; +use structopt::clap::{Shell, App, AppSettings, SubCommand, Arg}; -#[derive(StructOpt, Debug)] -pub enum Command { - Run, - Config, -} - -#[derive(StructOpt, Debug)] -#[structopt( -name = env!("CARGO_PKG_NAME"), -about = env!("CARGO_PKG_DESCRIPTION"), -version = env!("CARGO_PKG_VERSION"), -author = env!("CARGO_PKG_AUTHORS"), -)] -pub struct Opt { - #[structopt(short, long)] - pub debug: bool, - - #[structopt(short, long, parse(from_occurrences))] - pub verbose: u8, - - #[structopt(subcommand)] // help - pub command: Command, +pub fn cli<'b, 'a>() -> App<'a, 'b> { + App::new(env!("CARGO_PKG_NAME")) + .about(env!("CARGO_PKG_DESCRIPTION")) + .version(env!("CARGO_PKG_VERSION")) + .author(env!("CARGO_PKG_AUTHORS")) + .settings(&[ + AppSettings::SubcommandRequiredElseHelp, + ]) + .subcommands(vec![ + SubCommand::with_name("run") + .about("Start the WorldServer"), + SubCommand::with_name("config") + .setting(AppSettings::SubcommandRequiredElseHelp) + .subcommands(vec![ + SubCommand::with_name("show"), + ]), + SubCommand::with_name("completions") + .setting(AppSettings::SubcommandRequiredElseHelp) + .about("Generate shell completions") + .subcommands(vec![ + SubCommand::with_name("powershell"), + SubCommand::with_name("bash"), + SubCommand::with_name("elvish"), + SubCommand::with_name("zsh"), + SubCommand::with_name("fish"), + ]), + ]) + .args(&[ + Arg::with_name("debug") + .short("d") + .long("debug"), + Arg::with_name("trace") + .short("t") + .long("trace"), + ]) } diff --git a/src/main.rs b/src/main.rs index 322eabd..e3499b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,23 @@ +#[macro_use] +extern crate log; + use whirl::server::auto::server::AutoServer; use std::error::Error; use whirl::server::room::server::RoomServer; -use structopt::StructOpt; use whirl::config; -use whirl::cli::Command; use whirl::config::get_config; +use structopt::clap::{SubCommand, AppSettings, App, Arg, Shell}; +use whirl::cli::cli; #[tokio::main] async fn main() -> Result<(), Box<dyn Error>> { - let opt = whirl::cli::Opt::from_args(); + // Setup CLI + let matches = cli().get_matches(); // Set logging level let mut log_level = "whirl=error,whirl=warn,whirl=info".to_string(); - if opt.debug { log_level += ",whirl=debug"; } - if opt.verbose >= 1 { log_level += ",whirl=trace"; }; + if matches.is_present("debug") { log_level += ",whirl=debug"; } + if matches.is_present("trace") { log_level += ",whirl=trace"; } std::env::set_var("RUST_LOG", log_level); // Set database URL @@ -24,9 +28,25 @@ async fn main() -> Result<(), Box<dyn Error>> { pretty_env_logger::init(); // Handle CLI command - match opt.command { - Command::Run => run().await.unwrap(), - Command::Config => println!("{:#?}", config::get_config()), + if matches.is_present("run") { + run().await.unwrap(); + } else if let Some(cmd) = matches.subcommand_matches("config") { + if cmd.is_present("show") { + println!("{:#?}", config::get_config()); + } + } else if let Some(shell) = matches.subcommand_matches("completions") { + if shell.is_present("powershell") { + cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::PowerShell, "."); + } else if shell.is_present("bash") { + cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Bash, "."); + } else if shell.is_present("elvish") { + cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Elvish, "."); + } else if shell.is_present("zsh") { + cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Zsh, "."); + } else if shell.is_present("fish") { + cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Fish, "."); + } + debug!("generated shell completions"); } Ok(()) |