Skip to content

Commit cda34a7

Browse files
committed
split: replace OPT_ by options::
1 parent f885f25 commit cda34a7

4 files changed

Lines changed: 237 additions & 225 deletions

File tree

src/uu/split/src/cli.rs

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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, ValueHint};
7+
use std::env;
8+
use std::ffi::OsString;
9+
pub use uucore::{format_usage, translate};
10+
11+
pub const ARG_INPUT: &str = "input";
12+
pub const ARG_PREFIX: &str = "prefix";
13+
14+
pub mod options {
15+
pub const BYTES: &str = "bytes";
16+
pub const LINE_BYTES: &str = "line-bytes";
17+
pub const LINES: &str = "lines";
18+
pub const ADDITIONAL_SUFFIX: &str = "additional-suffix";
19+
pub const FILTER: &str = "filter";
20+
pub const NUMBER: &str = "number";
21+
pub const NUMERIC_SUFFIXES: &str = "numeric-suffixes";
22+
pub const NUMERIC_SUFFIXES_SHORT: &str = "-d";
23+
pub const HEX_SUFFIXES: &str = "hex-suffixes";
24+
pub const HEX_SUFFIXES_SHORT: &str = "-x";
25+
pub const SUFFIX_LENGTH: &str = "suffix-length";
26+
pub const VERBOSE: &str = "verbose";
27+
pub const SEPARATOR: &str = "separator";
28+
pub const ELIDE_EMPTY_FILES: &str = "elide-empty-files";
29+
pub const IO_BLKSIZE: &str = "-io-blksize";
30+
}
31+
32+
pub fn uu_app() -> Command {
33+
Command::new("split")
34+
.version(uucore::crate_version!())
35+
.help_template(uucore::localized_help_template(uucore::util_name()))
36+
.about(translate!("split-about"))
37+
.after_help(translate!("split-after-help"))
38+
.override_usage(format_usage(&translate!("split-usage")))
39+
.infer_long_args(true)
40+
// strategy (mutually exclusive)
41+
.arg(
42+
Arg::new(options::BYTES)
43+
.short('b')
44+
.long(options::BYTES)
45+
.allow_hyphen_values(true)
46+
.value_name("SIZE")
47+
.help(translate!("split-help-bytes")),
48+
)
49+
.arg(
50+
Arg::new(options::LINE_BYTES)
51+
.short('C')
52+
.long(options::LINE_BYTES)
53+
.allow_hyphen_values(true)
54+
.value_name("SIZE")
55+
.help(translate!("split-help-line-bytes")),
56+
)
57+
.arg(
58+
Arg::new(options::LINES)
59+
.short('l')
60+
.long(options::LINES)
61+
.allow_hyphen_values(true)
62+
.value_name("NUMBER")
63+
.default_value("1000")
64+
.help(translate!("split-help-lines")),
65+
)
66+
.arg(
67+
Arg::new(options::NUMBER)
68+
.short('n')
69+
.long(options::NUMBER)
70+
.allow_hyphen_values(true)
71+
.value_name("CHUNKS")
72+
.help(translate!("split-help-number")),
73+
)
74+
// rest of the arguments
75+
.arg(
76+
Arg::new(options::ADDITIONAL_SUFFIX)
77+
.long(options::ADDITIONAL_SUFFIX)
78+
.allow_hyphen_values(true)
79+
.value_name("SUFFIX")
80+
.default_value("")
81+
.value_parser(clap::value_parser!(OsString))
82+
.help(translate!("split-help-additional-suffix")),
83+
)
84+
.arg(
85+
Arg::new(options::FILTER)
86+
.long(options::FILTER)
87+
.allow_hyphen_values(true)
88+
.value_name("COMMAND")
89+
.value_hint(ValueHint::CommandName)
90+
.help(translate!("split-help-filter")),
91+
)
92+
.arg(
93+
Arg::new(options::ELIDE_EMPTY_FILES)
94+
.long(options::ELIDE_EMPTY_FILES)
95+
.short('e')
96+
.help(translate!("split-help-elide-empty-files"))
97+
.action(ArgAction::SetTrue),
98+
)
99+
.arg(
100+
Arg::new(options::NUMERIC_SUFFIXES_SHORT)
101+
.short('d')
102+
.action(ArgAction::SetTrue)
103+
.overrides_with_all([
104+
options::NUMERIC_SUFFIXES,
105+
options::NUMERIC_SUFFIXES_SHORT,
106+
options::HEX_SUFFIXES,
107+
options::HEX_SUFFIXES_SHORT,
108+
])
109+
.help(translate!("split-help-numeric-suffixes-short")),
110+
)
111+
.arg(
112+
Arg::new(options::NUMERIC_SUFFIXES)
113+
.long(options::NUMERIC_SUFFIXES)
114+
.require_equals(true)
115+
.num_args(0..=1)
116+
.overrides_with_all([
117+
options::NUMERIC_SUFFIXES,
118+
options::NUMERIC_SUFFIXES_SHORT,
119+
options::HEX_SUFFIXES,
120+
options::HEX_SUFFIXES_SHORT,
121+
])
122+
.value_name("FROM")
123+
.help(translate!("split-help-numeric-suffixes")),
124+
)
125+
.arg(
126+
Arg::new(options::HEX_SUFFIXES_SHORT)
127+
.short('x')
128+
.action(ArgAction::SetTrue)
129+
.overrides_with_all([
130+
options::NUMERIC_SUFFIXES,
131+
options::NUMERIC_SUFFIXES_SHORT,
132+
options::HEX_SUFFIXES,
133+
options::HEX_SUFFIXES_SHORT,
134+
])
135+
.help(translate!("split-help-hex-suffixes-short")),
136+
)
137+
.arg(
138+
Arg::new(options::HEX_SUFFIXES)
139+
.long(options::HEX_SUFFIXES)
140+
.require_equals(true)
141+
.num_args(0..=1)
142+
.overrides_with_all([
143+
options::NUMERIC_SUFFIXES,
144+
options::NUMERIC_SUFFIXES_SHORT,
145+
options::HEX_SUFFIXES,
146+
options::HEX_SUFFIXES_SHORT,
147+
])
148+
.value_name("FROM")
149+
.help(translate!("split-help-hex-suffixes")),
150+
)
151+
.arg(
152+
Arg::new(options::SUFFIX_LENGTH)
153+
.short('a')
154+
.long(options::SUFFIX_LENGTH)
155+
.allow_hyphen_values(true)
156+
.value_name("N")
157+
.help(translate!("split-help-suffix-length")),
158+
)
159+
.arg(
160+
Arg::new(options::VERBOSE)
161+
.long(options::VERBOSE)
162+
.help(translate!("split-help-verbose"))
163+
.action(ArgAction::SetTrue),
164+
)
165+
.arg(
166+
Arg::new(options::SEPARATOR)
167+
.short('t')
168+
.long(options::SEPARATOR)
169+
.allow_hyphen_values(true)
170+
.value_name("SEP")
171+
.action(ArgAction::Append)
172+
.help(translate!("split-help-separator")),
173+
)
174+
.arg(
175+
Arg::new(options::IO_BLKSIZE)
176+
.long("io-blksize")
177+
.alias(options::IO_BLKSIZE)
178+
.hide(true),
179+
)
180+
.arg(
181+
Arg::new(ARG_INPUT)
182+
.default_value("-")
183+
.value_hint(ValueHint::FilePath)
184+
.value_parser(clap::value_parser!(OsString)),
185+
)
186+
.arg(
187+
Arg::new(ARG_PREFIX)
188+
.default_value("x")
189+
.value_parser(clap::value_parser!(OsString)),
190+
)
191+
}

