Skip to content

Commit 8062dae

Browse files
oech3cakebaker
authored andcommitted
head: split head.rs
1 parent aa66859 commit 8062dae

2 files changed

Lines changed: 89 additions & 76 deletions

File tree

src/uu/head/src/cli.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// This file is part of the uutils coreutils package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
use clap::{Arg, ArgAction, Command};
7+
use std::ffi::OsString;
8+
use uucore::format_usage;
9+
use uucore::translate;
10+
11+
pub mod options {
12+
pub const BYTES: &str = "BYTES";
13+
pub const LINES: &str = "LINES";
14+
pub const QUIET: &str = "QUIET";
15+
pub const VERBOSE: &str = "VERBOSE";
16+
pub const ZERO: &str = "ZERO";
17+
pub const FILES: &str = "FILE";
18+
pub const PRESUME_INPUT_PIPE: &str = "-PRESUME-INPUT-PIPE";
19+
}
20+
21+
pub fn uu_app() -> Command {
22+
Command::new("head")
23+
.version(uucore::crate_version!())
24+
.help_template(uucore::localized_help_template("head"))
25+
.about(translate!("head-about"))
26+
.override_usage(format_usage(&translate!("head-usage")))
27+
.infer_long_args(true)
28+
.arg(
29+
Arg::new(options::BYTES)
30+
.short('c')
31+
.long("bytes")
32+
.value_name("[-]NUM")
33+
.help(translate!("head-help-bytes"))
34+
.overrides_with_all([options::BYTES, options::LINES])
35+
.allow_hyphen_values(true),
36+
)
37+
.arg(
38+
Arg::new(options::LINES)
39+
.short('n')
40+
.long("lines")
41+
.value_name("[-]NUM")
42+
.help(translate!("head-help-lines"))
43+
.overrides_with_all([options::LINES, options::BYTES])
44+
.allow_hyphen_values(true),
45+
)
46+
.arg(
47+
Arg::new(options::QUIET)
48+
.short('q')
49+
.long("quiet")
50+
.visible_alias("silent")
51+
.help(translate!("head-help-quiet"))
52+
.overrides_with_all([options::VERBOSE, options::QUIET])
53+
.action(ArgAction::SetTrue),
54+
)
55+
.arg(
56+
Arg::new(options::VERBOSE)
57+
.short('v')
58+
.long("verbose")
59+
.help(translate!("head-help-verbose"))
60+
.overrides_with_all([options::QUIET, options::VERBOSE])
61+
.action(ArgAction::SetTrue),
62+
)
63+
.arg(
64+
Arg::new(options::PRESUME_INPUT_PIPE)
65+
.long("presume-input-pipe")
66+
.alias("-presume-input-pipe")
67+
.hide(true)
68+
.action(ArgAction::SetTrue),
69+
)
70+
.arg(
71+
Arg::new(options::ZERO)
72+
.short('z')
73+
.long("zero-terminated")
74+
.help(translate!("head-help-zero-terminated"))
75+
.overrides_with(options::ZERO)
76+
.action(ArgAction::SetTrue),
77+
)
78+
.arg(
79+
Arg::new(options::FILES)
80+
.action(ArgAction::Append)
81+
.value_parser(clap::value_parser!(OsString))
82+
.value_hint(clap::ValueHint::FilePath),
83+
)
84+
}

src/uu/head/src/head.rs

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// spell-checker:ignore (vars) seekable memrchr
77

8-
use clap::{Arg, ArgAction, ArgMatches, Command};
8+
use clap::ArgMatches;
99
use memchr::memrchr_iter;
1010
use std::ffi::OsString;
1111
use std::fs::File;
@@ -18,20 +18,14 @@ use thiserror::Error;
1818
use uucore::display::{Quotable, print_verbatim};
1919
use uucore::error::{FromIo, UError, UResult, USimpleError};
2020
use uucore::line_ending::LineEnding;
21+
use uucore::show;
2122
use uucore::translate;
22-
use uucore::{format_usage, show};
2323

2424
const BUF_SIZE: usize = 65536;
2525

26-
mod options {
27-
pub const BYTES: &str = "BYTES";
28-
pub const LINES: &str = "LINES";
29-
pub const QUIET: &str = "QUIET";
30-
pub const VERBOSE: &str = "VERBOSE";
31-
pub const ZERO: &str = "ZERO";
32-
pub const FILES: &str = "FILE";
33-
pub const PRESUME_INPUT_PIPE: &str = "-PRESUME-INPUT-PIPE";
34-
}
26+
mod cli;
27+
use crate::cli::options;
28+
pub use crate::cli::uu_app;
3529

3630
mod parse;
3731
mod take;
@@ -66,71 +60,6 @@ impl UError for HeadError {
6660

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

69-
pub fn uu_app() -> Command {
70-
Command::new("head")
71-
.version(uucore::crate_version!())
72-
.help_template(uucore::localized_help_template("head"))
73-
.about(translate!("head-about"))
74-
.override_usage(format_usage(&translate!("head-usage")))
75-
.infer_long_args(true)
76-
.arg(
77-
Arg::new(options::BYTES)
78-
.short('c')
79-
.long("bytes")
80-
.value_name("[-]NUM")
81-
.help(translate!("head-help-bytes"))
82-
.overrides_with_all([options::BYTES, options::LINES])
83-
.allow_hyphen_values(true),
84-
)
85-
.arg(
86-
Arg::new(options::LINES)
87-
.short('n')
88-
.long("lines")
89-
.value_name("[-]NUM")
90-
.help(translate!("head-help-lines"))
91-
.overrides_with_all([options::LINES, options::BYTES])
92-
.allow_hyphen_values(true),
93-
)
94-
.arg(
95-
Arg::new(options::QUIET)
96-
.short('q')
97-
.long("quiet")
98-
.visible_alias("silent")
99-
.help(translate!("head-help-quiet"))
100-
.overrides_with_all([options::VERBOSE, options::QUIET])
101-
.action(ArgAction::SetTrue),
102-
)
103-
.arg(
104-
Arg::new(options::VERBOSE)
105-
.short('v')
106-
.long("verbose")
107-
.help(translate!("head-help-verbose"))
108-
.overrides_with_all([options::QUIET, options::VERBOSE])
109-
.action(ArgAction::SetTrue),
110-
)
111-
.arg(
112-
Arg::new(options::PRESUME_INPUT_PIPE)
113-
.long("presume-input-pipe")
114-
.alias("-presume-input-pipe")
115-
.hide(true)
116-
.action(ArgAction::SetTrue),
117-
)
118-
.arg(
119-
Arg::new(options::ZERO)
120-
.short('z')
121-
.long("zero-terminated")
122-
.help(translate!("head-help-zero-terminated"))
123-
.overrides_with(options::ZERO)
124-
.action(ArgAction::SetTrue),
125-
)
126-
.arg(
127-
Arg::new(options::FILES)
128-
.action(ArgAction::Append)
129-
.value_parser(clap::value_parser!(OsString))
130-
.value_hint(clap::ValueHint::FilePath),
131-
)
132-
}
133-
13463
#[derive(Debug, PartialEq)]
13564
enum Mode {
13665
FirstLines(u64),

0 commit comments

Comments
 (0)