aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/args.rs
diff options
context:
space:
mode:
authorIvan Tham2021-06-07 04:40:21 +0000
committerIvan Tham2021-06-07 13:35:31 +0000
commitb5682f984b596da32662dc8b5918811318b0c59f (patch)
treeb76d1f4c111b4ca7c2e9c1d98ed5e5f71e31b661 /helix-term/src/args.rs
parent68affa3c598723a8b9451ef3dcceda83ae161e39 (diff)
Separate helix-term as a library
helix-term stuff will now be documented in rustdoc.
Diffstat (limited to 'helix-term/src/args.rs')
-rw-r--r--helix-term/src/args.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs
new file mode 100644
index 00000000..e2bb07c6
--- /dev/null
+++ b/helix-term/src/args.rs
@@ -0,0 +1,53 @@
+use anyhow::{Error, Result};
+use std::path::PathBuf;
+
+#[derive(Default)]
+pub struct Args {
+ pub display_help: bool,
+ pub display_version: bool,
+ pub verbosity: u64,
+ pub files: Vec<PathBuf>,
+}
+
+impl Args {
+ pub fn parse_args() -> Result<Args> {
+ let mut args = Args::default();
+ let argv: Vec<String> = std::env::args().collect();
+ let mut iter = argv.iter();
+
+ iter.next(); // skip the program, we don't care about that
+
+ while let Some(arg) = iter.next() {
+ 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,
+ arg if arg.starts_with("--") => {
+ return Err(Error::msg(format!(
+ "unexpected double dash argument: {}",
+ arg
+ )))
+ }
+ 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,
+ _ => return Err(Error::msg(format!("unexpected short arg {}", chr))),
+ }
+ }
+ }
+ arg => args.files.push(PathBuf::from(arg)),
+ }
+ }
+
+ // push the remaining args, if any to the files
+ for filename in iter {
+ args.files.push(PathBuf::from(filename));
+ }
+
+ Ok(args)
+ }
+}