feat: add experimental cli

This commit is contained in:
鲁树人
2024-09-04 21:31:28 +01:00
parent 1a282c0912
commit 2c53e4d950
7 changed files with 246 additions and 3 deletions

44
um_cli/src/main.rs Normal file
View File

@@ -0,0 +1,44 @@
use crate::cmd::Commands;
use anyhow::Result;
use clap::Parser;
use std::process::exit;
use std::time::Instant;
mod cmd;
#[derive(Parser)]
#[command(name = "um_cli")]
#[command(version = "0.1")]
#[command(about = "um_cli (rust ver.)", long_about = None)]
struct Cli {
#[clap(subcommand)]
command: Option<Commands>,
/// Time command
#[clap(long, short, action=clap::ArgAction::SetTrue)]
time: Option<bool>,
}
fn run_command(cli: &Cli) -> Result<i32> {
match &cli.command {
Some(Commands::QMCv1(cmd)) => cmd.run(),
None => {
// https://github.com/clap-rs/clap/issues/3857#issuecomment-1161796261
todo!("implement a sensible default command, similar to um/cli");
}
}
}
fn main() {
let cli = Cli::parse();
let start = Instant::now();
let code = run_command(&cli).unwrap_or_else(|err| {
eprintln!("failed to run command: {}", err);
-1
});
let duration = start.elapsed();
if let Some(true) = cli.time {
eprintln!("time: {:?}", duration);
};
exit(code);
}