Skip to content

Commit

Permalink
refactor: use flatten global opts
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 27, 2023
1 parent 3a88deb commit 476ac4d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 151 deletions.
85 changes: 45 additions & 40 deletions src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cmd>,

#[arg(verbatim_doc_comment)]
/// Provide ID required by other options
/// Example: cnb --id 114514 post --show
#[arg(long)]
pub id: Option<usize>,

#[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<usize>,

#[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
/// <LENGTH> should be in the range [0,100]
/// If <LENGTH> 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<usize>,

pub struct GlobalOpt {
#[arg(verbatim_doc_comment)]
/// Execute with specific PAT
/// Example: cnb --with-pat 'FOOBARBAZ' post --list
Expand Down Expand Up @@ -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<Cmd>,
#[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<usize>,

#[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<usize>,

#[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
/// <LENGTH> should be in the range [0,100]
/// If <LENGTH> 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<usize>,
}
7 changes: 1 addition & 6 deletions src/args/parser/fav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 3 additions & 18 deletions src/args/parser/ing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand Down
9 changes: 2 additions & 7 deletions src/args/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>) -> usize {
skip.unwrap_or(0)
Expand All @@ -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, .. }
}
)
}
7 changes: 1 addition & 6 deletions src/args/parser/news.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
56 changes: 8 additions & 48 deletions src/args/parser/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -49,15 +44,10 @@ pub fn show_post(args: &Args) -> Option<usize> {
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,
}
Expand All @@ -78,15 +68,10 @@ pub fn show_post_meta(args: &Args) -> Option<usize> {
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,
}
Expand All @@ -107,15 +92,10 @@ pub fn show_post_comment(args: &Args) -> Option<usize> {
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,
}
Expand All @@ -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);
Expand All @@ -169,15 +144,10 @@ pub fn delete_post(args: &Args) -> Option<usize> {
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,
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down
Loading

0 comments on commit 476ac4d

Please sign in to comment.