mirror of
https://github.com/pythops/bluetui.git
synced 2026-05-06 13:31:35 +08:00
refactor: Un-nest code. Use clap-derive. (#122)
This commit is contained in:
20
src/cli.rs
20
src/cli.rs
@@ -1,17 +1,9 @@
|
||||
use clap::Parser;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{Command, arg, crate_description, crate_name, crate_version, value_parser};
|
||||
|
||||
pub fn cli() -> Command {
|
||||
Command::new(crate_name!())
|
||||
.about(crate_description!())
|
||||
.version(crate_version!())
|
||||
.arg(
|
||||
arg!(--config <config>)
|
||||
.short('c')
|
||||
.id("config")
|
||||
.required(false)
|
||||
.help("Config file path")
|
||||
.value_parser(value_parser!(PathBuf)),
|
||||
)
|
||||
#[derive(Parser)]
|
||||
#[command(version, about)]
|
||||
pub struct Args {
|
||||
#[arg(short, long)]
|
||||
pub config_path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
15
src/main.rs
15
src/main.rs
@@ -7,23 +7,22 @@ use bluetui::{
|
||||
rfkill,
|
||||
tui::Tui,
|
||||
};
|
||||
use clap::Parser;
|
||||
use ratatui::{Terminal, backend::CrosstermBackend};
|
||||
use std::{io, path::PathBuf, process::exit, sync::Arc};
|
||||
use std::{io, process::exit, sync::Arc};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> AppResult<()> {
|
||||
let args = cli::cli().get_matches();
|
||||
let args = cli::Args::parse();
|
||||
|
||||
let config_file_path = if let Some(config) = args.get_one::<PathBuf>("config") {
|
||||
if config.exists() {
|
||||
Some(config.to_owned())
|
||||
let config_file_path = args.config_path.map(|config_path| {
|
||||
if config_path.exists() {
|
||||
config_path.to_owned()
|
||||
} else {
|
||||
eprintln!("Config file not found");
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
});
|
||||
|
||||
rfkill::check()?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user