Skip to content

Commit 476ac4d

Browse files
committed
refactor: use flatten global opts
1 parent 3a88deb commit 476ac4d

File tree

8 files changed

+72
-151
lines changed

8 files changed

+72
-151
lines changed

src/args/mod.rs

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,8 @@ pub enum TimeStyle {
1717
Normal,
1818
}
1919

20-
// TODO: flatten options in struct?
2120
#[derive(Debug, Parser)]
22-
#[command(author, about, long_about = None, version)]
23-
pub struct Args {
24-
#[command(subcommand)]
25-
cmd: Option<Cmd>,
26-
27-
#[arg(verbatim_doc_comment)]
28-
/// Provide ID required by other options
29-
/// Example: cnb --id 114514 post --show
30-
#[arg(long)]
31-
pub id: Option<usize>,
32-
33-
#[arg(verbatim_doc_comment)]
34-
/// Reverse list output
35-
/// Example: cnb --rev ing list
36-
#[arg(long)]
37-
pub rev: bool,
38-
39-
#[arg(verbatim_doc_comment)]
40-
/// Skip items while request list
41-
/// Example: cnb --skip 2 ing list
42-
/// Use this option to save network I/O if some items of the list output are not needed
43-
/// If this option is required but not specified, it will be set to 0
44-
#[arg(long)]
45-
#[arg(short = 's')]
46-
#[arg(value_name = "LENGTH")]
47-
pub skip: Option<usize>,
48-
49-
#[arg(verbatim_doc_comment)]
50-
/// Take items while request list
51-
/// Example: cnb --take 2 ing list
52-
/// Use this option to save network I/O if only a subset of the list output are required
53-
/// <LENGTH> should be in the range [0,100]
54-
/// If <LENGTH> is greater than 100, it will be set to 100
55-
/// If this option is required but not specified, it will be set to 8
56-
#[arg(long)]
57-
#[arg(short = 't')]
58-
#[arg(value_name = "LENGTH")]
59-
pub take: Option<usize>,
60-
21+
pub struct GlobalOpt {
6122
#[arg(verbatim_doc_comment)]
6223
/// Execute with specific PAT
6324
/// Example: cnb --with-pat 'FOOBARBAZ' post --list
@@ -118,3 +79,47 @@ pub struct Args {
11879
#[arg(default_value_t = false)]
11980
pub quiet: bool,
12081
}
82+
83+
// TODO: flatten options in struct?
84+
#[derive(Debug, Parser)]
85+
#[command(author, about, long_about = None, version)]
86+
pub struct Args {
87+
#[command(subcommand)]
88+
pub cmd: Option<Cmd>,
89+
#[clap(flatten)]
90+
pub global_opt: GlobalOpt,
91+
92+
#[arg(verbatim_doc_comment)]
93+
/// Provide ID required by other options
94+
/// Example: cnb --id 114514 post --show
95+
#[arg(long)]
96+
pub id: Option<usize>,
97+
98+
#[arg(verbatim_doc_comment)]
99+
/// Reverse list output
100+
/// Example: cnb --rev ing list
101+
#[arg(long)]
102+
pub rev: bool,
103+
104+
#[arg(verbatim_doc_comment)]
105+
/// Skip items while request list
106+
/// Example: cnb --skip 2 ing list
107+
/// Use this option to save network I/O if some items of the list output are not needed
108+
/// If this option is required but not specified, it will be set to 0
109+
#[arg(long)]
110+
#[arg(short = 's')]
111+
#[arg(value_name = "LENGTH")]
112+
pub skip: Option<usize>,
113+
114+
#[arg(verbatim_doc_comment)]
115+
/// Take items while request list
116+
/// Example: cnb --take 2 ing list
117+
/// Use this option to save network I/O if only a subset of the list output are required
118+
/// <LENGTH> should be in the range [0,100]
119+
/// If <LENGTH> is greater than 100, it will be set to 100
120+
/// If this option is required but not specified, it will be set to 8
121+
#[arg(long)]
122+
#[arg(short = 't')]
123+
#[arg(value_name = "LENGTH")]
124+
pub take: Option<usize>,
125+
}

src/args/parser/fav.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@ pub fn list_fav(args: &Args) -> Option<(usize, usize)> {
77
Args {
88
cmd: Some(Cmd::Fav(cmd::fav::Opt { list: true })),
99
id: None,
10-
with_pat: _,
1110
rev: _,
1211
skip,
1312
take,
14-
debug: _,
15-
style: _,
16-
time_style: _,
17-
fail_on_error: _,
18-
quiet: _,
13+
global_opt: _,
1914
} => {
2015
let skip = get_skip(skip);
2116
let take = get_take(take);

src/args/parser/ing.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,10 @@ pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType, bool)> {
1313
comment: None,
1414
})),
1515
id: None,
16-
with_pat: _,
1716
rev: _,
1817
skip,
1918
take,
20-
debug: _,
21-
style: _,
22-
time_style: _,
23-
fail_on_error: _,
24-
quiet: _,
19+
global_opt: _,
2520
} => {
2621
let skip = get_skip(skip);
2722
let take = get_take(take);
@@ -43,15 +38,10 @@ pub fn publish_ing(args: &Args) -> Option<&String> {
4338
comment: None,
4439
})),
4540
id: None,
46-
with_pat: _,
4741
rev: false,
4842
skip: None,
4943
take: None,
50-
debug: _,
51-
style: _,
52-
time_style: _,
53-
fail_on_error: _,
54-
quiet: _,
44+
global_opt: _,
5545
} => content,
5646
_ => return None,
5747
}
@@ -68,15 +58,10 @@ pub fn comment_ing(args: &Args) -> Option<(&String, usize)> {
6858
comment: Some(content),
6959
})),
7060
id: Some(id),
71-
with_pat: _,
7261
rev: false,
7362
skip: None,
7463
take: None,
75-
debug: _,
76-
style: _,
77-
time_style: _,
78-
fail_on_error: _,
79-
quiet: _,
64+
global_opt: _,
8065
} => (content, *id),
8166
_ => return None,
8267
}

