diff options
| author | Teddy Wing | 2020-08-29 15:07:36 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2020-08-29 19:33:19 +0200 | 
| commit | 8706dc07bcf3b4c965de8e9dd050bd8b97ebc693 (patch) | |
| tree | 9a8582d5163a98ac4d85cd330199b4e4d1c819d6 | |
| parent | f8aa7f6c8a632546b4cb234abf0030a39be52eed (diff) | |
| download | git-suggestion-8706dc07bcf3b4c965de8e9dd050bd8b97ebc693.tar.bz2 | |
git-sugdiff: Use `diff_options.parse()` to extract diff options
Config::get():
Change `args` type so we can pass both a `Vec<String>` and
`Vec<&String>`.
diff_options.parse():
* Split diff arguments and program arguments by returning a tuple.
* Exit the args loop once we've parsed an argument.
* Update test
* Add a non-working test for suggestion arguments. An `OPT_OPTIONS`
  argument could be followed by an argument that's not the value of the
  `OPT_OPTIONS` option. Currently, that arg will always be parsed as the
  value. We need to handle this case.
| -rw-r--r-- | src/bin/git-sugdiff.rs | 6 | ||||
| -rw-r--r-- | src/config.rs | 5 | ||||
| -rw-r--r-- | src/diff_options.rs | 42 | 
3 files changed, 47 insertions, 6 deletions
| diff --git a/src/bin/git-sugdiff.rs b/src/bin/git-sugdiff.rs index 6b95239..13c3292 100644 --- a/src/bin/git-sugdiff.rs +++ b/src/bin/git-sugdiff.rs @@ -22,6 +22,7 @@ use exitcode;  use github_suggestion_cli::{gseprintln, for_suggestion};  use github_suggestion_cli::config::Config; +use github_suggestion_cli::diff_options;  fn main() { @@ -30,6 +31,10 @@ fn main() {      // TODO: Shift all diff options from args, then pass them to Config::get().      // Add diff options to Command call below. +    let (args, diff_args) = diff_options::parse(&args); +    dbg!(&args); +    dbg!(&diff_args); +      let config = match Config::get(          &args,          "usage: git sugdiff [options] <suggestion>...", @@ -56,6 +61,7 @@ fn main() {              match Command::new("git")                  .arg("--no-pager")                  .arg("diff") +                .args(&diff_args)                  .arg(format!("{}:{}", suggestion.commit(), suggestion.path()))                  .arg(blob.to_string())                  .spawn() diff --git a/src/config.rs b/src/config.rs index d8732d4..77793c4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -15,6 +15,7 @@  use std::env; +use std::ffi::OsStr;  use std::process;  use getopts::{self, Options}; @@ -57,7 +58,9 @@ pub struct Config {  impl Config {      /// Set up command line arguments. Extract configuration values from command      /// line arguments, Git config, and environment variables. -    pub fn get(args: &[String], usage_brief: &str) -> Result<Self, Error> { +    // pub fn get<C: IntoIterator>(args: C, usage_brief: &str) -> Result<Self, Error> +    // where C::Item: AsRef<OsStr> +    pub fn get<S: AsRef<OsStr>>(args: &[S], usage_brief: &str) -> Result<Self, Error> {          let mut opts = Options::new();          opts.optopt( diff --git a/src/diff_options.rs b/src/diff_options.rs index 587e214..fa0af8e 100644 --- a/src/diff_options.rs +++ b/src/diff_options.rs @@ -165,13 +165,15 @@ static OPT_OPTIONS: [&'static str; 19] = [  // pub fn parse(args: &[String]) -> &[String] { -pub fn parse(args: &[String]) -> Vec<&String> { +pub fn parse(args: &[String]) -> (Vec<&String>, Vec<&String>) { +    let mut program_args = Vec::new();      let mut found_args = Vec::new();      let mut add_next_arg = false; -    for arg in args { +    'args: for arg in args {          let find_arg_prefix = arg.find('-'); +        // TODO: Ignore suggestion args          if add_next_arg              && (                  find_arg_prefix.is_none() @@ -188,6 +190,8 @@ pub fn parse(args: &[String]) -> Vec<&String> {          for flag in FLAGS.iter() {              if arg.starts_with(flag) {                  found_args.push(arg); + +                continue 'args;              }          } @@ -216,6 +220,8 @@ pub fn parse(args: &[String]) -> Vec<&String> {                  if rest.find('=').is_none() {                      add_next_arg = true;                  } + +                continue 'args;              }          } @@ -230,11 +236,16 @@ pub fn parse(args: &[String]) -> Vec<&String> {                  if rest.find('=').is_none() {                      add_next_arg = true;                  } + +                continue 'args;              }          } + +        // TODO: Otherwise, add to normal arguments list +        program_args.push(arg)      } -    found_args +    (program_args, found_args)  } @@ -261,9 +272,9 @@ mod tests {              "--relative".to_owned(),          ]; -        let options = parse(&args); +        let (_, diff_opts) = parse(&args); -        assert_eq!(options, vec![ +        assert_eq!(diff_opts, vec![              "--diff-filter=A",              "-D",              "--color", @@ -278,6 +289,27 @@ mod tests {              "--relative",          ]);      } + +    #[test] +    fn parse_does_not_consume_suggestion_args() { +        let args = vec![ +            "--github-token".to_owned(), +            "MY_TOKEN".to_owned(), +            "--word-diff".to_owned(), +            "459692838".to_owned(), +            "https://github.com/teddywing/git-suggestion/pull/1#discussion_r459691747".to_owned(), +        ]; + +        let (options, diff_opts) = parse(&args); + +        assert_eq!(diff_opts, vec!["--word-diff"]); +        assert_eq!(options, vec![ +            "--github-token", +            "MY_TOKEN", +            "459692838", +            "https://github.com/teddywing/git-suggestion/pull/1#discussion_r459691747", +        ]); +    }  }  // -p | 
