Skip to content

Commit

Permalink
Fix for when only verbosity flags are passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Xion committed Oct 11, 2017
1 parent 0d5e764 commit a9eb599
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
19 changes: 14 additions & 5 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ pub struct Options {
pub locality: Option<Locality>,
/// Gist command that's been issued.
pub command: Command,
/// Gist to operate on.
pub gist: GistArg,
/// Gist to operate on, if any.
pub gist: Option<GistArg>,
/// Arguments to the gist, if any.
/// This is only used if command == Command::Run.
pub gist_args: Option<Vec<String>>,
Expand Down Expand Up @@ -159,9 +159,10 @@ impl<'a> TryFrom<ArgMatches<'a>> for Options {
let command = Command::from_str(cmd).unwrap_or(Command::Run);

// Parse out the gist argument.
let gist = try!(GistArg::from_str(
cmd_matches.value_of(ARG_GIST).unwrap()
));
let gist = match cmd_matches.value_of(ARG_GIST) {
Some(g) => Some(try!(GistArg::from_str(g))),
None => None,
};

// For the "run" command, arguments may be provided.
let mut gist_args = cmd_matches.values_of(ARG_GIST_ARGV)
Expand Down Expand Up @@ -557,4 +558,12 @@ mod tests {
assert!(parse_from_argv(args).is_err(),
"\"help\" command was incorrectly treated as gist command");
}

/// Verify that you can call the program with just the verbosity flags.
#[test]
fn just_verbosity_works() {
let args = vec!["gisht", "-v"];
assert!(parse_from_argv(args).is_ok(),
"Failed to parse command line with just the verbosity args");
}
}
13 changes: 9 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn main() {
fn print_args_error(e: ArgsError) -> io::Result<()> {
match e {
ArgsError::Parse(ref e) =>
// In case of generic parse error,
// In case of a generic parse error,
// message provided by the clap library will be the usage string.
writeln!(&mut io::stderr(), "{}", e.message),
e => {
Expand Down Expand Up @@ -206,12 +206,17 @@ fn ensure_app_dir(opts: &Options) -> Result<(), ExitCode> {
/// This may include fetching a fresh gist from a host, or updating it.
/// If an error occurred, returns the corresponding exit code.
fn decode_gist(opts: &Options) -> Result<Gist, ExitCode> {
let gist = match opts.gist {
GistArg::Uri(ref uri) => {
if opts.gist.is_none() {
error!("No gist provided. Try --help?");
return Err(exitcode::USAGE);
}

let gist = match opts.gist.as_ref().unwrap() {
&GistArg::Uri(ref uri) => {
debug!("Gist {} specified as the argument", uri);
Gist::from_uri(uri.clone())
},
GistArg::BrowserUrl(ref url) => {
&GistArg::BrowserUrl(ref url) => {
debug!("Gist URL `{}` specified as the argument", url);
let url = url.as_str();
let maybe_gist = try!(gist_from_url(url));
Expand Down

0 comments on commit a9eb599

Please sign in to comment.