Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@

pub use gumdrop_derive::*;

use std::env::args;
use std::error::Error as StdError;
use std::fmt;
use std::slice::Iter;
Expand Down Expand Up @@ -530,6 +531,36 @@ pub trait Options: Sized {
Self::parse(&mut Parser::new(args, style))
}

/// Just print the usage to stdout based on parsed Options.
///
/// Useful for printing the same --help or default no-args output
/// for multiple cases.
fn print_usage(opts: Self) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason this can't be &self?

let args = args().collect::<Vec<_>>();

match opts.command_name() {
None => {
println!("Usage: {} [OPTIONS]", args[0]);
println!();
println!("{}", Self::usage());

if let Some(cmds) = Self::command_list() {
println!();
println!("Available commands:");
println!();
println!("{}", cmds);
}
}
Some(cmd) => {
let help = Self::command_usage(cmd).unwrap_or_default();

println!("Usage: {} {} [OPTIONS]", args[0], cmd);
println!();
println!("{}", help);
}
}
}

/// Parses arguments from the environment.
///
/// If an error is encountered, the error is printed to `stderr` and the
Expand All @@ -540,7 +571,6 @@ pub trait Options: Sized {
///
/// Otherwise, the parsed options are returned.
fn parse_args_or_exit(style: ParsingStyle) -> Self {
use std::env::args;
use std::process::exit;

let args = args().collect::<Vec<_>>();
Expand All @@ -551,27 +581,7 @@ pub trait Options: Sized {
});

if opts.help_requested() {
match opts.command_name() {
None => {
println!("Usage: {} [OPTIONS]", args[0]);
println!();
println!("{}", Self::usage());

if let Some(cmds) = Self::command_list() {
println!();
println!("Available commands:");
println!();
println!("{}", cmds);
}
}
Some(cmd) => {
let help = Self::command_usage(cmd).unwrap_or_default();

println!("Usage: {} {} [OPTIONS]", args[0], cmd);
println!();
println!("{}", help);
}
}
Self::print_usage(opts);
exit(0);
}

Expand Down