Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite flags.rs::parse_flags #2227

Closed
bartlomieju opened this issue Apr 27, 2019 · 1 comment · Fixed by #2237
Closed

Rewrite flags.rs::parse_flags #2227

bartlomieju opened this issue Apr 27, 2019 · 1 comment · Fixed by #2237

Comments

@bartlomieju
Copy link
Member

bartlomieju commented Apr 27, 2019

With recent numerous changes to CLI and more to come (#2213, #2215) I found myself in situation when it's harder and harder to properly test CLI parsing logic and not break anything.

That's why I'd like to propose yet another rewrite to:

pub fn parse_flags(matches: ArgMatches) -> DenoFlags {

Currently it only returns DenoFlags and choosing the actual subcommand and construction of argument array that is passed to script in done in main.rs. That's not elegant, and logic is split into multiple files and makes it impossible to properly test all entry points.

As previously suggested in #2157 it should be flags_from_vec that is only public function in flags.rs that takes care of parsing of CLI flags and choosing appropriate action.

So my proposition is to change what's returned from this function to:

enum DenoSubcommand {
   Fmt = "fmt",
   Info = "info",
   Types = "types",
   Fetch = "fetch",
   Repl = "repl",
   Run = "run",
}

// last parameter is `rest_argv` which is passed down 
// to script and available as Deno.args
fn flags_from_vec(args: Vec<String>) -> (DenoFlags, DenoSubcommand, Vec<String>)

Then we'll be able to nicely test everything:

  #[test]
  fn test_set_flags_x() {
    let cli_args = svec!["deno", "-r", "-D", "run", "script.ts", "--foo", "bar", "bazz"];
    let (flags, cmd, args) = flags_from_vec(cli_args);
    assert_eq!(cmd, DenoSubcommand::Run);
    assert_eq!(args, svec!["deno", "script.ts", "--foo", "bar", "bazz");
    assert_eq!(
      flags,
      DenoFlags {
        log_debug: true,
        reload: true,
        ..DenoFlags::default()
      }
    );
  }

With DenoSubcommand there will be simple match on it in main.rs that basically selects function to call, as DenoFlags and script args will be already parsed.

CC @ry

@ry
Copy link
Member

ry commented Apr 27, 2019

SGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants