| 1 |
use anyhow::Result; |
| 2 |
use helix_core::Position; |
| 3 |
use helix_view::tree::Layout; |
| 4 |
use indexmap::IndexMap; |
| 5 |
use std::path::{Path, PathBuf}; |
| 6 |
|
| 7 |
#[derive(Default)] |
| 8 |
pub struct Args { |
| 9 |
pub display_help: bool, |
| 10 |
pub display_version: bool, |
| 11 |
pub health: bool, |
| 12 |
pub health_arg: Option<String>, |
| 13 |
pub load_tutor: bool, |
| 14 |
pub fetch_grammars: bool, |
| 15 |
pub build_grammars: bool, |
| 16 |
pub strict: bool, |
| 17 |
pub split: Option<Layout>, |
| 18 |
pub verbosity: u64, |
| 19 |
pub log_file: Option<PathBuf>, |
| 20 |
pub config_file: Option<PathBuf>, |
| 21 |
pub files: IndexMap<PathBuf, Vec<Position>>, |
| 22 |
pub working_directory: Option<PathBuf>, |
| 23 |
} |
| 24 |
|
| 25 |
impl Args { |
| 26 |
#[allow(clippy::too_many_lines)] |
| 27 |
pub fn parse_args() -> Result<Args> { |
| 28 |
let mut args = Args::default(); |
| 29 |
let mut argv = std::env::args().peekable(); |
| 30 |
let mut line_number = 0; |
| 31 |
|
| 32 |
let mut insert_file_with_position = |file_with_position: &str| { |
| 33 |
let (filename, position) = parse_file(file_with_position); |
| 34 |
|
| 35 |
|
| 36 |
let filename = helix_stdx::path::canonicalize(filename); |
| 37 |
|
| 38 |
args.files |
| 39 |
.entry(filename) |
| 40 |
.and_modify(|positions| positions.push(position)) |
| 41 |
.or_insert_with(|| vec![position]); |
| 42 |
}; |
| 43 |
|
| 44 |
argv.next(); |
| 45 |
|
| 46 |
while let Some(arg) = argv.next() { |
| 47 |
match arg.as_str() { |
| 48 |
"--" => break, |
| 49 |
"--version" => args.display_version = true, |
| 50 |
"--help" => args.display_help = true, |
| 51 |
"--strict" => args.strict = true, |
| 52 |
"--tutor" => args.load_tutor = true, |
| 53 |
"--vsplit" => match args.split { |
| 54 |
Some(_) => anyhow::bail!("can only set a split once of a specific type"), |
| 55 |
None => args.split = Some(Layout::Vertical), |
| 56 |
}, |
| 57 |
"--hsplit" => match args.split { |
| 58 |
Some(_) => anyhow::bail!("can only set a split once of a specific type"), |
| 59 |
None => args.split = Some(Layout::Horizontal), |
| 60 |
}, |
| 61 |
"--health" => { |
| 62 |
args.health = true; |
| 63 |
args.health_arg = argv.next_if(|opt| !opt.starts_with('-')); |
| 64 |
} |
| 65 |
"-g" | "--grammar" => match argv.next().as_deref() { |
| 66 |
Some("fetch") => args.fetch_grammars = true, |
| 67 |
Some("build") => args.build_grammars = true, |
| 68 |
_ => { |
| 69 |
anyhow::bail!("--grammar must be followed by either 'fetch' or 'build'") |
| 70 |
} |
| 71 |
}, |
| 72 |
"-c" | "--config" => match argv.next().as_deref() { |
| 73 |
Some(path) => args.config_file = Some(path.into()), |
| 74 |
None => anyhow::bail!("--config must specify a path to read"), |
| 75 |
}, |
| 76 |
"--log" => match argv.next().as_deref() { |
| 77 |
Some(path) => args.log_file = Some(path.into()), |
| 78 |
None => anyhow::bail!("--log must specify a path to write"), |
| 79 |
}, |
| 80 |
"-w" | "--working-dir" => match argv.next().as_deref() { |
| 81 |
Some(path) => { |
| 82 |
args.working_directory = if Path::new(path).is_dir() { |
| 83 |
Some(PathBuf::from(path)) |
| 84 |
} else { |
| 85 |
anyhow::bail!( |
| 86 |
"--working-dir specified does not exist or is not a directory" |
| 87 |
) |
| 88 |
} |
| 89 |
} |
| 90 |
None => { |
| 91 |
anyhow::bail!("--working-dir must specify an initial working directory") |
| 92 |
} |
| 93 |
}, |
| 94 |
arg if arg.starts_with("--") => { |
| 95 |
anyhow::bail!("unexpected double dash argument: {}", arg) |
| 96 |
} |
| 97 |
arg if arg.starts_with('-') => { |
| 98 |
let arg = arg.get(1..).unwrap().chars(); |
| 99 |
for chr in arg { |
| 100 |
match chr { |
| 101 |
'v' => args.verbosity += 1, |
| 102 |
'V' => args.display_version = true, |
| 103 |
'h' => args.display_help = true, |
| 104 |
_ => anyhow::bail!("unexpected short arg {}", chr), |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
"+" => line_number = usize::MAX, |
| 109 |
arg if arg.starts_with('+') => { |
| 110 |
match arg[1..].parse::<usize>() { |
| 111 |
Ok(n) => line_number = n.saturating_sub(1), |
| 112 |
_ => insert_file_with_position(arg), |
| 113 |
}; |
| 114 |
} |
| 115 |
arg => insert_file_with_position(arg), |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
for arg in argv { |
| 121 |
insert_file_with_position(&arg); |
| 122 |
} |
| 123 |
|
| 124 |
if line_number != 0 { |
| 125 |
if let Some(first_position) = args |
| 126 |
.files |
| 127 |
.first_mut() |
| 128 |
.and_then(|(_, positions)| positions.first_mut()) |
| 129 |
{ |
| 130 |
first_position.row = line_number; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
Ok(args) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
pub(crate) fn parse_file(s: &str) -> (PathBuf, Position) { |
| 140 |
let def = || (PathBuf::from(s), Position::default()); |
| 141 |
if Path::new(s).exists() { |
| 142 |
return def(); |
| 143 |
} |
| 144 |
split_path_row_col(s) |
| 145 |
.or_else(|| split_path_row(s)) |
| 146 |
.unwrap_or_else(def) |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
fn split_path_row_col(s: &str) -> Option<(PathBuf, Position)> { |
| 153 |
let mut s = s.trim_end_matches(':').rsplitn(3, ':'); |
| 154 |
let col: usize = s.next()?.parse().ok()?; |
| 155 |
let row: usize = s.next()?.parse().ok()?; |
| 156 |
let path = s.next()?.into(); |
| 157 |
let pos = Position::new(row.saturating_sub(1), col.saturating_sub(1)); |
| 158 |
Some((path, pos)) |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
fn split_path_row(s: &str) -> Option<(PathBuf, Position)> { |
| 165 |
let (path, row) = s.trim_end_matches(':').rsplit_once(':')?; |
| 166 |
let row: usize = row.parse().ok()?; |
| 167 |
let path = path.into(); |
| 168 |
let pos = Position::new(row.saturating_sub(1), 0); |
| 169 |
Some((path, pos)) |
| 170 |
} |