From 138787f76e39f208f3a9a757d12b58d86504d1de Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Mon, 31 May 2021 17:07:43 +0900 Subject: Drop clap for pico-args We barely have any flags so it's not worth the compilation time or binary size to use clap. --- helix-term/Cargo.toml | 2 +- helix-term/src/application.rs | 8 +++--- helix-term/src/main.rs | 60 +++++++++++++++++++++++++++++-------------- 3 files changed, 45 insertions(+), 25 deletions(-) (limited to 'helix-term') diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index af710151..ece32395 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -24,7 +24,7 @@ tokio = { version = "1", features = ["full"] } num_cpus = "1" tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"] } crossterm = { version = "0.19", features = ["event-stream"] } -clap = { version = "3.0.0-beta.2 ", default-features = false, features = ["std", "cargo"] } +pico-args = "0.4" futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false } diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 42a46f1e..5ea6be2b 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -1,8 +1,6 @@ -use clap::ArgMatches as Args; - use helix_view::{document::Mode, Document, Editor, Theme, View}; -use crate::{compositor::Compositor, ui}; +use crate::{compositor::Compositor, ui, Args}; use log::{error, info}; @@ -47,8 +45,8 @@ impl Application { let size = compositor.size(); let mut editor = Editor::new(size); - if let Ok(files) = args.values_of_t::("files") { - for file in files { + if !args.files.is_empty() { + for file in args.files { editor.open(file, Action::VerticalSplit)?; } } else { diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 386c2333..9ca5b067 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -8,7 +8,6 @@ mod ui; use application::Application; -use clap::{App, Arg}; use std::path::PathBuf; use anyhow::Error; @@ -48,25 +47,48 @@ fn setup_logging(verbosity: u64) -> Result<(), fern::InitError> { Ok(()) } +pub struct Args { + files: Vec, +} + fn main() { - let args = clap::app_from_crate!() - .arg( - Arg::new("files") - .about("Sets the input file to use") - .required(false) - .multiple(true) - .index(1), - ) - .arg( - Arg::new("verbose") - .about("Increases logging verbosity each use for up to 3 times") - .short('v') - .takes_value(false) - .multiple_occurrences(true), - ) - .get_matches(); - - let verbosity: u64 = args.occurrences_of("verbose"); + let help = format!( + "\ +{} {} +{} +{} + +USAGE: + hx [FLAGS] [files]... + +ARGS: + ... Sets the input file to use + +FLAGS: + -h, --help Prints help information + -v Increases logging verbosity each use for up to 3 times + -V, --version Prints version information +", + env!("CARGO_PKG_NAME"), + env!("CARGO_PKG_VERSION"), + env!("CARGO_PKG_AUTHORS"), + env!("CARGO_PKG_DESCRIPTION"), + ); + + let mut pargs = pico_args::Arguments::from_env(); + + // Help has a higher priority and should be handled separately. + if pargs.contains(["-h", "--help"]) { + print!("{}", help); + std::process::exit(0); + } + + let args = Args { + files: pargs.finish().into_iter().map(|arg| arg.into()).collect(), + }; + + // let verbosity: u64 = args.occurrences_of("verbose"); + let verbosity: u64 = 0; setup_logging(verbosity).expect("failed to initialize logging."); -- cgit v1.2.3-70-g09d2