mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
Refactor arg parsing
This commit is contained in:
parent
90e61ac75d
commit
017f0e4e53
1 changed files with 168 additions and 114 deletions
|
@ -14,6 +14,15 @@ use ra_syntax::{AstNode, SourceFile};
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
|
type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
env_logger::try_init()?;
|
||||||
|
|
||||||
|
let command = Command::from_args()?;
|
||||||
|
command.run()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub enum Verbosity {
|
pub enum Verbosity {
|
||||||
Spammy,
|
Spammy,
|
||||||
|
@ -37,13 +46,36 @@ impl Verbosity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
enum Command {
|
||||||
env_logger::try_init()?;
|
Parse {
|
||||||
|
no_dump: bool,
|
||||||
|
},
|
||||||
|
Symbols,
|
||||||
|
Highlight {
|
||||||
|
rainbow: bool,
|
||||||
|
},
|
||||||
|
Stats {
|
||||||
|
verbosity: Verbosity,
|
||||||
|
randomize: bool,
|
||||||
|
memory_usage: bool,
|
||||||
|
only: Option<String>,
|
||||||
|
with_deps: bool,
|
||||||
|
path: String,
|
||||||
|
},
|
||||||
|
Bench {
|
||||||
|
verbose: bool,
|
||||||
|
path: String,
|
||||||
|
op: analysis_bench::Op,
|
||||||
|
},
|
||||||
|
HelpPrinted,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Command {
|
||||||
|
fn from_args() -> Result<Command> {
|
||||||
let mut matches = Arguments::from_env();
|
let mut matches = Arguments::from_env();
|
||||||
let subcommand = matches.subcommand()?.unwrap_or_default();
|
let subcommand = matches.subcommand()?.unwrap_or_default();
|
||||||
|
|
||||||
match subcommand.as_str() {
|
let command = match subcommand.as_str() {
|
||||||
"parse" => {
|
"parse" => {
|
||||||
if matches.contains(["-h", "--help"]) {
|
if matches.contains(["-h", "--help"]) {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
|
@ -57,18 +89,12 @@ FLAGS:
|
||||||
-h, --help Prints help inforamtion
|
-h, --help Prints help inforamtion
|
||||||
--no-dump"
|
--no-dump"
|
||||||
);
|
);
|
||||||
return Ok(());
|
return Ok(Command::HelpPrinted);
|
||||||
}
|
}
|
||||||
|
|
||||||
let no_dump = matches.contains("--no-dump");
|
let no_dump = matches.contains("--no-dump");
|
||||||
matches.finish().or_else(handle_extra_flags)?;
|
matches.finish().or_else(handle_extra_flags)?;
|
||||||
|
Command::Parse { no_dump }
|
||||||
let _p = profile("parsing");
|
|
||||||
let file = file()?;
|
|
||||||
if !no_dump {
|
|
||||||
println!("{:#?}", file.syntax());
|
|
||||||
}
|
|
||||||
std::mem::forget(file);
|
|
||||||
}
|
}
|
||||||
"symbols" => {
|
"symbols" => {
|
||||||
if matches.contains(["-h", "--help"]) {
|
if matches.contains(["-h", "--help"]) {
|
||||||
|
@ -82,15 +108,12 @@ USAGE:
|
||||||
FLAGS:
|
FLAGS:
|
||||||
-h, --help Prints help inforamtion"
|
-h, --help Prints help inforamtion"
|
||||||
);
|
);
|
||||||
return Ok(());
|
return Ok(Command::HelpPrinted);
|
||||||
}
|
}
|
||||||
|
|
||||||
matches.finish().or_else(handle_extra_flags)?;
|
matches.finish().or_else(handle_extra_flags)?;
|
||||||
|
|
||||||
let file = file()?;
|
Command::Symbols
|
||||||
for s in file_structure(&file) {
|
|
||||||
println!("{:?}", s);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
"highlight" => {
|
"highlight" => {
|
||||||
if matches.contains(["-h", "--help"]) {
|
if matches.contains(["-h", "--help"]) {
|
||||||
|
@ -105,15 +128,12 @@ FLAGS:
|
||||||
-h, --help Prints help information
|
-h, --help Prints help information
|
||||||
-r, --rainbow"
|
-r, --rainbow"
|
||||||
);
|
);
|
||||||
return Ok(());
|
return Ok(Command::HelpPrinted);
|
||||||
}
|
}
|
||||||
|
|
||||||
let rainbow_opt = matches.contains(["-r", "--rainbow"]);
|
let rainbow = matches.contains(["-r", "--rainbow"]);
|
||||||
matches.finish().or_else(handle_extra_flags)?;
|
matches.finish().or_else(handle_extra_flags)?;
|
||||||
|
Command::Highlight { rainbow }
|
||||||
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" => {
|
"analysis-stats" => {
|
||||||
if matches.contains(["-h", "--help"]) {
|
if matches.contains(["-h", "--help"]) {
|
||||||
|
@ -136,7 +156,7 @@ OPTIONS:
|
||||||
ARGS:
|
ARGS:
|
||||||
<PATH>"
|
<PATH>"
|
||||||
);
|
);
|
||||||
return Ok(());
|
return Ok(Command::HelpPrinted);
|
||||||
}
|
}
|
||||||
|
|
||||||
let verbosity = match (
|
let verbosity = match (
|
||||||
|
@ -163,14 +183,7 @@ ARGS:
|
||||||
trailing.pop().unwrap()
|
trailing.pop().unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
analysis_stats::run(
|
Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path }
|
||||||
verbosity,
|
|
||||||
memory_usage,
|
|
||||||
path.as_ref(),
|
|
||||||
only.as_ref().map(String::as_ref),
|
|
||||||
with_deps,
|
|
||||||
randomize,
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
"analysis-bench" => {
|
"analysis-bench" => {
|
||||||
if matches.contains(["-h", "--help"]) {
|
if matches.contains(["-h", "--help"]) {
|
||||||
|
@ -192,7 +205,7 @@ OPTIONS:
|
||||||
ARGS:
|
ARGS:
|
||||||
<PATH> Project to analyse"
|
<PATH> Project to analyse"
|
||||||
);
|
);
|
||||||
return Ok(());
|
return Ok(Command::HelpPrinted);
|
||||||
}
|
}
|
||||||
|
|
||||||
let verbose = matches.contains(["-v", "--verbose"]);
|
let verbose = matches.contains(["-v", "--verbose"]);
|
||||||
|
@ -208,11 +221,10 @@ ARGS:
|
||||||
"exactly one of `--highlight`, `--complete` or `--goto-def` must be set"
|
"exactly one of `--highlight`, `--complete` or `--goto-def` must be set"
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
matches.finish().or_else(handle_extra_flags)?;
|
Command::Bench { verbose, path, op }
|
||||||
|
|
||||||
analysis_bench::run(verbose, path.as_ref(), op)?;
|
|
||||||
}
|
}
|
||||||
_ => eprintln!(
|
_ => {
|
||||||
|
eprintln!(
|
||||||
"\
|
"\
|
||||||
ra-cli
|
ra-cli
|
||||||
|
|
||||||
|
@ -228,9 +240,51 @@ SUBCOMMANDS:
|
||||||
highlight
|
highlight
|
||||||
parse
|
parse
|
||||||
symbols"
|
symbols"
|
||||||
),
|
);
|
||||||
|
return Ok(Command::HelpPrinted);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(command)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(self) -> Result<()> {
|
||||||
|
match self {
|
||||||
|
Command::Parse { no_dump } => {
|
||||||
|
let _p = profile("parsing");
|
||||||
|
let file = file()?;
|
||||||
|
if !no_dump {
|
||||||
|
println!("{:#?}", file.syntax());
|
||||||
|
}
|
||||||
|
std::mem::forget(file);
|
||||||
|
}
|
||||||
|
Command::Symbols => {
|
||||||
|
let file = file()?;
|
||||||
|
for s in file_structure(&file) {
|
||||||
|
println!("{:?}", s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Command::Highlight { rainbow } => {
|
||||||
|
let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
|
||||||
|
let html = analysis.highlight_as_html(file_id, rainbow).unwrap();
|
||||||
|
println!("{}", html);
|
||||||
|
}
|
||||||
|
Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } => {
|
||||||
|
analysis_stats::run(
|
||||||
|
verbosity,
|
||||||
|
memory_usage,
|
||||||
|
path.as_ref(),
|
||||||
|
only.as_ref().map(String::as_ref),
|
||||||
|
with_deps,
|
||||||
|
randomize,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Command::Bench { verbose, path, op } => {
|
||||||
|
analysis_bench::run(verbose, path.as_ref(), op)?;
|
||||||
|
}
|
||||||
|
Command::HelpPrinted => (),
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
|
fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue