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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::path::PathBuf;
use clap::{Arg, Command, ValueEnum, builder::PossibleValue, value_parser};
use pickems::datatypes::Name;
/// Report formats accepted by the `simulate --report` option.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ReportType {
/// Include every report.
All,
/// Basic 3-0, advancement, and 0-3 percentages.
Basic,
/// Opponent strength statistics.
Strength,
/// Recommended pick'em selections.
Picks,
/// Simulated outcome for user-provided picks.
Assess,
}
impl ValueEnum for ReportType {
fn value_variants<'a>() -> &'a [Self] {
&[
Self::All,
Self::Basic,
Self::Strength,
Self::Picks,
Self::Assess,
]
}
fn to_possible_value(&self) -> Option<PossibleValue> {
Some(match self {
Self::All => PossibleValue::new("all").help("includes all statistics"),
Self::Basic => PossibleValue::new("basic")
.help("3-0, advancement, and 0-3 percentages for each team"),
Self::Strength => PossibleValue::new("strength")
.help("relative strength of opponents faced for each team"),
Self::Picks => PossibleValue::new("picks").help("statistically optimal picks"),
Self::Assess => PossibleValue::new("assess").help("simulated outcome of picks"),
})
}
}
fn name_parser(s: &str) -> Result<Name, String> {
Name::try_new(s).map_err(|e| format!("{e}"))
}
fn iterations_parser(s: &str) -> Result<u64, String> {
let iterations = s.parse::<u64>().map_err(|e| format!("{e}"))?;
if iterations == 0 {
Err(String::from("iterations must be greater than 0"))
} else {
Ok(iterations)
}
}
fn sigma_parser(s: &str) -> Result<f32, String> {
let sigma = s.parse::<f32>().map_err(|e| format!("{e}"))?;
if sigma.is_finite() && sigma > 0.0 {
Ok(sigma)
} else {
Err(String::from("sigma must be finite and greater than 0"))
}
}
/// Additional command arguments that only apply to specific report modes.
#[derive(Clone)]
pub enum ExtraArgs {
/// Team-name picks required by the assess report.
Assess {
three_zero: [Name; 2],
advancing: [Name; 6],
zero_three: [Name; 2],
},
}
/// Parsed top-level command.
pub enum Args {
Simulate {
file: PathBuf,
sigma: f32,
iterations: u64,
report_type: ReportType,
extra_args: Option<Box<ExtraArgs>>,
},
Inspect {
file: PathBuf,
},
Wizard {
file: PathBuf,
},
Update {
path: PathBuf,
},
}
impl Args {
#[allow(clippy::too_many_lines)]
fn cmd() -> Command {
Command::new("pickems")
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
Command::new("simulate")
.about("Simulate tournament outcomes")
.arg(
Arg::new("file")
.short('f')
.long("file")
.help("Path to load input data from (.toml)")
.required(true)
.value_parser(value_parser!(PathBuf)),
)
.arg(
Arg::new("iterations")
.short('n')
.long("iterations")
.default_value("1000000")
.help("Number of iterations to run")
.value_parser(iterations_parser),
)
.arg(
Arg::new("sigma")
.short('s')
.long("sigma")
.default_value("800.0")
.help("Sigma value to use for win probability")
.value_parser(sigma_parser),
)
.arg(
Arg::new("report")
.short('r')
.long("report")
.default_value("basic")
.requires_ifs([
("assess", "three-zero"),
("assess", "advancing"),
("assess", "zero-three"),
])
.help("Report format to return")
.value_parser(value_parser!(ReportType)),
)
.arg(
Arg::new("three-zero")
.long("three-zero")
.required_if_eq("report", "assess")
.num_args(2)
.help("3-0 picks")
.value_parser(name_parser),
)
.arg(
Arg::new("advancing")
.long("advancing")
.required_if_eq("report", "assess")
.num_args(6)
.help("3-1/3-2 picks")
.value_parser(name_parser),
)
.arg(
Arg::new("zero-three")
.long("zero-three")
.required_if_eq("report", "assess")
.num_args(2)
.help("0-3 picks")
.value_parser(name_parser),
),
)
.subcommand(
Command::new("data")
.about("Inspect or create input data")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
Command::new("inspect")
.about("Inspect input data file")
.arg(
Arg::new("file")
.short('f')
.long("file")
.help("Path to load input data from (.toml)")
.required(true)
.value_parser(value_parser!(PathBuf)),
),
)
.subcommand(
Command::new("wizard")
.about("Use the data wizard to create an input data file")
.arg(
Arg::new("file")
.short('f')
.long("file")
.help("Path to save input data to (.toml)")
.required(true)
.value_parser(value_parser!(PathBuf)),
),
)
.subcommand(
Command::new("update")
.about("Retrieve new data files from the repository")
.arg(
Arg::new("path")
.short('p')
.long("path")
.help("Path to the directory containing input data files")
.required(true)
.value_parser(value_parser!(PathBuf)),
),
),
)
}
/// Parse process arguments into a top-level command.
pub fn parse() -> Option<Self> {
let matches = Self::cmd().get_matches();
if let Some(sim) = matches.subcommand_matches("simulate") {
let report_type = *sim.get_one::<ReportType>("report")?;
// `clap` has already enforced these arguments for `--report assess`;
// collect into fixed arrays so downstream code can rely on counts.
let extra_args = if report_type == ReportType::Assess {
Some(Box::new(ExtraArgs::Assess {
three_zero: sim
.get_many::<Name>("three-zero")?
.cloned()
.collect::<Vec<_>>()
.try_into()
.ok()?,
advancing: sim
.get_many::<Name>("advancing")?
.cloned()
.collect::<Vec<_>>()
.try_into()
.ok()?,
zero_three: sim
.get_many::<Name>("zero-three")?
.cloned()
.collect::<Vec<_>>()
.try_into()
.ok()?,
}))
} else {
None
};
return Some(Self::Simulate {
file: sim.get_one::<PathBuf>("file")?.clone(),
sigma: *sim.get_one::<f32>("sigma")?,
iterations: *sim.get_one::<u64>("iterations")?,
report_type,
extra_args,
});
} else if let Some(data) = matches.subcommand_matches("data") {
if let Some(inspect) = data.subcommand_matches("inspect") {
return Some(Self::Inspect {
file: inspect.get_one::<PathBuf>("file")?.clone(),
});
} else if let Some(wizard) = data.subcommand_matches("wizard") {
return Some(Self::Wizard {
file: wizard.get_one::<PathBuf>("file")?.clone(),
});
} else if let Some(update) = data.subcommand_matches("update") {
return Some(Self::Update {
path: update.get_one::<PathBuf>("path")?.clone(),
});
}
}
None
}
}