Use clap for parsing dump-syntax and dump-ts options

html_output
Wilfred Hughes 2022-01-29 12:10:35 +07:00
parent d0fce4c42f
commit d37a233b39
1 changed files with 25 additions and 33 deletions

@ -94,14 +94,22 @@ fn app() -> clap::App<'static> {
.about("A syntax aware diff.") .about("A syntax aware diff.")
.author("Wilfred Hughes") .author("Wilfred Hughes")
.arg( .arg(
Arg::new("dump-syntax").long("dump-syntax").help( Arg::new("dump-syntax")
"Parse a single file with tree-sitter and display the difftastic syntax tree.", .long("dump-syntax")
), .takes_value(true)
.value_name("PATH")
.help(
"Parse a single file with tree-sitter and display the difftastic syntax tree.",
),
) )
.arg( .arg(
Arg::new("dump-ts").long("dump-ts").help( Arg::new("dump-ts")
"Parse a single file with tree-sitter and display the tree-sitter parse tree.", .long("dump-ts")
), .takes_value(true)
.value_name("PATH")
.help(
"Parse a single file with tree-sitter and display the tree-sitter parse tree.",
),
) )
.arg( .arg(
Arg::new("paths") Arg::new("paths")
@ -115,37 +123,21 @@ fn app() -> clap::App<'static> {
fn parse_args() -> Mode { fn parse_args() -> Mode {
let matches = app().get_matches(); let matches = app().get_matches();
let args: Vec<_> = matches.values_of_lossy("paths").unwrap(); if let Some(path) = matches.value_of("dump-syntax") {
info!("CLI arguments: {:?}", args); return Mode::DumpSyntax {
path: path.to_string(),
if matches.is_present("dump-syntax") { };
if args.len() == 1 {
return Mode::DumpSyntax {
path: args[0].clone(),
};
} else {
// TODO: delegate this parsing to clap.
panic!(
"Error: --dump-syntax takes one argument, but got: {}",
args.len()
);
}
} }
if matches.is_present("dump-ts") { if let Some(path) = matches.value_of("dump-ts") {
if args.len() == 1 { return Mode::DumpTreeSitter {
return Mode::DumpTreeSitter { path: path.to_string(),
path: args[0].clone(), };
};
} else {
// TODO: delegate this parsing to clap.
panic!(
"Error: --dump-ts takes one argument, but got: {}",
args.len()
);
}
} }
let args: Vec<_> = matches.values_of_lossy("paths").unwrap();
info!("CLI arguments: {:?}", args);
// TODO: document these different ways of calling difftastic. // TODO: document these different ways of calling difftastic.
let (display_path, lhs_path, rhs_path) = match &args[..] { let (display_path, lhs_path, rhs_path) = match &args[..] {
[lhs_path, rhs_path] => ( [lhs_path, rhs_path] => (