Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit 8f2ebc6

Browse files
committed
style: improve argument parsing
1 parent 40d08c5 commit 8f2ebc6

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/main.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ fn main() -> std::io::Result<()> {
2121
tracing_subscriber::Registry::default()
2222
.with(sentry::integrations::tracing::layer())
2323
.init();
24-
24+
fn parse_feature(feature: &str) -> Result<Feature, String> {
25+
match feature {
26+
"start" => Ok(Feature::Start),
27+
"stop" => Ok(Feature::Stop),
28+
"restart" => Ok(Feature::Restart),
29+
"logs" => Ok(Feature::SeeLogs),
30+
"commit" => Ok(Feature::Commit),
31+
"status" => Ok(Feature::Status),
32+
"setram" => Ok(Feature::SetRam),
33+
"backup" => Ok(Feature::Backup),
34+
_ => Err(format!("Invalid permission: {}", feature)),
35+
}
36+
}
2537
let _guard = sentry::init((
2638
"https://0512a7bb28624cfc848cdad08f2186a7@sentry.discloudbot.com/3",
2739
sentry::ClientOptions {
@@ -133,24 +145,30 @@ fn main() -> std::io::Result<()> {
133145
.subcommand(
134146
Command::new("allow")
135147
.about("Gives permissions to a moderator")
136-
.arg(Arg::new("id").value_parser(value_parser!(u128)).action(clap::ArgAction::Set))
148+
.arg(Arg::new("id").value_parser(value_parser!(u128)).action(clap::ArgAction::Set)
149+
.required(true))
150+
.arg(Arg::new("to").value_parser(["to"]).action(clap::ArgAction::Set).required(true))
137151
.arg(
138152
Arg::new("perm")
139-
.value_parser(value_parser!(Feature))
153+
.value_parser(parse_feature)
140154
.action(clap::ArgAction::Append)
141-
.multiple_occurrences(true)
155+
.min_values(1)
156+
.required(true)
142157
)
143158
)
144159
.subcommand(
145160
Command::new("deny")
146161
.about("Removes permissions from a moderator")
147-
.arg(Arg::new("id").value_parser(value_parser!(u128)).action(clap::ArgAction::Set))
148162
.arg(
149163
Arg::new("perm")
150-
.value_parser(value_parser!(Feature))
164+
.value_parser(parse_feature)
151165
.action(clap::ArgAction::Append)
152-
.multiple_occurrences(true)
166+
.min_values(1)
167+
.required(true)
153168
)
169+
.arg(Arg::new("to").value_parser(["to"]).action(clap::ArgAction::Set).required(true))
170+
.arg(Arg::new("id").value_parser(value_parser!(u128)).action(clap::ArgAction::Set)
171+
.required(true))
154172
)
155173
.after_help("Be careful with what people you add and what permissions you give: With Great Power comes Great Responsability.")
156174
);

0 commit comments

Comments
 (0)