Merge pull request #5063 from cakebaker/nl_use_value_parser_for_number_format

nl: use value parser for "--number-format"
This commit is contained in:
Sylvestre Ledru 2023-07-11 17:10:20 +02:00 committed by GitHub
commit 8e8b825b45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 18 deletions

View file

@ -36,23 +36,10 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &clap::ArgMatches) ->
settings.number_separator = val.to_owned(); settings.number_separator = val.to_owned();
} }
} }
match opts.get_one::<String>(options::NUMBER_FORMAT) { settings.number_format = opts
None => {} .get_one::<String>(options::NUMBER_FORMAT)
Some(val) => match val.as_str() { .map(Into::into)
"ln" => { .unwrap_or_default();
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"));
}
},
}
match opts.get_one::<String>(options::BODY_NUMBERING) { match opts.get_one::<String>(options::BODY_NUMBERING) {
None => {} None => {}
Some(val) => { Some(val) => {

View file

@ -76,12 +76,25 @@ enum NumberingStyle {
// NumberFormat specifies how line numbers are output within their allocated // NumberFormat specifies how line numbers are output within their allocated
// space. They are justified to the left or right, in the latter case with // 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. // the option of having all unused space to its left turned into leading zeroes.
#[derive(Default)]
enum NumberFormat { enum NumberFormat {
Left, Left,
#[default]
Right, Right,
RightZero, 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 mod options {
pub const HELP: &str = "help"; pub const HELP: &str = "help";
pub const FILE: &str = "file"; pub const FILE: &str = "file";
@ -209,7 +222,8 @@ pub fn uu_app() -> Command {
.short('n') .short('n')
.long(options::NUMBER_FORMAT) .long(options::NUMBER_FORMAT)
.help("insert line numbers according to FORMAT") .help("insert line numbers according to FORMAT")
.value_name("FORMAT"), .value_name("FORMAT")
.value_parser(["ln", "rn", "rz"]),
) )
.arg( .arg(
Arg::new(options::NO_RENUMBER) Arg::new(options::NO_RENUMBER)

View file

@ -1,3 +1,4 @@
// spell-checker:ignore ninvalid
use crate::common::util::TestScenario; use crate::common::util::TestScenario;
#[test] #[test]
@ -78,3 +79,46 @@ fn test_no_renumber() {
new_ucmd!().arg(arg).succeeds(); 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'");
}
}