Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/uu/head/src/cli.rs
Original file line number Diff line number Diff line change
@@ -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),
)
}
81 changes: 5 additions & 76 deletions src/uu/head/src/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -66,71 +60,6 @@ impl UError for HeadError {

type HeadResult<T> = Result<T, HeadError>;

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),
Expand Down
Loading