Skip to content

Commit c5cff82

Browse files
oech3cakebaker
authored andcommitted
tee: split cli part of tee.rs
1 parent 5d6d5cb commit c5cff82

2 files changed

Lines changed: 108 additions & 96 deletions

File tree

src/uu/tee/src/cli.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
// spell-checker:ignore nopipe
7+
8+
use clap::{Arg, ArgAction, Command, builder::PossibleValue};
9+
use std::ffi::OsString;
10+
use uucore::parser::shortcut_value_parser::ShortcutValueParser;
11+
pub use uucore::{format_usage, translate};
12+
13+
pub mod options {
14+
pub const APPEND: &str = "append";
15+
pub const IGNORE_INTERRUPTS: &str = "ignore-interrupts";
16+
pub const FILE: &str = "file";
17+
pub const IGNORE_PIPE_ERRORS: &str = "ignore-pipe-errors";
18+
pub const OUTPUT_ERROR: &str = "output-error";
19+
}
20+
21+
#[derive(Clone, Debug)]
22+
pub enum OutputErrorMode {
23+
/// Diagnose write error on any output
24+
Warn,
25+
/// Diagnose write error on any output that is not a pipe
26+
WarnNoPipe,
27+
/// Exit upon write error on any output
28+
Exit,
29+
/// Exit upon write error on any output that is not a pipe
30+
ExitNoPipe,
31+
}
32+
33+
#[allow(dead_code)]
34+
pub struct Options {
35+
pub append: bool,
36+
pub ignore_interrupts: bool,
37+
pub ignore_pipe_errors: bool,
38+
pub files: Vec<OsString>,
39+
pub output_error: Option<OutputErrorMode>,
40+
}
41+
42+
pub fn uu_app() -> Command {
43+
Command::new("tee")
44+
.version(uucore::crate_version!())
45+
.help_template(uucore::localized_help_template("tee"))
46+
.about(translate!("tee-about"))
47+
.override_usage(format_usage(&translate!("tee-usage")))
48+
.after_help(translate!("tee-after-help"))
49+
.infer_long_args(true)
50+
// Since we use value-specific help texts for "--output-error", clap's "short help" and "long help" differ.
51+
// However, this is something that the GNU tests explicitly test for, so we *always* show the long help instead.
52+
.disable_help_flag(true)
53+
.arg(
54+
Arg::new("--help")
55+
.short('h')
56+
.long("help")
57+
.help(translate!("tee-help-help"))
58+
.action(ArgAction::HelpLong),
59+
)
60+
.arg(
61+
Arg::new(options::APPEND)
62+
.long(options::APPEND)
63+
.short('a')
64+
.help(translate!("tee-help-append"))
65+
.action(ArgAction::SetTrue)
66+
.overrides_with(options::APPEND),
67+
)
68+
.arg(
69+
Arg::new(options::IGNORE_INTERRUPTS)
70+
.long(options::IGNORE_INTERRUPTS)
71+
.short('i')
72+
.help(translate!("tee-help-ignore-interrupts"))
73+
.action(ArgAction::SetTrue),
74+
)
75+
.arg(
76+
Arg::new(options::FILE)
77+
.action(ArgAction::Append)
78+
.value_hint(clap::ValueHint::FilePath)
79+
.value_parser(clap::value_parser!(OsString)),
80+
)
81+
.arg(
82+
Arg::new(options::IGNORE_PIPE_ERRORS)
83+
.short('p')
84+
.help(translate!("tee-help-ignore-pipe-errors"))
85+
.action(ArgAction::SetTrue),
86+
)
87+
.arg(
88+
Arg::new(options::OUTPUT_ERROR)
89+
.long(options::OUTPUT_ERROR)
90+
.require_equals(true)
91+
.num_args(0..=1)
92+
.default_missing_value("warn-nopipe")
93+
.value_parser(ShortcutValueParser::new([
94+
PossibleValue::new("warn").help(translate!("tee-help-output-error-warn")),
95+
PossibleValue::new("warn-nopipe")
96+
.help(translate!("tee-help-output-error-warn-nopipe")),
97+
PossibleValue::new("exit").help(translate!("tee-help-output-error-exit")),
98+
PossibleValue::new("exit-nopipe")
99+
.help(translate!("tee-help-output-error-exit-nopipe")),
100+
]))
101+
.help(translate!("tee-help-output-error")),
102+
)
103+
}

src/uu/tee/src/tee.rs

Lines changed: 5 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,25 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use clap::{Arg, ArgAction, Command, builder::PossibleValue};
6+
// spell-checker:ignore espidf nopipe
7+
78
use std::ffi::OsString;
89
use std::fs::OpenOptions;
910
use std::io::{Error, ErrorKind, Read, Result, Write, stderr, stdin, stdout};
1011
use std::path::PathBuf;
1112
use uucore::display::Quotable;
1213
use uucore::error::{UResult, strip_errno};
13-
use uucore::format_usage;
14-
use uucore::parser::shortcut_value_parser::ShortcutValueParser;
1514
use uucore::translate;
1615

