1806: refactor(args): Switch to pico-args r=matklad a=Geobert

Fixes https://github.com/rust-analyzer/rust-analyzer/issues/1768

Co-authored-by: Geobert Quach <geobert@protonmail.com>
This commit is contained in:
bors[bot] 2019-09-10 17:37:21 +00:00 committed by GitHub
commit 1140a83c1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 297 additions and 114 deletions

30
Cargo.lock generated
View file

@ -184,16 +184,6 @@ dependencies = [
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "clap"
version = "2.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "clicolors-control"
version = "1.0.1"
@ -837,6 +827,11 @@ dependencies = [
"ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pico-args"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "ppv-lite86"
version = "0.2.5"
@ -930,9 +925,9 @@ dependencies = [
name = "ra_cli"
version = "0.1.0"
dependencies = [
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"flexi_logger 0.14.3 (registry+https://github.com/rust-lang/crates.io-index)",
"indicatif 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pico-args 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_batch 0.1.0",
"ra_db 0.1.0",
"ra_hir 0.1.0",
@ -1112,8 +1107,8 @@ dependencies = [
name = "ra_tools"
version = "0.1.0"
dependencies = [
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pico-args 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1594,14 +1589,6 @@ name = "text_unit"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.6"
@ -1796,7 +1783,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum chalk-rust-ir 0.1.0 (git+https://github.com/rust-lang/chalk.git)" = "<none>"
"checksum chalk-solve 0.1.0 (git+https://github.com/rust-lang/chalk.git)" = "<none>"
"checksum chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e8493056968583b0193c1bb04d6f7684586f3726992d6c573261941a895dbd68"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
"checksum clicolors-control 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90082ee5dcdd64dc4e9e0d37fbf3ee325419e39c0092191e0393df65518f741e"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum console 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b147390a412132d75d10dd3b7b175a69cf5fd95032f7503c7091b8831ba10242"
@ -1874,6 +1860,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum paste-impl 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "4214c9e912ef61bf42b81ba9a47e8aad1b2ffaf739ab162bf96d1e011f54e6c5"
"checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
"checksum petgraph 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3659d1ee90221741f65dd128d9998311b0e40c5d3c23a62445938214abce4f"
"checksum pico-args 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2fce25154205cf4360b456fd7d48994afe20663b77e3bd3d0a353a2fccf7f22c"
"checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b"
"checksum proc-macro-hack 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e688f31d92ffd7c1ddc57a1b4e6d773c0f2a14ee437a4b0a4f5a69c80eb221c8"
"checksum proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "175a40b9cf564ce9bf050654633dbf339978706b8ead1a907bb970b63185dd95"
@ -1931,7 +1918,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
"checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625"
"checksum text_unit 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e08bbcb7a3adbda0eb23431206b653bdad3d8dea311e72d36bf2215e27a42579"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865"
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"

View file

@ -6,7 +6,7 @@ authors = ["rust-analyzer developers"]
publish = false
[dependencies]
clap = { version = "2.32.0", default-features = false }
pico-args = "0.2.0"
flexi_logger = "0.14.0"
indicatif = "0.11.0"

72
crates/ra_cli/src/help.rs Normal file
View file

@ -0,0 +1,72 @@
pub const GLOBAL_HELP: &str = "ra-cli
USAGE:
ra_cli <SUBCOMMAND>
FLAGS:
-h, --help Prints help information
SUBCOMMANDS:
analysis-bench
analysis-stats
highlight
parse
symbols";
pub const ANALYSIS_BENCH_HELP: &str = "ra_cli-analysis-bench
USAGE:
ra_cli analysis-bench [FLAGS] [OPTIONS] [PATH]
FLAGS:
-h, --help Prints help information
-v, --verbose
OPTIONS:
--complete <PATH:LINE:COLUMN> Compute completions at this location
--highlight <PATH> Hightlight this file
ARGS:
<PATH> Project to analyse";
pub const ANALYSIS_STATS_HELP: &str = "ra-cli-analysis-stats
USAGE:
ra_cli analysis-stats [FLAGS] [OPTIONS] [PATH]
FLAGS:
-h, --help Prints help information
--memory-usage
-v, --verbose
OPTIONS:
-o <ONLY>
ARGS:
<PATH>";
pub const HIGHLIGHT_HELP: &str = "ra-cli-highlight
USAGE:
ra_cli highlight [FLAGS]
FLAGS:
-h, --help Prints help information
-r, --rainbow";
pub const SYMBOLS_HELP: &str = "ra-cli-symbols
USAGE:
ra_cli highlight [FLAGS]
FLAGS:
-h, --help Prints help inforamtion";
pub const PARSE_HELP: &str = "ra-cli-parse
USAGE:
ra_cli parse [FLAGS]
FLAGS:
-h, --help Prints help inforamtion
--no-dump";

View file

@ -1,10 +1,11 @@
mod analysis_stats;
mod analysis_bench;
mod help;
use std::{error::Error, io::Read};
use std::{error::Error, fmt::Write, io::Read};
use clap::{App, Arg, SubCommand};
use flexi_logger::Logger;
use pico_args::Arguments;
use ra_ide_api::{file_structure, Analysis};
use ra_prof::profile;
use ra_syntax::{AstNode, SourceFile};
@ -13,77 +14,89 @@ type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
fn main() -> Result<()> {
Logger::with_env().start()?;
let matches = App::new("ra-cli")
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("parse").arg(Arg::with_name("no-dump").long("--no-dump")))
.subcommand(SubCommand::with_name("symbols"))
.subcommand(
SubCommand::with_name("highlight")
.arg(Arg::with_name("rainbow").short("r").long("rainbow")),
)
.subcommand(
SubCommand::with_name("analysis-stats")
.arg(Arg::with_name("verbose").short("v").long("verbose"))
.arg(Arg::with_name("memory-usage").long("memory-usage"))
.arg(Arg::with_name("only").short("o").takes_value(true))
.arg(Arg::with_name("path")),
)
.subcommand(
SubCommand::with_name("analysis-bench")
.arg(Arg::with_name("verbose").short("v").long("verbose"))
.arg(
Arg::with_name("highlight")
.long("highlight")
.takes_value(true)
.conflicts_with("complete")
.value_name("PATH")
.help("highlight this file"),
)
.arg(
Arg::with_name("complete")
.long("complete")
.takes_value(true)
.conflicts_with("highlight")
.value_name("PATH:LINE:COLUMN")
.help("compute completions at this location"),
)
.arg(Arg::with_name("path").value_name("PATH").help("project to analyze")),
)
.get_matches();
match matches.subcommand() {
("parse", Some(matches)) => {
let subcommand = match std::env::args_os().nth(1) {
None => {
eprintln!("{}", help::GLOBAL_HELP);
return Ok(());
}
Some(s) => s,
};
let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect());
match &*subcommand.to_string_lossy() {
"parse" => {
if matches.contains(["-h", "--help"]) {
eprintln!("{}", help::PARSE_HELP);
return Ok(());
}
let no_dump = matches.contains("--no-dump");
matches.finish().or_else(handle_extra_flags)?;
let _p = profile("parsing");
let file = file()?;
if !matches.is_present("no-dump") {
if !no_dump {
println!("{:#?}", file.syntax());
}
std::mem::forget(file);
}
("symbols", _) => {
"symbols" => {
if matches.contains(["-h", "--help"]) {
eprintln!("{}", help::SYMBOLS_HELP);
return Ok(());
}
matches.finish().or_else(handle_extra_flags)?;
let file = file()?;
for s in file_structure(&file) {
println!("{:?}", s);
}
}
("highlight", Some(matches)) => {
"highlight" => {
if matches.contains(["-h", "--help"]) {
eprintln!("{}", help::HIGHLIGHT_HELP);
return Ok(());
}
let rainbow_opt = matches.contains(["-r", "--rainbow"]);
matches.finish().or_else(handle_extra_flags)?;
let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
let html = analysis.highlight_as_html(file_id, matches.is_present("rainbow")).unwrap();
let html = analysis.highlight_as_html(file_id, rainbow_opt).unwrap();
println!("{}", html);
}
("analysis-stats", Some(matches)) => {
let verbose = matches.is_present("verbose");
let memory_usage = matches.is_present("memory-usage");
let path = matches.value_of("path").unwrap_or("");
let only = matches.value_of("only");
analysis_stats::run(verbose, memory_usage, path.as_ref(), only)?;
"analysis-stats" => {
if matches.contains(["-h", "--help"]) {
eprintln!("{}", help::ANALYSIS_STATS_HELP);
return Ok(());
}
let verbose = matches.contains(["-v", "--verbose"]);
let memory_usage = matches.contains("--memory-usage");
let path: String = matches.value_from_str("--path")?.unwrap_or_default();
let only = matches.value_from_str(["-o", "--only"])?.map(|v: String| v.to_owned());
matches.finish().or_else(handle_extra_flags)?;
analysis_stats::run(
verbose,
memory_usage,
path.as_ref(),
only.as_ref().map(String::as_ref),
)?;
}
("analysis-bench", Some(matches)) => {
let verbose = matches.is_present("verbose");
let path = matches.value_of("path").unwrap_or("");
let op = if let Some(path) = matches.value_of("highlight") {
"analysis-bench" => {
if matches.contains(["-h", "--help"]) {
eprintln!("{}", help::ANALYSIS_BENCH_HELP);
return Ok(());
}
let verbose = matches.contains(["-v", "--verbose"]);
let path: String = matches.value_from_str("--path")?.unwrap_or_default();
let highlight_path = matches.value_from_str("--highlight")?;
let complete_path = matches.value_from_str("--complete")?;
if highlight_path.is_some() && complete_path.is_some() {
panic!("either --highlight or --complete must be set, not both")
}
let op = if let Some(path) = highlight_path {
let path: String = path;
analysis_bench::Op::Highlight { path: path.into() }
} else if let Some(path_line_col) = matches.value_of("complete") {
let (path_line, column) = rsplit_at_char(path_line_col, ':')?;
} else if let Some(path_line_col) = complete_path {
let path_line_col: String = path_line_col;
let (path_line, column) = rsplit_at_char(path_line_col.as_str(), ':')?;
let (path, line) = rsplit_at_char(path_line, ':')?;
analysis_bench::Op::Complete {
path: path.into(),
@ -93,13 +106,27 @@ fn main() -> Result<()> {
} else {
panic!("either --highlight or --complete must be set")
};
matches.finish().or_else(handle_extra_flags)?;
analysis_bench::run(verbose, path.as_ref(), op)?;
}
_ => unreachable!(),
_ => eprintln!("{}", help::GLOBAL_HELP),
}
Ok(())
}
fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
if let pico_args::Error::UnusedArgsLeft(flags) = e {
let mut invalid_flags = String::new();
for flag in flags {
write!(&mut invalid_flags, "{}, ", flag)?;
}
let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
Err(format!("Invalid flags: {}", invalid_flags).into())
} else {
Err(e.to_string().into())
}
}
fn file() -> Result<SourceFile> {
let text = read_stdin()?;
Ok(SourceFile::parse(&text).tree())

View file

@ -8,7 +8,7 @@ publish = false
[dependencies]
walkdir = "2.1.3"
itertools = "0.8.0"
clap = { version = "2.32.0", default-features = false }
pico-args = "0.2.0"
quote = "1.0.2"
proc-macro2 = "1.0.1"
ron = "0.5.1"

View file

@ -0,0 +1,45 @@
pub const GLOBAL_HELP: &str = "tasks
USAGE:
ra_tools <SUBCOMMAND>
FLAGS:
-h, --help Prints help information
SUBCOMMANDS:
format
format-hook
fuzz-tests
gen-syntax
gen-tests
install-ra
lint";
pub const INSTALL_RA_HELP: &str = "ra_tools-install-ra
USAGE:
ra_tools.exe install-ra [FLAGS]
FLAGS:
--client-code
-h, --help Prints help information
--jemalloc
--server";
pub fn print_no_param_subcommand_help(subcommand: &str) {
eprintln!(
"ra_tools-{}
USAGE:
ra_tools {}
FLAGS:
-h, --help Prints help information",
subcommand, subcommand
);
}
pub const INSTALL_RA_CONFLICT: &str =
"error: The argument `--server` cannot be used with `--client-code`
For more information try --help";

View file

@ -1,5 +1,8 @@
use clap::{App, Arg, SubCommand};
mod help;
use core::fmt::Write;
use core::str;
use pico_args::Arguments;
use ra_tools::{
gen_tests, generate_boilerplate, install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt,
Cmd, Overwrite, Result,
@ -20,45 +23,95 @@ struct ServerOpt {
}
fn main() -> Result<()> {
let matches = App::new("tasks")
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("gen-syntax"))
.subcommand(SubCommand::with_name("gen-tests"))
.subcommand(
SubCommand::with_name("install-ra")
.arg(Arg::with_name("server").long("--server"))
.arg(Arg::with_name("jemalloc").long("jemalloc"))
.arg(Arg::with_name("client-code").long("client-code").conflicts_with("server")),
)
.alias("install-code")
.subcommand(SubCommand::with_name("format"))
.subcommand(SubCommand::with_name("format-hook"))
.subcommand(SubCommand::with_name("fuzz-tests"))
.subcommand(SubCommand::with_name("lint"))
.get_matches();
match matches.subcommand() {
("install-ra", Some(matches)) => {
let subcommand = match std::env::args_os().nth(1) {
None => {
eprintln!("{}", help::GLOBAL_HELP);
return Ok(());
}
Some(s) => s,
};
let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect());
let subcommand = &*subcommand.to_string_lossy();
match subcommand {
"install-ra" | "install-code" => {
if matches.contains(["-h", "--help"]) {
eprintln!("{}", help::INSTALL_RA_HELP);
return Ok(());
}
let server = matches.contains("--server");
let client_code = matches.contains("--client-code");
if server && client_code {
eprintln!("{}", help::INSTALL_RA_CONFLICT);
return Ok(());
}
let jemalloc = matches.contains("--jemalloc");
matches.finish().or_else(handle_extra_flags)?;
let opts = InstallOpt {
client: if matches.is_present("server") { None } else { Some(ClientOpt::VsCode) },
server: if matches.is_present("client-code") {
None
} else {
Some(ServerOpt { jemalloc: matches.is_present("jemalloc") })
},
client: if server { None } else { Some(ClientOpt::VsCode) },
server: if client_code { None } else { Some(ServerOpt { jemalloc: jemalloc }) },
};
install(opts)?
}
("gen-tests", _) => gen_tests(Overwrite)?,
("gen-syntax", _) => generate_boilerplate(Overwrite)?,
("format", _) => run_rustfmt(Overwrite)?,
("format-hook", _) => install_format_hook()?,
("lint", _) => run_clippy()?,
("fuzz-tests", _) => run_fuzzer()?,
_ => unreachable!(),
"gen-tests" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
}
gen_tests(Overwrite)?
}
"gen-syntax" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
}
generate_boilerplate(Overwrite)?
}
"format" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
}
run_rustfmt(Overwrite)?
}
"format-hook" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
}
install_format_hook()?
}
"lint" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
}
run_clippy()?
}
"fuzz-tests" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
}
run_fuzzer()?
}
_ => eprintln!("{}", help::GLOBAL_HELP),
}
Ok(())
}
fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
if let pico_args::Error::UnusedArgsLeft(flags) = e {
let mut invalid_flags = String::new();
for flag in flags {
write!(&mut invalid_flags, "{}, ", flag)?;
}
let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
Err(format!("Invalid flags: {}", invalid_flags).into())
} else {
Err(e.to_string().into())
}
}
fn install(opts: InstallOpt) -> Result<()> {
if cfg!(target_os = "macos") {
fix_path_for_mac()?