Skip to content

Commit e7d7f5d

Browse files
committed
cat: bypass clap when possible
1 parent 96a049c commit e7d7f5d

File tree

1 file changed

+69
-44
lines changed

1 file changed

+69
-44
lines changed

src/uu/cat/src/cat.rs

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -219,54 +219,79 @@ mod options {
219219

220220
#[uucore::main]
221221
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
222-
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
222+
let args: Vec<OsString> = args.collect();
223+
let mut files: Vec<OsString>;
224+
let options;
223225

224-
let number_mode = if matches.get_flag(options::NUMBER_NONBLANK) {
225-
NumberingMode::NonEmpty
226-
} else if matches.get_flag(options::NUMBER) {
227-
NumberingMode::All
226+
if args.len() > 1
227+
&& args
228+
.iter()
229+
.skip(1)
230+
.all(|a| !a.to_string_lossy().starts_with('-') || a == "-")
231+
{
232+
// No options, just files.
233+
// Skip clap parsing
234+
files = args.into_iter().skip(1).collect();
235+
if files.is_empty() {
236+
files.push(OsString::from("-"));
237+
}
238+
options = OutputOptions {
239+
show_ends: false,
240+
number: NumberingMode::None,
241+
show_nonprint: false,
242+
show_tabs: false,
243+
squeeze_blank: false,
244+
};
228245
} else {
229-
NumberingMode::None
230-
};
246+
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
231247

232-
let show_nonprint = [
233-
options::SHOW_ALL.to_owned(),
234-
options::SHOW_NONPRINTING_ENDS.to_owned(),
235-
options::SHOW_NONPRINTING_TABS.to_owned(),
236-
options::SHOW_NONPRINTING.to_owned(),
237-
]
238-
.iter()
239-
.any(|v| matches.get_flag(v));
240-
241-
let show_ends = [
242-
options::SHOW_ENDS.to_owned(),
243-
options::SHOW_ALL.to_owned(),
244-
options::SHOW_NONPRINTING_ENDS.to_owned(),
245-
]
246-
.iter()
247-
.any(|v| matches.get_flag(v));
248-
249-
let show_tabs = [
250-
options::SHOW_ALL.to_owned(),
251-
options::SHOW_TABS.to_owned(),
252-
options::SHOW_NONPRINTING_TABS.to_owned(),
253-
]
254-
.iter()
255-
.any(|v| matches.get_flag(v));
256-
257-
let squeeze_blank = matches.get_flag(options::SQUEEZE_BLANK);
258-
let files: Vec<OsString> = match matches.get_many::<OsString>(options::FILE) {
259-
Some(v) => v.cloned().collect(),
260-
None => vec![OsString::from("-")],
261-
};
248+
let number_mode = if matches.get_flag(options::NUMBER_NONBLANK) {
249+
NumberingMode::NonEmpty
250+
} else if matches.get_flag(options::NUMBER) {
251+
NumberingMode::All
252+
} else {
253+
NumberingMode::None
254+
};
262255

263-
let options = OutputOptions {
264-
show_ends,
265-
number: number_mode,
266-
show_nonprint,
267-
show_tabs,
268-
squeeze_blank,
269-
};
256+
let show_nonprint = [
257+
options::SHOW_ALL.to_owned(),
258+
options::SHOW_NONPRINTING_ENDS.to_owned(),
259+
options::SHOW_NONPRINTING_TABS.to_owned(),
260+
options::SHOW_NONPRINTING.to_owned(),
261+
]
262+
.iter()
263+
.any(|v| matches.get_flag(v));
264+
265+
let show_ends = [
266+
options::SHOW_ENDS.to_owned(),
267+
options::SHOW_ALL.to_owned(),
268+
options::SHOW_NONPRINTING_ENDS.to_owned(),
269+
]
270+
.iter()
271+
.any(|v| matches.get_flag(v));
272+
273+
let show_tabs = [
274+
options::SHOW_ALL.to_owned(),
275+
options::SHOW_TABS.to_owned(),
276+
options::SHOW_NONPRINTING_TABS.to_owned(),
277+
]
278+
.iter()
279+
.any(|v| matches.get_flag(v));
280+
281+
let squeeze_blank = matches.get_flag(options::SQUEEZE_BLANK);
282+
files = match matches.get_many::<OsString>(options::FILE) {
283+
Some(v) => v.cloned().collect(),
284+
None => vec![OsString::from("-")],
285+
};
286+
287+
options = OutputOptions {
288+
show_ends,
289+
number: number_mode,
290+
show_nonprint,
291+
show_tabs,
292+
squeeze_blank,
293+
};
294+
}
270295
cat_files(&files, &options)
271296
}
272297

0 commit comments

Comments
 (0)