-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharguments.rs
More file actions
187 lines (161 loc) · 4.62 KB
/
arguments.rs
File metadata and controls
187 lines (161 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use super::day_parser::parse_days_of_week;
use crate::cli::week_parser::parse_week_and_part;
use crate::domain::models::{day, line_number::LineNumber};
use clap::{Parser, Subcommand, ValueEnum};
use color_print::cformat;
use serde::Serialize;
use std::str::FromStr;
#[derive(Parser, ValueEnum, Default, Debug, Clone, Serialize, Copy, PartialEq, Eq)]
pub(crate) enum WeekPart {
#[default]
#[serde(rename = "")]
WHOLE,
A,
B,
}
impl FromStr for WeekPart {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"a" => Ok(WeekPart::A),
"b" => Ok(WeekPart::B),
part => Err(format!("Invalid week part '{part}'")),
}
}
}
#[derive(Parser, Debug)]
pub(crate) struct Week {
/// Week number (defaults to current week if omitted)
#[arg(long = "week", short = 'w',value_parser = parse_week_and_part )]
pub(crate) week: Option<WeekAndPart>,
/// N:th previous week counted from current week (defaults to 1 if N is omitted)
#[arg(
long = "previous-week",
short,
value_name = "N",
conflicts_with = "week",
default_missing_value = Some("1"),
num_args(0..=1),
)]
pub(crate) previous: Option<u8>,
/// Year (defaults to current year if omitted)
#[arg(long, short, requires = "week")]
pub(crate) year: Option<i32>,
}
#[derive(Debug, Clone)]
pub(crate) struct WeekAndPart {
pub(crate) number: Option<u8>,
pub(crate) part: Option<WeekPart>,
}
#[derive(Parser, Debug)]
pub(crate) struct Task {
/// Name of the job
#[arg(long, short)]
pub(crate) job: String,
/// Name of the task
#[arg(long = "task", short = 't')]
pub(crate) name: String,
}
#[derive(Parser, Debug)]
pub(crate) struct Days {
/// Day(s) of the week, for example "tuesday"
///
/// Defaults to today if omitted
///
/// You can also specify multiple days, and/or a range of days, for example
/// "monday-tuesday, friday"
///
/// Also accepts short day names like "mon", "tue", etc.
#[arg(long = "day", short, value_parser = parse_days_of_week)]
pub(crate) days: Option<day::Days>,
#[command(flatten)]
pub(crate) week: Week,
}
#[derive(Debug, Clone, clap::ValueEnum)]
pub(crate) enum Format {
Json,
Table,
}
impl FromStr for Format {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"json" => Ok(Format::Json),
"table" => Ok(Format::Table),
format => Err(format!("Invalid format '{format}'")),
}
}
}
#[derive(Debug, Subcommand)]
pub enum Line {
/// Delete line based on line number (1-indexed)
Delete {
line_number: LineNumber,
#[command(flatten)]
week: Week,
},
}
#[derive(Debug, Subcommand)]
pub enum Command {
/// Get the time sheet for the current week
Get {
/// Output format
#[arg(long, short, default_value = "table")]
format: Format,
/// Show all rows, including those with no hours reported
#[arg(long)]
full: bool,
#[command(flatten)]
week: Week,
},
/// Set number of hours on day(s) for a given job and task
Set {
// TODO: change to string that allows "4:30" hours, etc.
/// Number of hours to set
hours: f32,
#[command(flatten)]
task: Task,
#[command(flatten)]
days: Days,
},
/// Remove hours on day(s) for a given job and task
Clear {
#[command(flatten)]
task: Task,
#[command(flatten)]
days: Days,
},
/// Submit time sheet for week
Submit {
#[command(flatten)]
week: Week,
},
/// Log out
Logout,
/// Operate on entire lines in the time sheet
#[command(subcommand)]
Line(Line),
}
#[derive(Parser, Debug)]
#[command(
author = "Melker Ulander",
about,
version,
arg_required_else_help = true,
after_help = cformat!("<bold,underline>Examples:</bold,underline>\
\n maconomy get \
\n maconomy get --full\
\n maconomy set 8 --job '<<job name>>' --task '<<task name>>' \
\n maconomy set 8 --job '<<job name>>' --task '<<task name>>' --day 'mon-wed, fri' --week 46 \
\n maconomy set 8 --job '<<job name>>' --task '<<task name>>' --day mo --previous-week 2 \
\n maconomy clear --job '<<job name>>' --task '<<task name>>' --day tuesday \
\n maconomy line delete 2 \
")
)]
pub struct Args {
#[command(subcommand)]
pub command: Command,
}
pub fn parse_arguments() -> Command {
Args::parse().command
}