Skip to content

Commit 350b7ad

Browse files
committed
Refactor duplicated arguments
1 parent a34988e commit 350b7ad

File tree

2 files changed

+46
-53
lines changed

2 files changed

+46
-53
lines changed

src/cli/arguments.rs

+36-40
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use std::str::FromStr;
88
#[derive(Parser, Debug)]
99
pub(crate) struct Week {
1010
/// Week number (defaults to current week if omitted)
11-
#[arg(long = "week", short)]
11+
#[arg(long = "week", short = 'w')]
1212
pub(crate) number: Option<u8>,
1313

14-
/// N:th previous week counted from current week (defaults to 1 if N isn't specified)
14+
/// N:th previous week counted from current week (defaults to 1 if N is omitted)
1515
#[arg(
1616
long = "previous-week",
1717
short,
@@ -27,6 +27,34 @@ pub(crate) struct Week {
2727
pub(crate) year: Option<i32>,
2828
}
2929

30+
#[derive(Parser, Debug)]
31+
pub(crate) struct Task {
32+
/// Name of the job
33+
#[arg(long, short)]
34+
pub(crate) job: String,
35+
36+
/// Name of the task
37+
#[arg(long = "task", short = 't')]
38+
pub(crate) name: String,
39+
}
40+
41+
#[derive(Parser, Debug)]
42+
pub(crate) struct Day {
43+
/// Day(s) of the week, for example "tuesday"
44+
///
45+
/// Defaults to today if omitted
46+
///
47+
/// You can also specify multiple days, and/or a range of days, for example
48+
/// "monday-tuesday, friday"
49+
///
50+
/// Also accepts short day names like "mon", "tue", etc.
51+
#[arg(long, short, value_parser = parse_days_of_week)]
52+
pub(crate) day: Option<Days>,
53+
54+
#[command(flatten)]
55+
pub(crate) week: Week,
56+
}
57+
3058
#[derive(Debug, Clone, clap::ValueEnum)]
3159
pub(crate) enum Format {
3260
Json,
@@ -74,52 +102,20 @@ pub enum Command {
74102
/// Number of hours to set
75103
hours: f32,
76104

77-
/// Name of the job
78-
#[arg(long, short)]
79-
job: String,
80-
81-
/// Name of the task
82-
#[arg(long, short)]
83-
task: String,
84-
85-
/// Day(s) of the week, for example "tuesday"
86-
///
87-
/// Defaults to today if omitted
88-
///
89-
/// You can also specify multiple days, and/or a range of days, for example
90-
/// "monday-tuesday, friday"
91-
///
92-
/// Also accepts short day names like "mon", "tue", etc.
93-
#[arg(long, short, value_parser = parse_days_of_week)]
94-
day: Option<Days>,
105+
#[command(flatten)]
106+
task: Task,
95107

96108
#[command(flatten)]
97-
week: Week,
109+
day: Day,
98110
},
99111

100112
/// Remove hours on day(s) for a given job and task
101113
Clear {
102-
/// Name of the job
103-
#[arg(long, short)]
104-
job: String,
105-
106-
/// Name of the task
107-
#[arg(long, short)]
108-
task: String,
109-
110-
/// Day(s) of the week, for example "tuesday"
111-
///
112-
/// Defaults to today if omitted
113-
///
114-
/// You can also specify multiple days, and/or a range of days, for example
115-
/// "monday-tuesday, friday"
116-
///
117-
/// Also accepts short day names like "mon", "tue", etc.
118-
#[arg(long, short, value_parser = parse_days_of_week)]
119-
day: Option<Days>,
114+
#[command(flatten)]
115+
task: Task,
120116

121117
#[command(flatten)]
122-
week: Week,
118+
day: Day,
123119
},
124120

125121
/// Submit time sheet for week

src/main.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,16 @@ async fn main() -> anyhow::Result<()> {
5050
.get(week.number, week.previous, week.year, format)
5151
.await
5252
}
53-
Command::Set {
54-
hours,
55-
job,
56-
task,
57-
day,
58-
week,
59-
} => command_client.set(hours, day, week, &job, &task).await,
60-
Command::Clear {
61-
job,
62-
task,
63-
day,
64-
week,
65-
} => command_client.clear(&job, &task, day, week).await,
53+
Command::Set { hours, task, day } => {
54+
command_client
55+
.set(hours, day.day, day.week, &task.job, &task.name)
56+
.await
57+
}
58+
Command::Clear { task, day } => {
59+
command_client
60+
.clear(&task.job, &task.name, day.day, day.week)
61+
.await
62+
}
6663
Command::Submit { week } => command_client.submit(week).await,
6764
Command::Logout => command_client.logout().await,
6865
Command::Line(line) => match line {

0 commit comments

Comments
 (0)