Skip to content

Commit

Permalink
Merge pull request #5063 from cakebaker/nl_use_value_parser_for_numbe…
Browse files Browse the repository at this point in the history
…r_format

nl: use value parser for "--number-format"
  • Loading branch information
sylvestre authored Jul 11, 2023
2 parents e339e53 + cb50208 commit 8e8b825
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
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 @@ -76,12 +76,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 @@ -209,7 +222,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'");
}
}

0 comments on commit 8e8b825

Please sign in to comment.