2022-03-08 06:25:46 +01:00
|
|
|
use anyhow::Result;
|
2022-01-23 08:54:03 +01:00
|
|
|
use helix_core::Position;
|
2022-07-01 11:27:32 +02:00
|
|
|
use helix_view::tree::Layout;
|
2022-01-23 08:54:03 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-06-07 06:40:21 +02:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Args {
|
|
|
|
pub display_help: bool,
|
|
|
|
pub display_version: bool,
|
2022-03-08 06:25:46 +01:00
|
|
|
pub health: bool,
|
|
|
|
pub health_arg: Option<String>,
|
2021-10-28 03:23:46 +02:00
|
|
|
pub load_tutor: bool,
|
2022-02-14 03:08:11 +01:00
|
|
|
pub fetch_grammars: bool,
|
|
|
|
pub build_grammars: bool,
|
2022-07-01 11:27:32 +02:00
|
|
|
pub split: Option<Layout>,
|
2021-06-07 06:40:21 +02:00
|
|
|
pub verbosity: u64,
|
2022-08-04 06:05:52 +02:00
|
|
|
pub config_file: Option<PathBuf>,
|
2022-01-23 08:54:03 +01:00
|
|
|
pub files: Vec<(PathBuf, Position)>,
|
2021-06-07 06:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Args {
|
|
|
|
pub fn parse_args() -> Result<Args> {
|
|
|
|
let mut args = Args::default();
|
2022-03-08 06:25:46 +01:00
|
|
|
let mut argv = std::env::args().peekable();
|
2021-06-07 06:40:21 +02:00
|
|
|
|
2022-03-08 06:25:46 +01:00
|
|
|
argv.next(); // skip the program, we don't care about that
|
2021-06-07 06:40:21 +02:00
|
|
|
|
2022-03-08 06:25:46 +01:00
|
|
|
while let Some(arg) = argv.next() {
|
2021-06-07 06:40:21 +02:00
|
|
|
match arg.as_str() {
|
|
|
|
"--" => break, // stop parsing at this point treat the remaining as files
|
|
|
|
"--version" => args.display_version = true,
|
|
|
|
"--help" => args.display_help = true,
|
2021-10-28 03:23:46 +02:00
|
|
|
"--tutor" => args.load_tutor = true,
|
2022-07-01 11:27:32 +02:00
|
|
|
"--vsplit" => args.split = Some(Layout::Vertical),
|
|
|
|
"--hsplit" => args.split = Some(Layout::Horizontal),
|
2022-03-08 06:25:46 +01:00
|
|
|
"--health" => {
|
|
|
|
args.health = true;
|
|
|
|
args.health_arg = argv.next_if(|opt| !opt.starts_with('-'));
|
|
|
|
}
|
2022-03-08 07:13:15 +01:00
|
|
|
"-g" | "--grammar" => match argv.next().as_deref() {
|
|
|
|
Some("fetch") => args.fetch_grammars = true,
|
|
|
|
Some("build") => args.build_grammars = true,
|
|
|
|
_ => {
|
|
|
|
anyhow::bail!("--grammar must be followed by either 'fetch' or 'build'")
|
|
|
|
}
|
|
|
|
},
|
2022-08-04 06:05:52 +02:00
|
|
|
"-c" | "--config" => match argv.next().as_deref() {
|
|
|
|
Some(path) => args.config_file = Some(path.into()),
|
|
|
|
None => anyhow::bail!("--config must specify a path to read"),
|
|
|
|
},
|
2021-06-07 06:40:21 +02:00
|
|
|
arg if arg.starts_with("--") => {
|
2022-03-08 06:25:46 +01:00
|
|
|
anyhow::bail!("unexpected double dash argument: {}", arg)
|
2021-06-07 06:40:21 +02:00
|
|
|
}
|
|
|
|
arg if arg.starts_with('-') => {
|
|
|
|
let arg = arg.get(1..).unwrap().chars();
|
|
|
|
for chr in arg {
|
|
|
|
match chr {
|
|
|
|
'v' => args.verbosity += 1,
|
|
|
|
'V' => args.display_version = true,
|
|
|
|
'h' => args.display_help = true,
|
2022-03-08 06:25:46 +01:00
|
|
|
_ => anyhow::bail!("unexpected short arg {}", chr),
|
2021-06-07 06:40:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-23 08:54:03 +01:00
|
|
|
arg => args.files.push(parse_file(arg)),
|
2021-06-07 06:40:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// push the remaining args, if any to the files
|
2022-03-08 06:25:46 +01:00
|
|
|
for arg in argv {
|
|
|
|
args.files.push(parse_file(&arg));
|
2021-06-07 06:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(args)
|
|
|
|
}
|
|
|
|
}
|
2022-01-23 08:54:03 +01:00
|
|
|
|
|
|
|
/// Parse arg into [`PathBuf`] and position.
|
|
|
|
pub(crate) fn parse_file(s: &str) -> (PathBuf, Position) {
|
|
|
|
let def = || (PathBuf::from(s), Position::default());
|
|
|
|
if Path::new(s).exists() {
|
|
|
|
return def();
|
|
|
|
}
|
|
|
|
split_path_row_col(s)
|
|
|
|
.or_else(|| split_path_row(s))
|
|
|
|
.unwrap_or_else(def)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Split file.rs:10:2 into [`PathBuf`], row and col.
|
|
|
|
///
|
|
|
|
/// Does not validate if file.rs is a file or directory.
|
|
|
|
fn split_path_row_col(s: &str) -> Option<(PathBuf, Position)> {
|
|
|
|
let mut s = s.rsplitn(3, ':');
|
|
|
|
let col: usize = s.next()?.parse().ok()?;
|
|
|
|
let row: usize = s.next()?.parse().ok()?;
|
|
|
|
let path = s.next()?.into();
|
|
|
|
let pos = Position::new(row.saturating_sub(1), col.saturating_sub(1));
|
|
|
|
Some((path, pos))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Split file.rs:10 into [`PathBuf`] and row.
|
|
|
|
///
|
|
|
|
/// Does not validate if file.rs is a file or directory.
|
|
|
|
fn split_path_row(s: &str) -> Option<(PathBuf, Position)> {
|
2022-02-18 04:13:02 +01:00
|
|
|
let (path, row) = s.rsplit_once(':')?;
|
2022-01-23 08:54:03 +01:00
|
|
|
let row: usize = row.parse().ok()?;
|
|
|
|
let path = path.into();
|
|
|
|
let pos = Position::new(row.saturating_sub(1), 0);
|
|
|
|
Some((path, pos))
|
|
|
|
}
|