Permalink
Browse files

Fix for when only verbosity flags are passed

  • Loading branch information...
Xion committed Oct 11, 2017
1 parent 0d5e764 commit a9eb599168f5b6821aefa46dde0b0a89a41cd4e6
Showing with 23 additions and 9 deletions.
  1. +14 −5 src/args.rs
  2. +9 −4 src/main.rs
View
@@ -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>>,
@@ -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)
@@ -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");
}
}
View
@@ -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 => {
@@ -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));

0 comments on commit a9eb599

Please sign in to comment.