Skip to content

Commit ed5c050

Browse files
Koenraad VerheydenJoshua Nelson
authored andcommitted
Add ArgRequiredElseHelp to clap commands without default behaviour
This ensures that the help message is printed every time, even if no subcommand matches.
1 parent ad59a5c commit ed5c050

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/bin/cratesfyi.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate rustwide;
1111
use std::env;
1212
use std::path::{Path, PathBuf};
1313

14-
use clap::{Arg, App, SubCommand};
14+
use clap::{Arg, App, AppSettings, SubCommand};
1515
use cratesfyi::{DocBuilder, RustwideBuilder, DocBuilderOptions, db};
1616
use cratesfyi::utils::add_crate_to_queue;
1717
use cratesfyi::start_web_server;
@@ -24,8 +24,10 @@ pub fn main() {
2424
let matches = App::new("cratesfyi")
2525
.version(cratesfyi::BUILD_VERSION)
2626
.about(env!("CARGO_PKG_DESCRIPTION"))
27+
.setting(AppSettings::ArgRequiredElseHelp)
2728
.subcommand(SubCommand::with_name("build")
2829
.about("Builds documentation in a chroot environment")
30+
.setting(AppSettings::ArgRequiredElseHelp)
2931
.arg(Arg::with_name("PREFIX")
3032
.short("P")
3133
.long("prefix")
@@ -89,6 +91,7 @@ pub fn main() {
8991
.help("run the server in the foreground instead of detaching a child")))
9092
.subcommand(SubCommand::with_name("database")
9193
.about("Database operations")
94+
.setting(AppSettings::ArgRequiredElseHelp)
9295
.subcommand(SubCommand::with_name("move-to-s3"))
9396
.subcommand(SubCommand::with_name("migrate")
9497
.about("Run database migrations")
@@ -115,6 +118,7 @@ pub fn main() {
115118
.help("Name of the crate to delete"))))
116119
.subcommand(SubCommand::with_name("queue")
117120
.about("Interactions with the build queue")
121+
.setting(AppSettings::ArgRequiredElseHelp)
118122
.subcommand(SubCommand::with_name("add")
119123
.about("Add a crate to the build queue")
120124
.arg(Arg::with_name("CRATE_NAME")
@@ -231,12 +235,15 @@ pub fn main() {
231235
let conn = db::connect_db().expect("failed to connect to the database");
232236
db::delete_crate(&conn, &name).expect("failed to delete the crate");
233237
}
238+
234239
} else if let Some(matches) = matches.subcommand_matches("start-web-server") {
235240
start_web_server(Some(matches.value_of("SOCKET_ADDR").unwrap_or("0.0.0.0:3000")));
241+
236242
} else if let Some(_) = matches.subcommand_matches("daemon") {
237243
let foreground = matches.subcommand_matches("daemon")
238244
.map_or(false, |opts| opts.is_present("FOREGROUND"));
239245
cratesfyi::utils::start_daemon(!foreground);
246+
240247
} else if let Some(matches) = matches.subcommand_matches("queue") {
241248
if let Some(matches) = matches.subcommand_matches("add") {
242249
let priority = matches.value_of("BUILD_PRIORITY").unwrap_or("5");
@@ -248,8 +255,6 @@ pub fn main() {
248255
matches.value_of("CRATE_VERSION").unwrap(),
249256
priority).expect("Could not add crate to queue");
250257
}
251-
} else {
252-
println!("{}", matches.usage());
253258
}
254259
}
255260

0 commit comments

Comments
 (0)