From 476ac4d9818f56a426fe42c7ab3312fcdf7164ae Mon Sep 17 00:00:00 2001 From: Thaumy Date: Wed, 27 Sep 2023 12:18:49 +0800 Subject: [PATCH] refactor: use flatten global opts --- src/args/mod.rs | 85 ++++++++++++++++++++++------------------- src/args/parser/fav.rs | 7 +--- src/args/parser/ing.rs | 21 ++-------- src/args/parser/mod.rs | 9 +---- src/args/parser/news.rs | 7 +--- src/args/parser/post.rs | 56 ++++----------------------- src/args/parser/user.rs | 23 ++--------- src/main.rs | 15 ++++---- 8 files changed, 72 insertions(+), 151 deletions(-) diff --git a/src/args/mod.rs b/src/args/mod.rs index 435d319..d74b534 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -17,47 +17,8 @@ pub enum TimeStyle { Normal, } -// TODO: flatten options in struct? #[derive(Debug, Parser)] -#[command(author, about, long_about = None, version)] -pub struct Args { - #[command(subcommand)] - cmd: Option, - - #[arg(verbatim_doc_comment)] - /// Provide ID required by other options - /// Example: cnb --id 114514 post --show - #[arg(long)] - pub id: Option, - - #[arg(verbatim_doc_comment)] - /// Reverse list output - /// Example: cnb --rev ing list - #[arg(long)] - pub rev: bool, - - #[arg(verbatim_doc_comment)] - /// Skip items while request list - /// Example: cnb --skip 2 ing list - /// Use this option to save network I/O if some items of the list output are not needed - /// If this option is required but not specified, it will be set to 0 - #[arg(long)] - #[arg(short = 's')] - #[arg(value_name = "LENGTH")] - pub skip: Option, - - #[arg(verbatim_doc_comment)] - /// Take items while request list - /// Example: cnb --take 2 ing list - /// Use this option to save network I/O if only a subset of the list output are required - /// should be in the range [0,100] - /// If is greater than 100, it will be set to 100 - /// If this option is required but not specified, it will be set to 8 - #[arg(long)] - #[arg(short = 't')] - #[arg(value_name = "LENGTH")] - pub take: Option, - +pub struct GlobalOpt { #[arg(verbatim_doc_comment)] /// Execute with specific PAT /// Example: cnb --with-pat 'FOOBARBAZ' post --list @@ -118,3 +79,47 @@ pub struct Args { #[arg(default_value_t = false)] pub quiet: bool, } + +// TODO: flatten options in struct? +#[derive(Debug, Parser)] +#[command(author, about, long_about = None, version)] +pub struct Args { + #[command(subcommand)] + pub cmd: Option, + #[clap(flatten)] + pub global_opt: GlobalOpt, + + #[arg(verbatim_doc_comment)] + /// Provide ID required by other options + /// Example: cnb --id 114514 post --show + #[arg(long)] + pub id: Option, + + #[arg(verbatim_doc_comment)] + /// Reverse list output + /// Example: cnb --rev ing list + #[arg(long)] + pub rev: bool, + + #[arg(verbatim_doc_comment)] + /// Skip items while request list + /// Example: cnb --skip 2 ing list + /// Use this option to save network I/O if some items of the list output are not needed + /// If this option is required but not specified, it will be set to 0 + #[arg(long)] + #[arg(short = 's')] + #[arg(value_name = "LENGTH")] + pub skip: Option, + + #[arg(verbatim_doc_comment)] + /// Take items while request list + /// Example: cnb --take 2 ing list + /// Use this option to save network I/O if only a subset of the list output are required + /// should be in the range [0,100] + /// If is greater than 100, it will be set to 100 + /// If this option is required but not specified, it will be set to 8 + #[arg(long)] + #[arg(short = 't')] + #[arg(value_name = "LENGTH")] + pub take: Option, +} diff --git a/src/args/parser/fav.rs b/src/args/parser/fav.rs index 493503d..afafd53 100644 --- a/src/args/parser/fav.rs +++ b/src/args/parser/fav.rs @@ -7,15 +7,10 @@ pub fn list_fav(args: &Args) -> Option<(usize, usize)> { Args { cmd: Some(Cmd::Fav(cmd::fav::Opt { list: true })), id: None, - with_pat: _, rev: _, skip, take, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => { let skip = get_skip(skip); let take = get_take(take); diff --git a/src/args/parser/ing.rs b/src/args/parser/ing.rs index 0747a5b..1ec3af1 100644 --- a/src/args/parser/ing.rs +++ b/src/args/parser/ing.rs @@ -13,15 +13,10 @@ pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType, bool)> { comment: None, })), id: None, - with_pat: _, rev: _, skip, take, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => { let skip = get_skip(skip); let take = get_take(take); @@ -43,15 +38,10 @@ pub fn publish_ing(args: &Args) -> Option<&String> { comment: None, })), id: None, - with_pat: _, rev: false, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => content, _ => return None, } @@ -68,15 +58,10 @@ pub fn comment_ing(args: &Args) -> Option<(&String, usize)> { comment: Some(content), })), id: Some(id), - with_pat: _, rev: false, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => (content, *id), _ => return None, } diff --git a/src/args/parser/mod.rs b/src/args/parser/mod.rs index 72965dc..fe6ba72 100644 --- a/src/args/parser/mod.rs +++ b/src/args/parser/mod.rs @@ -4,7 +4,7 @@ pub mod news; pub mod post; pub mod user; -use crate::args::Args; +use crate::args::{Args, GlobalOpt}; fn get_skip(skip: &Option) -> usize { skip.unwrap_or(0) @@ -20,15 +20,10 @@ pub const fn no_operation(args: &Args) -> bool { Args { cmd: None, id: None, - with_pat: None, rev: false, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: GlobalOpt { with_pat: None, .. } } ) } diff --git a/src/args/parser/news.rs b/src/args/parser/news.rs index 230e97a..4ee5505 100644 --- a/src/args/parser/news.rs +++ b/src/args/parser/news.rs @@ -7,15 +7,10 @@ pub fn list_news(args: &Args) -> Option<(usize, usize)> { Args { cmd: Some(Cmd::News(cmd::news::Opt { list: true })), id: None, - with_pat: _, rev: _, skip, take, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => { let skip = get_skip(skip); let take = get_take(take); diff --git a/src/args/parser/post.rs b/src/args/parser/post.rs index 6a541e1..77d98ab 100644 --- a/src/args/parser/post.rs +++ b/src/args/parser/post.rs @@ -16,15 +16,10 @@ pub fn list_post(args: &Args) -> Option<(usize, usize)> { cmd: None, })), id: None, - with_pat: _, rev: _, skip, take, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => { let skip = get_skip(skip); let take = get_take(take); @@ -49,15 +44,10 @@ pub fn show_post(args: &Args) -> Option { cmd: None, })), id: Some(id), - with_pat: _, rev: false, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => *id, _ => return None, } @@ -78,15 +68,10 @@ pub fn show_post_meta(args: &Args) -> Option { cmd: None, })), id: Some(id), - with_pat: _, rev: false, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => *id, _ => return None, } @@ -107,15 +92,10 @@ pub fn show_post_comment(args: &Args) -> Option { cmd: None, })), id: Some(id), - with_pat: _, rev: _, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => *id, _ => return None, } @@ -136,15 +116,10 @@ pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> { cmd: None, })), id: None, - with_pat: _, rev: _, skip, take, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => { let skip = get_skip(skip); let take = get_take(take); @@ -169,15 +144,10 @@ pub fn delete_post(args: &Args) -> Option { cmd: None, })), id: Some(id), - with_pat: _, rev: false, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => *id, _ => return None, } @@ -203,15 +173,10 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> { }), })), id: None, - with_pat: _, rev: _, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => (title, body, *publish), _ => return None, } @@ -241,15 +206,10 @@ pub fn update_post( }), })), id: Some(id), - with_pat: _, rev: _, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } => (*id, title, body, publish), _ => return None, } diff --git a/src/args/parser/user.rs b/src/args/parser/user.rs index e9ee7cb..9b610ab 100644 --- a/src/args/parser/user.rs +++ b/src/args/parser/user.rs @@ -1,4 +1,4 @@ -use crate::args::{cmd, Args, Cmd}; +use crate::args::{cmd, Args, Cmd, GlobalOpt}; use crate::infra::option::IntoOption; pub fn login(args: &Args) -> Option<&String> { @@ -11,15 +11,10 @@ pub fn login(args: &Args) -> Option<&String> { info: false, })), id: None, - with_pat: None, rev: false, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: GlobalOpt { with_pat: None, .. }, } => pat, _ => return None, } @@ -36,15 +31,10 @@ pub const fn logout(args: &Args) -> bool { info: false, })), id: None, - with_pat: None, rev: false, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: GlobalOpt { with_pat: None, .. }, } ) } @@ -59,15 +49,10 @@ pub const fn user_info(args: &Args) -> bool { info: true, })), id: None, - with_pat: _, rev: false, skip: None, take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, + global_opt: _, } ) } diff --git a/src/main.rs b/src/main.rs index dc60213..4d6d5f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,15 +58,16 @@ async fn main() -> Result<()> { } let args: Args = Args::parse(); - if args.debug { + let global_opt = &args.global_opt; + if global_opt.debug { dbg!(&args); } - let pat = args.with_pat.clone().or_eval_result(session::get_pat); - let style = &args.style; - let time_style = &args.time_style; + let pat = global_opt.with_pat.clone().or_eval_result(session::get_pat); + let style = &global_opt.style; + let time_style = &global_opt.time_style; let rev = args.rev; - let foe = args.fail_on_error; + let foe = global_opt.fail_on_error; let output = match args { _ if let Some(pat) = parser::user::login(&args) => { @@ -190,7 +191,7 @@ async fn main() -> Result<()> { _ => "Invalid usage, follow '--help' for more information".to_owned() }; - if args.quiet { + if global_opt.quiet { return ().into_ok(); } @@ -202,7 +203,7 @@ async fn main() -> Result<()> { } else { format!("{}\n", output) }; - if args.debug { + if global_opt.debug { show_non_printable_chars(output) } else { output