mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 13:48:50 +00:00
refactor(args): Switch to pico-args
This commit is contained in:
parent
e2ebb467bd
commit
735845d86e
4 changed files with 219 additions and 85 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -837,6 +837,11 @@ dependencies = [
|
||||||
"ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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]]
|
[[package]]
|
||||||
name = "ppv-lite86"
|
name = "ppv-lite86"
|
||||||
version = "0.2.5"
|
version = "0.2.5"
|
||||||
|
@ -930,9 +935,9 @@ dependencies = [
|
||||||
name = "ra_cli"
|
name = "ra_cli"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
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)",
|
"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)",
|
"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_batch 0.1.0",
|
||||||
"ra_db 0.1.0",
|
"ra_db 0.1.0",
|
||||||
"ra_hir 0.1.0",
|
"ra_hir 0.1.0",
|
||||||
|
@ -1873,6 +1878,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 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 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 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 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-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"
|
"checksum proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "175a40b9cf564ce9bf050654633dbf339978706b8ead1a907bb970b63185dd95"
|
||||||
|
|
|
@ -6,7 +6,7 @@ authors = ["rust-analyzer developers"]
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "2.32.0", default-features = false }
|
pico-args = "0.2.0"
|
||||||
flexi_logger = "0.14.0"
|
flexi_logger = "0.14.0"
|
||||||
indicatif = "0.11.0"
|
indicatif = "0.11.0"
|
||||||
|
|
||||||
|
|
96
crates/ra_cli/src/help.rs
Normal file
96
crates/ra_cli/src/help.rs
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
pub fn print_global_help() {
|
||||||
|
println!(
|
||||||
|
"ra-cli
|
||||||
|
|
||||||
|
USAGE:
|
||||||
|
ra_cli <SUBCOMMAND>
|
||||||
|
|
||||||
|
FLAGS:
|
||||||
|
-h, --help Prints help information
|
||||||
|
|
||||||
|
SUBCOMMANDS:
|
||||||
|
analysis-bench
|
||||||
|
analysis-stats
|
||||||
|
highlight
|
||||||
|
parse
|
||||||
|
symbols"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_analysis_bench_help() {
|
||||||
|
println!(
|
||||||
|
"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 fn print_analysis_stats_help() {
|
||||||
|
println!(
|
||||||
|
"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 fn print_highlight_help() {
|
||||||
|
println!(
|
||||||
|
"ra-cli-highlight
|
||||||
|
|
||||||
|
USAGE:
|
||||||
|
ra_cli highlight [FLAGS]
|
||||||
|
|
||||||
|
FLAGS:
|
||||||
|
-h, --help Prints help information
|
||||||
|
-r, --rainbow"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_symbols_help() {
|
||||||
|
println!(
|
||||||
|
"ra-cli-symbols
|
||||||
|
|
||||||
|
USAGE:
|
||||||
|
ra_cli highlight [FLAGS]
|
||||||
|
|
||||||
|
FLAGS:
|
||||||
|
-h, --help Prints help inforamtion"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_parse_help() {
|
||||||
|
println!(
|
||||||
|
"ra-cli-parse
|
||||||
|
|
||||||
|
USAGE:
|
||||||
|
ra_cli parse [FLAGS]
|
||||||
|
|
||||||
|
FLAGS:
|
||||||
|
-h, --help Prints help inforamtion
|
||||||
|
--no-dump"
|
||||||
|
)
|
||||||
|
}
|
|
@ -1,10 +1,11 @@
|
||||||
mod analysis_stats;
|
mod analysis_stats;
|
||||||
mod analysis_bench;
|
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 flexi_logger::Logger;
|
||||||
|
use pico_args::Arguments;
|
||||||
use ra_ide_api::{file_structure, Analysis};
|
use ra_ide_api::{file_structure, Analysis};
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
use ra_syntax::{AstNode, SourceFile};
|
use ra_syntax::{AstNode, SourceFile};
|
||||||
|
@ -13,93 +14,124 @@ type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
Logger::with_env().start()?;
|
Logger::with_env().start()?;
|
||||||
let matches = App::new("ra-cli")
|
|
||||||
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
|
let subcommand = std::env::args_os().nth(1);
|
||||||
.subcommand(SubCommand::with_name("parse").arg(Arg::with_name("no-dump").long("--no-dump")))
|
if subcommand.is_none() {
|
||||||
.subcommand(SubCommand::with_name("symbols"))
|
help::print_global_help();
|
||||||
.subcommand(
|
return Ok(());
|
||||||
SubCommand::with_name("highlight")
|
}
|
||||||
.arg(Arg::with_name("rainbow").short("r").long("rainbow")),
|
let subcommand = subcommand.unwrap();
|
||||||
)
|
let mut args: Vec<_> = std::env::args_os().collect();
|
||||||
.subcommand(
|
let mut matches = Arguments::from_vec(args.drain(2..).collect());
|
||||||
SubCommand::with_name("analysis-stats")
|
|
||||||
.arg(Arg::with_name("verbose").short("v").long("verbose"))
|
match &*subcommand.to_string_lossy() {
|
||||||
.arg(Arg::with_name("memory-usage").long("memory-usage"))
|
"parse" => {
|
||||||
.arg(Arg::with_name("only").short("o").takes_value(true))
|
if matches.contains(["-h", "--help"]) {
|
||||||
.arg(Arg::with_name("path")),
|
help::print_parse_help();
|
||||||
)
|
return Ok(());
|
||||||
.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 _p = profile("parsing");
|
|
||||||
let file = file()?;
|
|
||||||
if !matches.is_present("no-dump") {
|
|
||||||
println!("{:#?}", file.syntax());
|
|
||||||
}
|
|
||||||
std::mem::forget(file);
|
|
||||||
}
|
|
||||||
("symbols", _) => {
|
|
||||||
let file = file()?;
|
|
||||||
for s in file_structure(&file) {
|
|
||||||
println!("{:?}", s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
("highlight", Some(matches)) => {
|
|
||||||
let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
|
|
||||||
let html = analysis.highlight_as_html(file_id, matches.is_present("rainbow")).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-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::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, ':')?;
|
|
||||||
let (path, line) = rsplit_at_char(path_line, ':')?;
|
|
||||||
analysis_bench::Op::Complete {
|
|
||||||
path: path.into(),
|
|
||||||
line: line.parse()?,
|
|
||||||
column: column.parse()?,
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
panic!("either --highlight or --complete must be set")
|
let no_dump = matches.contains("--no-dump");
|
||||||
};
|
matches.finish().or_else(handle_extra_flags)?;
|
||||||
analysis_bench::run(verbose, path.as_ref(), op)?;
|
|
||||||
|
let _p = profile("parsing");
|
||||||
|
let file = file()?;
|
||||||
|
if !no_dump {
|
||||||
|
println!("{:#?}", file.syntax());
|
||||||
|
}
|
||||||
|
std::mem::forget(file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
"symbols" => {
|
||||||
|
if matches.contains(["-h", "--help"]) {
|
||||||
|
help::print_symbols_help();
|
||||||
|
return Ok(());
|
||||||
|
} else {
|
||||||
|
matches.finish().or_else(handle_extra_flags)?;
|
||||||
|
let file = file()?;
|
||||||
|
for s in file_structure(&file) {
|
||||||
|
println!("{:?}", s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"highlight" => {
|
||||||
|
if matches.contains(["-h", "--help"]) {
|
||||||
|
help::print_highlight_help();
|
||||||
|
return Ok(());
|
||||||
|
} else {
|
||||||
|
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, rainbow_opt).unwrap();
|
||||||
|
println!("{}", html);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"analysis-stats" => {
|
||||||
|
if matches.contains(["-h", "--help"]) {
|
||||||
|
help::print_analysis_stats_help();
|
||||||
|
return Ok(());
|
||||||
|
} else {
|
||||||
|
let verbose = matches.contains(["-v", "--verbose"]);
|
||||||
|
let memory_usage = matches.contains("--memory-usage");
|
||||||
|
let path = matches.value_from_str("--path")?.unwrap_or("".to_string());
|
||||||
|
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" => {
|
||||||
|
if matches.contains(["-h", "--help"]) {
|
||||||
|
help::print_analysis_bench_help();
|
||||||
|
return Ok(());
|
||||||
|
} else {
|
||||||
|
let verbose = matches.contains(["-v", "--verbose"]);
|
||||||
|
let path = matches.value_from_str("--path")?.unwrap_or("".to_string());
|
||||||
|
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) = 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(),
|
||||||
|
line: line.parse()?,
|
||||||
|
column: column.parse()?,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panic!("either --highlight or --complete must be set")
|
||||||
|
};
|
||||||
|
matches.finish().or_else(handle_extra_flags)?;
|
||||||
|
analysis_bench::run(verbose, path.as_ref(), op)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => help::print_global_help(),
|
||||||
}
|
}
|
||||||
Ok(())
|
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).expect("Error on write");
|
||||||
|
}
|
||||||
|
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> {
|
fn file() -> Result<SourceFile> {
|
||||||
let text = read_stdin()?;
|
let text = read_stdin()?;
|
||||||
Ok(SourceFile::parse(&text).tree())
|
Ok(SourceFile::parse(&text).tree())
|
||||||
|
|
Loading…
Reference in a new issue