on master help sign in
Merge pull request #66 from IceDragon200/replaced-args-parser
7e86032Blaž Hrastnik
Cargo.lock−7
@@ -326,7 +326,6 @@ dependencies = [
326 "log",
326 "log",
327 "num_cpus",
327 "num_cpus",
328 "once_cell",
328 "once_cell",
329− "pico-args",
330 "pulldown-cmark",
329 "pulldown-cmark",
331 "serde",
330 "serde",
332 "serde_json",
331 "serde_json",
@@ -607,12 +606,6 @@ version = "2.1.0"
607source = "registry+https://github.com/rust-lang/crates.io-index"
606source = "registry+https://github.com/rust-lang/crates.io-index"
608checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
607checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
609
608
610−[[package]]
611−name = "pico-args"
612−version = "0.4.1"
613−source = "registry+https://github.com/rust-lang/crates.io-index"
614−checksum = "7d7afeb98c5a10e0bffcc7fc16e105b04d06729fac5fd6384aebf7ff5cb5a67d"
615−
616[[package]]
609[[package]]
617name = "pin-project-lite"
610name = "pin-project-lite"
618version = "0.2.6"
611version = "0.2.6"
helix-term/Cargo.toml−1
@@ -24,7 +24,6 @@ tokio = { version = "1", features = ["full"] }
24num_cpus = "1"
24num_cpus = "1"
25tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"] }
25tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"] }
26crossterm = { version = "0.19", features = ["event-stream"] }
26crossterm = { version = "0.19", features = ["event-stream"] }
27−pico-args = "0.4"
28
27
29futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }
28futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }
30
29
helix-term/src/main.rs+57−12
@@ -12,7 +12,7 @@ use helix_core::config_dir;
12
12
13use std::path::PathBuf;
13use std::path::PathBuf;
14
14
15−use anyhow::{Context, Result};
15+use anyhow::{Context, Error, Result};
16
16
17fn setup_logging(verbosity: u64) -> Result<()> {
17fn setup_logging(verbosity: u64) -> Result<()> {
18 let mut base_config = fern::Dispatch::new();
18 let mut base_config = fern::Dispatch::new();
@@ -48,9 +48,52 @@ fn setup_logging(verbosity: u64) -> Result<()> {
48}
48}
49
49
50pub struct Args {
50pub struct Args {
51+ display_help: bool,
52+ display_version: bool,
53+ verbosity: u64,
51 files: Vec<PathBuf>,
54 files: Vec<PathBuf>,
52}
55}
53
56
57+fn parse_args(mut args: Args) -> Result<Args> {
58+ let argv: Vec<String> = std::env::args().collect();
59+ let mut iter = argv.iter();
60+
61+ iter.next(); // skip the program, we don't care about that
62+
63+ while let Some(arg) = iter.next() {
64+ match arg.as_str() {
65+ "--" => break, // stop parsing at this point treat the remaining as files
66+ "--version" => args.display_version = true,
67+ "--help" => args.display_help = true,
68+ arg if arg.starts_with("--") => {
69+ return Err(Error::msg(format!(
70+ "unexpected double dash argument: {}",
71+ arg
72+ )))
73+ }
74+ arg if arg.starts_with('-') => {
75+ let arg = arg.get(1..).unwrap().chars();
76+ for chr in arg {
77+ match chr {
78+ 'v' => args.verbosity += 1,
79+ 'V' => args.display_version = true,
80+ 'h' => args.display_help = true,
81+ _ => return Err(Error::msg(format!("unexpected short arg {}", chr))),
82+ }
83+ }
84+ }
85+ arg => args.files.push(PathBuf::from(arg)),
86+ }
87+ }
88+
89+ // push the remaining args, if any to the files
90+ for filename in iter {
91+ args.files.push(PathBuf::from(filename));
92+ }
93+
94+ Ok(args)
95+}
96+
54#[tokio::main]
97#[tokio::main]
55async fn main() -> Result<()> {
98async fn main() -> Result<()> {
56 let help = format!(
99 let help = format!(
@@ -76,18 +119,24 @@ FLAGS:
76 env!("CARGO_PKG_DESCRIPTION"),
119 env!("CARGO_PKG_DESCRIPTION"),
77 );
120 );
78
121
79− let mut pargs = pico_args::Arguments::from_env();
122+ let mut args: Args = Args {
123+ display_help: false,
124+ display_version: false,
125+ verbosity: 0,
126+ files: [].to_vec(),
127+ };
128+
129+ args = parse_args(args).context("could not parse arguments")?;
80
130
81 // Help has a higher priority and should be handled separately.
131 // Help has a higher priority and should be handled separately.
82− if pargs.contains(["-h", "--help"]) {
132+ if args.display_help {
83 print!("{}", help);
133 print!("{}", help);
84 std::process::exit(0);
134 std::process::exit(0);
85 }
135 }
86
136
87− let mut verbosity: u64 = 0;
137+ if args.display_version {
88−
138+ println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
89− if pargs.contains("-v") {
139+ std::process::exit(0);
90− verbosity = 1;
91 }
140 }
92
141
93 let conf_dir = config_dir();
142 let conf_dir = config_dir();
@@ -96,11 +145,7 @@ FLAGS:
96 std::fs::create_dir(&conf_dir);
145 std::fs::create_dir(&conf_dir);
97 }
146 }
98
147
99− setup_logging(verbosity).context("failed to initialize logging")?;
148+ setup_logging(args.verbosity).context("failed to initialize logging")?;
100−
101− let args = Args {
102− files: pargs.finish().into_iter().map(|arg| arg.into()).collect(),
103− };
104
149
105 // initialize language registry
150 // initialize language registry
106 use helix_core::syntax::{Loader, LOADER};
151 use helix_core::syntax::{Loader, LOADER};