src/uu/split/src/filenames.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@
3030
//! assert_eq!(it.next().unwrap(), "chunk_ac.txt");
3131
//! ```
3232
33+
use crate::cli::options;
3334
use crate::number::DynamicWidthNumber;
3435
use crate::number::FixedWidthNumber;
3536
use crate::number::Number;
3637
use crate::strategy::Strategy;
37-
use crate::{
38-
OPT_ADDITIONAL_SUFFIX, OPT_HEX_SUFFIXES, OPT_HEX_SUFFIXES_SHORT, OPT_NUMERIC_SUFFIXES,
39-
OPT_NUMERIC_SUFFIXES_SHORT, OPT_SUFFIX_LENGTH,
40-
};
4138
use clap::ArgMatches;
4239
use std::ffi::{OsStr, OsString};
4340
use std::path::is_separator;
@@ -148,15 +145,15 @@ impl Suffix {
148145
// Since all suffixes are setup with 'overrides_with_all()' against themselves and each other,
149146
// last one wins, all others are ignored
150147
match (
151-
matches.contains_id(OPT_NUMERIC_SUFFIXES),
152-
matches.contains_id(OPT_HEX_SUFFIXES),
153-
matches.get_flag(OPT_NUMERIC_SUFFIXES_SHORT),
154-
matches.get_flag(OPT_HEX_SUFFIXES_SHORT),
148+
matches.contains_id(options::NUMERIC_SUFFIXES),
149+
matches.contains_id(options::HEX_SUFFIXES),
150+
matches.get_flag(options::NUMERIC_SUFFIXES_SHORT),
151+
matches.get_flag(options::HEX_SUFFIXES_SHORT),
155152
) {
156153
(true, _, _, _) => {
157154
stype = SuffixType::Decimal;
158155
// if option was specified, but without value - this will return None as there is no default value
159-
if let Some(opt) = matches.get_one::<String>(OPT_NUMERIC_SUFFIXES) {
156+
if let Some(opt) = matches.get_one::<String>(options::NUMERIC_SUFFIXES) {
160157
start = opt
161158
.parse::<usize>()
162159
.map_err(|_| SuffixError::NotParsable(opt.to_owned()))?;
@@ -166,7 +163,7 @@ impl Suffix {
166163
(_, true, _, _) => {
167164
stype = SuffixType::Hexadecimal;
168165
// if option was specified, but without value - this will return None as there is no default value
169-
if let Some(opt) = matches.get_one::<String>(OPT_HEX_SUFFIXES) {
166+
if let Some(opt) = matches.get_one::<String>(options::HEX_SUFFIXES) {
170167
start = usize::from_str_radix(opt, 16)
171168
.map_err(|_| SuffixError::NotParsable(opt.to_owned()))?;
172169
auto_widening = false;
@@ -179,7 +176,7 @@ impl Suffix {
179176

180177
// Get suffix length and a flag to indicate if it was specified with command line option
181178
let (mut length, is_length_cmd_opt) =
182-
if let Some(v) = matches.get_one::<String>(OPT_SUFFIX_LENGTH) {
179+
if let Some(v) = matches.get_one::<String>(options::SUFFIX_LENGTH) {
183180
// suffix length was specified in command line
184181
let parsed_length = v
185182
.parse::<usize>()
@@ -228,7 +225,7 @@ impl Suffix {
228225
}
229226

230227
let additional = matches
231-
.get_one::<OsString>(OPT_ADDITIONAL_SUFFIX)
228+
.get_one::<OsString>(options::ADDITIONAL_SUFFIX)
232229
.unwrap()
233230
.clone();
234231
if additional.to_string_lossy().chars().any(is_separator) {

0 commit comments

Comments
 (0)