Skip to content

Commit a9eb599

Browse files
committed
Fix for when only verbosity flags are passed
1 parent 0d5e764 commit a9eb599

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

src/args.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ pub struct Options {
120120
pub locality: Option<Locality>,
121121
/// Gist command that's been issued.
122122
pub command: Command,
123-
/// Gist to operate on.
124-
pub gist: GistArg,
123+
/// Gist to operate on, if any.
124+
pub gist: Option<GistArg>,
125125
/// Arguments to the gist, if any.
126126
/// This is only used if command == Command::Run.
127127
pub gist_args: Option<Vec<String>>,
@@ -159,9 +159,10 @@ impl<'a> TryFrom<ArgMatches<'a>> for Options {
159159
let command = Command::from_str(cmd).unwrap_or(Command::Run);
160160

161161
// Parse out the gist argument.
162-
let gist = try!(GistArg::from_str(
163-
cmd_matches.value_of(ARG_GIST).unwrap()
164-
));
162+
let gist = match cmd_matches.value_of(ARG_GIST) {
163+
Some(g) => Some(try!(GistArg::from_str(g))),
164+
None => None,
165+
};
165166

166167
// For the "run" command, arguments may be provided.
167168
let mut gist_args = cmd_matches.values_of(ARG_GIST_ARGV)
@@ -557,4 +558,12 @@ mod tests {
557558
assert!(parse_from_argv(args).is_err(),
558559
"\"help\" command was incorrectly treated as gist command");
559560
}
561+
562+
/// Verify that you can call the program with just the verbosity flags.
563+
#[test]
564+
fn just_verbosity_works() {
565+
let args = vec!["gisht", "-v"];
566+
assert!(parse_from_argv(args).is_ok(),
567+
"Failed to parse command line with just the verbosity args");
568+
}
560569
}

src/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn main() {
138138
fn print_args_error(e: ArgsError) -> io::Result<()> {
139139
match e {
140140
ArgsError::Parse(ref e) =>
141-
// In case of generic parse error,
141+
// In case of a generic parse error,
142142
// message provided by the clap library will be the usage string.
143143
writeln!(&mut io::stderr(), "{}", e.message),
144144
e => {
@@ -206,12 +206,17 @@ fn ensure_app_dir(opts: &Options) -> Result<(), ExitCode> {
206206
/// This may include fetching a fresh gist from a host, or updating it.
207207
/// If an error occurred, returns the corresponding exit code.
208208
fn decode_gist(opts: &Options) -> Result<Gist, ExitCode> {
209-
let gist = match opts.gist {
210-
GistArg::Uri(ref uri) => {
209+
if opts.gist.is_none() {
210+
error!("No gist provided. Try --help?");
211+
return Err(exitcode::USAGE);
212+
}
213+
214+
let gist = match opts.gist.as_ref().unwrap() {
215+
&GistArg::Uri(ref uri) => {
211216
debug!("Gist {} specified as the argument", uri);
212217
Gist::from_uri(uri.clone())
213218
},
214-
GistArg::BrowserUrl(ref url) => {
219+
&GistArg::BrowserUrl(ref url) => {
215220
debug!("Gist URL `{}` specified as the argument", url);
216221
let url = url.as_str();
217222
let maybe_gist = try!(gist_from_url(url));

0 commit comments

Comments
 (0)