src/args/parser/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod news;
44
pub mod post;
55
pub mod user;
66

7-
use crate::args::Args;
7+
use crate::args::{Args, GlobalOpt};
88

99
fn get_skip(skip: &Option<usize>) -> usize {
1010
skip.unwrap_or(0)
@@ -20,15 +20,10 @@ pub const fn no_operation(args: &Args) -> bool {
2020
Args {
2121
cmd: None,
2222
id: None,
23-
with_pat: None,
2423
rev: false,
2524
skip: None,
2625
take: None,
27-
debug: _,
28-
style: _,
29-
time_style: _,
30-
fail_on_error: _,
31-
quiet: _,
26+
global_opt: GlobalOpt { with_pat: None, .. }
3227
}
3328
)
3429
}

src/args/parser/news.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@ pub fn list_news(args: &Args) -> Option<(usize, usize)> {
77
Args {
88
cmd: Some(Cmd::News(cmd::news::Opt { list: true })),
99
id: None,
10-
with_pat: _,
1110
rev: _,
1211
skip,
1312
take,
14-
debug: _,
15-
style: _,
16-
time_style: _,
17-
fail_on_error: _,
18-
quiet: _,
13+
global_opt: _,
1914
} => {
2015
let skip = get_skip(skip);
2116
let take = get_take(take);

src/args/parser/post.rs

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@ pub fn list_post(args: &Args) -> Option<(usize, usize)> {
1616
cmd: None,
1717
})),
1818
id: None,
19-
with_pat: _,
2019
rev: _,
2120
skip,
2221
take,
23-
debug: _,
24-
style: _,
25-
time_style: _,
26-
fail_on_error: _,
27-
quiet: _,
22+
global_opt: _,
2823
} => {
2924
let skip = get_skip(skip);
3025
let take = get_take(take);
@@ -49,15 +44,10 @@ pub fn show_post(args: &Args) -> Option<usize> {
4944
cmd: None,
5045
})),
5146
id: Some(id),
52-
with_pat: _,
5347
rev: false,
5448
skip: None,
5549
take: None,
56-
debug: _,
57-
style: _,
58-
time_style: _,
59-
fail_on_error: _,
60-
quiet: _,
50+
global_opt: _,
6151
} => *id,
6252
_ => return None,
6353
}
@@ -78,15 +68,10 @@ pub fn show_post_meta(args: &Args) -> Option<usize> {
7868
cmd: None,
7969
})),
8070
id: Some(id),
81-
with_pat: _,
8271
rev: false,
8372
skip: None,
8473
take: None,
85-
debug: _,
86-
style: _,
87-
time_style: _,
88-
fail_on_error: _,
89-
quiet: _,
74+
global_opt: _,
9075
} => *id,
9176
_ => return None,
9277
}
@@ -107,15 +92,10 @@ pub fn show_post_comment(args: &Args) -> Option<usize> {
10792
cmd: None,
10893
})),
10994
id: Some(id),
110-
with_pat: _,
11195
rev: _,
11296
skip: None,
11397
take: None,
114-
debug: _,
115-
style: _,
116-
time_style: _,
117-
fail_on_error: _,
118-
quiet: _,
98+
global_opt: _,
11999
} => *id,
120100
_ => return None,
121101
}
@@ -136,15 +116,10 @@ pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> {
136116
cmd: None,
137117
})),
138118
id: None,
139-
with_pat: _,
140119
rev: _,
141120
skip,
142121
take,
143-
debug: _,
144-
style: _,
145-
time_style: _,
146-
fail_on_error: _,
147-
quiet: _,
122+
global_opt: _,
148123
} => {
149124
let skip = get_skip(skip);
150125
let take = get_take(take);
@@ -169,15 +144,10 @@ pub fn delete_post(args: &Args) -> Option<usize> {
169144
cmd: None,
170145
})),
171146
id: Some(id),
172-
with_pat: _,
173147
rev: false,
174148
skip: None,
175149
take: None,
176-
debug: _,
177-
style: _,
178-
time_style: _,
179-
fail_on_error: _,
180-
quiet: _,
150+
global_opt: _,
181151
} => *id,
182152
_ => return None,
183153
}
@@ -203,15 +173,10 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
203173
}),
204174
})),
205175
id: None,
206-
with_pat: _,
207176
rev: _,
208177
skip: None,
209178
take: None,
210-
debug: _,
211-
style: _,
212-
time_style: _,
213-
fail_on_error: _,
214-
quiet: _,
179+
global_opt: _,
215180
} => (title, body, *publish),
216181
_ => return None,
217182
}
@@ -241,15 +206,10 @@ pub fn update_post(
241206
}),
242207
})),
243208
id: Some(id),
244-
with_pat: _,
245209
rev: _,
246210
skip: None,
247211
take: None,
248-
debug: _,
249-
style: _,
250-
time_style: _,
251-
fail_on_error: _,
252-
quiet: _,
212+
global_opt: _,
253213
} => (*id, title, body, publish),
254214
_ => return None,
255215
}

0 commit comments

Comments
 (0)