Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nl: use value parser for "--number-format" #5063

Merged
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
21 changes: 4 additions & 17 deletions src/uu/nl/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,10 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &clap::ArgMatches) ->
settings.number_separator = val.to_owned();
}
}
match opts.get_one::<String>(options::NUMBER_FORMAT) {
None => {}
Some(val) => match val.as_str() {
"ln" => {
settings.number_format = crate::NumberFormat::Left;
}
"rn" => {
settings.number_format = crate::NumberFormat::Right;
}
"rz" => {
settings.number_format = crate::NumberFormat::RightZero;
}
_ => {
errs.push(String::from("Illegal value for -n"));
}
},
}
settings.number_format = opts
.get_one::<String>(options::NUMBER_FORMAT)
.map(Into::into)
.unwrap_or_default();
match opts.get_one::<String>(options::BODY_NUMBERING) {
None => {}
Some(val) => {
Expand Down
16 changes: 15 additions & 1 deletion src/uu/nl/src/nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,25 @@ enum NumberingStyle {
// NumberFormat specifies how line numbers are output within their allocated
// space. They are justified to the left or right, in the latter case with
// the option of having all unused space to its left turned into leading zeroes.
#[derive(Default)]
enum NumberFormat {
Left,
#[default]
Right,
RightZero,
}

impl<T: AsRef<str>> From<T> for NumberFormat {
fn from(s: T) -> Self {
match s.as_ref() {
"ln" => Self::Left,
"rn" => Self::Right,
"rz" => Self::RightZero,
_ => unreachable!("Should have been caught by clap"),
}
}
}

pub mod options {
pub const HELP: &str = "help";
pub const FILE: &str = "file";
Expand Down Expand Up @@ -207,7 +220,8 @@ pub fn uu_app() -> Command {
.short('n')
.long(options::NUMBER_FORMAT)
.help("insert line numbers according to FORMAT")
.value_name("FORMAT"),
.value_name("FORMAT")
.value_parser(["ln", "rn", "rz"]),
)
.arg(
Arg::new(options::NO_RENUMBER)
Expand Down
44 changes: 44 additions & 0 deletions tests/by-util/test_nl.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// spell-checker:ignore ninvalid
use crate::common::util::TestScenario;

#[test]
Expand Down Expand Up @@ -78,3 +79,46 @@ fn test_no_renumber() {
new_ucmd!().arg(arg).succeeds();
}
}

#[test]
fn test_number_format_ln() {
for arg in ["-nln", "--number-format=ln"] {
new_ucmd!()
.arg(arg)
.pipe_in("test")
.succeeds()
.stdout_is("1 \ttest\n");
}
}

#[test]
fn test_number_format_rn() {
for arg in ["-nrn", "--number-format=rn"] {
new_ucmd!()
.arg(arg)
.pipe_in("test")
.succeeds()
.stdout_is(" 1\ttest\n");
}
}

#[test]
fn test_number_format_rz() {
for arg in ["-nrz", "--number-format=rz"] {
new_ucmd!()
.arg(arg)
.pipe_in("test")
.succeeds()
.stdout_is("000001\ttest\n");
}
}

#[test]
fn test_invalid_number_format() {
for arg in ["-ninvalid", "--number-format=invalid"] {
new_ucmd!()
.arg(arg)
.fails()
.stderr_contains("invalid value 'invalid'");
}
}