17-
// spell-checker:ignore espidf nopipe
16+
mod cli;
17+
pub use crate::cli::uu_app;
18+
use crate::cli::{Options, OutputErrorMode, options};
1819

1920
#[cfg(target_os = "linux")]
2021
use uucore::signals::ensure_stdout_not_broken;
2122
#[cfg(unix)]
2223
use uucore::signals::{disable_pipe_errors, ignore_interrupts};
2324

24-
mod options {
25-
pub const APPEND: &str = "append";
26-
pub const IGNORE_INTERRUPTS: &str = "ignore-interrupts";
27-
pub const FILE: &str = "file";
28-
pub const IGNORE_PIPE_ERRORS: &str = "ignore-pipe-errors";
29-
pub const OUTPUT_ERROR: &str = "output-error";
30-
}
31-
32-
#[allow(dead_code)]
33-
struct Options {
34-
append: bool,
35-
ignore_interrupts: bool,
36-
ignore_pipe_errors: bool,
37-
files: Vec<OsString>,
38-
output_error: Option<OutputErrorMode>,
39-
}
40-
41-
#[derive(Clone, Debug)]
42-
enum OutputErrorMode {
43-
/// Diagnose write error on any output
44-
Warn,
45-
/// Diagnose write error on any output that is not a pipe
46-
WarnNoPipe,
47-
/// Exit upon write error on any output
48-
Exit,
49-
/// Exit upon write error on any output that is not a pipe
50-
ExitNoPipe,
51-
}
52-
5325
#[uucore::main]
5426
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5527
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
@@ -84,69 +56,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8456
tee(&options).map_err(|_| 1.into())
8557
}
8658

87-
pub fn uu_app() -> Command {
88-
Command::new("tee")
89-
.version(uucore::crate_version!())
90-
.help_template(uucore::localized_help_template("tee"))
91-
.about(translate!("tee-about"))
92-
.override_usage(format_usage(&translate!("tee-usage")))
93-
.after_help(translate!("tee-after-help"))
94-
.infer_long_args(true)
95-
// Since we use value-specific help texts for "--output-error", clap's "short help" and "long help" differ.
96-
// However, this is something that the GNU tests explicitly test for, so we *always* show the long help instead.
97-
.disable_help_flag(true)
98-
.arg(
99-
Arg::new("--help")
100-
.short('h')
101-
.long("help")
102-
.help(translate!("tee-help-help"))
103-
.action(ArgAction::HelpLong),
104-
)
105-
.arg(
106-
Arg::new(options::APPEND)
107-
.long(options::APPEND)
108-
.short('a')
109-
.help(translate!("tee-help-append"))
110-
.action(ArgAction::SetTrue)
111-
.overrides_with(options::APPEND),
112-
)
113-
.arg(
114-
Arg::new(options::IGNORE_INTERRUPTS)
115-
.long(options::IGNORE_INTERRUPTS)
116-
.short('i')
117-
.help(translate!("tee-help-ignore-interrupts"))
118-
.action(ArgAction::SetTrue),
119-
)
120-
.arg(
121-
Arg::new(options::FILE)
122-
.action(ArgAction::Append)
123-
.value_hint(clap::ValueHint::FilePath)
124-
.value_parser(clap::value_parser!(OsString)),
125-
)
126-
.arg(
127-
Arg::new(options::IGNORE_PIPE_ERRORS)
128-
.short('p')
129-
.help(translate!("tee-help-ignore-pipe-errors"))
130-
.action(ArgAction::SetTrue),
131-
)
132-
.arg(
133-
Arg::new(options::OUTPUT_ERROR)
134-
.long(options::OUTPUT_ERROR)
135-
.require_equals(true)
136-
.num_args(0..=1)
137-
.default_missing_value("warn-nopipe")
138-
.value_parser(ShortcutValueParser::new([
139-
PossibleValue::new("warn").help(translate!("tee-help-output-error-warn")),
140-
PossibleValue::new("warn-nopipe")
141-
.help(translate!("tee-help-output-error-warn-nopipe")),
142-
PossibleValue::new("exit").help(translate!("tee-help-output-error-exit")),
143-
PossibleValue::new("exit-nopipe")
144-
.help(translate!("tee-help-output-error-exit-nopipe")),
145-
]))
146-
.help(translate!("tee-help-output-error")),
147-
)
148-
}
149-
15059
fn tee(options: &Options) -> Result<()> {
15160
#[cfg(unix)]
15261
{

0 commit comments

Comments
 (0)