diff --git a/src/uu/head/src/cli.rs b/src/uu/head/src/cli.rs new file mode 100644 index 00000000000..823d3869116 --- /dev/null +++ b/src/uu/head/src/cli.rs @@ -0,0 +1,84 @@ +// This file is part of the uutils coreutils package. +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +use clap::{Arg, ArgAction, Command}; +use std::ffi::OsString; +use uucore::format_usage; +use uucore::translate; + +pub mod options { + pub const BYTES: &str = "BYTES"; + pub const LINES: &str = "LINES"; + pub const QUIET: &str = "QUIET"; + pub const VERBOSE: &str = "VERBOSE"; + pub const ZERO: &str = "ZERO"; + pub const FILES: &str = "FILE"; + pub const PRESUME_INPUT_PIPE: &str = "-PRESUME-INPUT-PIPE"; +} + +pub fn uu_app() -> Command { + Command::new("head") + .version(uucore::crate_version!()) + .help_template(uucore::localized_help_template("head")) + .about(translate!("head-about")) + .override_usage(format_usage(&translate!("head-usage"))) + .infer_long_args(true) + .arg( + Arg::new(options::BYTES) + .short('c') + .long("bytes") + .value_name("[-]NUM") + .help(translate!("head-help-bytes")) + .overrides_with_all([options::BYTES, options::LINES]) + .allow_hyphen_values(true), + ) + .arg( + Arg::new(options::LINES) + .short('n') + .long("lines") + .value_name("[-]NUM") + .help(translate!("head-help-lines")) + .overrides_with_all([options::LINES, options::BYTES]) + .allow_hyphen_values(true), + ) + .arg( + Arg::new(options::QUIET) + .short('q') + .long("quiet") + .visible_alias("silent") + .help(translate!("head-help-quiet")) + .overrides_with_all([options::VERBOSE, options::QUIET]) + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new(options::VERBOSE) + .short('v') + .long("verbose") + .help(translate!("head-help-verbose")) + .overrides_with_all([options::QUIET, options::VERBOSE]) + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new(options::PRESUME_INPUT_PIPE) + .long("presume-input-pipe") + .alias("-presume-input-pipe") + .hide(true) + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new(options::ZERO) + .short('z') + .long("zero-terminated") + .help(translate!("head-help-zero-terminated")) + .overrides_with(options::ZERO) + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new(options::FILES) + .action(ArgAction::Append) + .value_parser(clap::value_parser!(OsString)) + .value_hint(clap::ValueHint::FilePath), + ) +} diff --git a/src/uu/head/src/head.rs b/src/uu/head/src/head.rs index 43ebc47aa6b..1fbb21dcb4c 100644 --- a/src/uu/head/src/head.rs +++ b/src/uu/head/src/head.rs @@ -5,7 +5,7 @@ // spell-checker:ignore (vars) seekable memrchr -use clap::{Arg, ArgAction, ArgMatches, Command}; +use clap::ArgMatches; use memchr::memrchr_iter; use std::ffi::OsString; use std::fs::File; @@ -18,20 +18,14 @@ use thiserror::Error; use uucore::display::{Quotable, print_verbatim}; use uucore::error::{FromIo, UError, UResult, USimpleError}; use uucore::line_ending::LineEnding; +use uucore::show; use uucore::translate; -use uucore::{format_usage, show}; const BUF_SIZE: usize = 65536; -mod options { - pub const BYTES: &str = "BYTES"; - pub const LINES: &str = "LINES"; - pub const QUIET: &str = "QUIET"; - pub const VERBOSE: &str = "VERBOSE"; - pub const ZERO: &str = "ZERO"; - pub const FILES: &str = "FILE"; - pub const PRESUME_INPUT_PIPE: &str = "-PRESUME-INPUT-PIPE"; -} +mod cli; +use crate::cli::options; +pub use crate::cli::uu_app; mod parse; mod take; @@ -66,71 +60,6 @@ impl UError for HeadError { type HeadResult = Result; -pub fn uu_app() -> Command { - Command::new("head") - .version(uucore::crate_version!()) - .help_template(uucore::localized_help_template("head")) - .about(translate!("head-about")) - .override_usage(format_usage(&translate!("head-usage"))) - .infer_long_args(true) - .arg( - Arg::new(options::BYTES) - .short('c') - .long("bytes") - .value_name("[-]NUM") - .help(translate!("head-help-bytes")) - .overrides_with_all([options::BYTES, options::LINES]) - .allow_hyphen_values(true), - ) - .arg( - Arg::new(options::LINES) - .short('n') - .long("lines") - .value_name("[-]NUM") - .help(translate!("head-help-lines")) - .overrides_with_all([options::LINES, options::BYTES]) - .allow_hyphen_values(true), - ) - .arg( - Arg::new(options::QUIET) - .short('q') - .long("quiet") - .visible_alias("silent") - .help(translate!("head-help-quiet")) - .overrides_with_all([options::VERBOSE, options::QUIET]) - .action(ArgAction::SetTrue), - ) - .arg( - Arg::new(options::VERBOSE) - .short('v') - .long("verbose") - .help(translate!("head-help-verbose")) - .overrides_with_all([options::QUIET, options::VERBOSE]) - .action(ArgAction::SetTrue), - ) - .arg( - Arg::new(options::PRESUME_INPUT_PIPE) - .long("presume-input-pipe") - .alias("-presume-input-pipe") - .hide(true) - .action(ArgAction::SetTrue), - ) - .arg( - Arg::new(options::ZERO) - .short('z') - .long("zero-terminated") - .help(translate!("head-help-zero-terminated")) - .overrides_with(options::ZERO) - .action(ArgAction::SetTrue), - ) - .arg( - Arg::new(options::FILES) - .action(ArgAction::Append) - .value_parser(clap::value_parser!(OsString)) - .value_hint(clap::ValueHint::FilePath), - ) -} - #[derive(Debug, PartialEq)] enum Mode { FirstLines(u64),