Skip to content

Commit

Permalink
Merge pull request #6174 from BenWiederhake/dev-all-partial-stringly-…
Browse files Browse the repository at this point in the history
…options

all: Accept shortcuts for stringly-enum options
  • Loading branch information
tertsdiepraam committed Apr 14, 2024
2 parents 333e4d9 + 91679fc commit 652c65f
Show file tree
Hide file tree
Showing 27 changed files with 565 additions and 177 deletions.
3 changes: 3 additions & 0 deletions docs/src/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ number of spaces representing a tab when determining the line length.
GNU `ls` provides two ways to use a long listing format: `-l` and `--format=long`. We support a
third way: `--long`.

GNU `ls --sort=VALUE` only supports special non-default sort orders.
We support `--sort=name`, which makes it possible to override an earlier value.

## `du`

`du` allows `birth` and `creation` as values for the `--time` argument to show the creation time. It
Expand Down
24 changes: 7 additions & 17 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use uucore::{backup_control, update_control};
// requires these enum.
pub use uucore::{backup_control::BackupMode, update_control::UpdateMode};
use uucore::{
format_usage, help_about, help_section, help_usage, prompt_yes, show_error, show_warning,
util_name,
format_usage, help_about, help_section, help_usage, prompt_yes,
shortcut_value_parser::ShortcutValueParser, show_error, show_warning, util_name,
};

use crate::copydir::copy_directory;
Expand Down Expand Up @@ -396,22 +396,14 @@ static PRESERVABLE_ATTRIBUTES: &[&str] = &[
"ownership",
"timestamps",
"context",
"link",
"links",
"xattr",
"all",
];

#[cfg(not(unix))]
static PRESERVABLE_ATTRIBUTES: &[&str] = &[
"mode",
"timestamps",
"context",
"link",
"links",
"xattr",
"all",
];
static PRESERVABLE_ATTRIBUTES: &[&str] =
&["mode", "timestamps", "context", "links", "xattr", "all"];

pub fn uu_app() -> Command {
const MODE_ARGS: &[&str] = &[
Expand Down Expand Up @@ -543,7 +535,7 @@ pub fn uu_app() -> Command {
.overrides_with_all(MODE_ARGS)
.require_equals(true)
.default_missing_value("always")
.value_parser(["auto", "always", "never"])
.value_parser(ShortcutValueParser::new(["auto", "always", "never"]))
.num_args(0..=1)
.help("control clone/CoW copies. See below"),
)
Expand All @@ -559,9 +551,7 @@ pub fn uu_app() -> Command {
.long(options::PRESERVE)
.action(ArgAction::Append)
.use_value_delimiter(true)
.value_parser(clap::builder::PossibleValuesParser::new(
PRESERVABLE_ATTRIBUTES,
))
.value_parser(ShortcutValueParser::new(PRESERVABLE_ATTRIBUTES))
.num_args(0..)
.require_equals(true)
.value_name("ATTR_LIST")
Expand Down Expand Up @@ -655,7 +645,7 @@ pub fn uu_app() -> Command {
Arg::new(options::SPARSE)
.long(options::SPARSE)
.value_name("WHEN")
.value_parser(["never", "auto", "always"])
.value_parser(ShortcutValueParser::new(["never", "auto", "always"]))
.help("control creation of sparse files. See below"),
)
// TODO: implement the following args
Expand Down
9 changes: 7 additions & 2 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// file that was distributed with this source code.

use chrono::{DateTime, Local};
use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
use clap::{builder::PossibleValue, crate_version, Arg, ArgAction, ArgMatches, Command};
use glob::Pattern;
use std::collections::HashSet;
use std::env;
Expand All @@ -30,6 +30,7 @@ use uucore::error::{set_exit_code, FromIo, UError, UResult, USimpleError};
use uucore::line_ending::LineEnding;
use uucore::parse_glob;
use uucore::parse_size::{parse_size_u64, ParseSizeError};
use uucore::shortcut_value_parser::ShortcutValueParser;
use uucore::{format_usage, help_about, help_section, help_usage, show, show_error, show_warning};
#[cfg(windows)]
use windows_sys::Win32::Foundation::HANDLE;
Expand Down Expand Up @@ -1040,7 +1041,11 @@ pub fn uu_app() -> Command {
.value_name("WORD")
.require_equals(true)
.num_args(0..)
.value_parser(["atime", "access", "use", "ctime", "status", "birth", "creation"])
.value_parser(ShortcutValueParser::new([
PossibleValue::new("atime").alias("access").alias("use"),
PossibleValue::new("ctime").alias("status"),
PossibleValue::new("creation").alias("birth"),
]))
.help(
"show time of the last modification of any file in the \
directory, or any of its subdirectories. If WORD is given, show time as WORD instead \
Expand Down
61 changes: 35 additions & 26 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// spell-checker:ignore (ToDO) somegroup nlink tabsize dired subdired dtype colorterm stringly

use clap::{
builder::{NonEmptyStringValueParser, ValueParser},
builder::{NonEmptyStringValueParser, PossibleValue, ValueParser},
crate_version, Arg, ArgAction, Command,
};
use glob::{MatchOptions, Pattern};
Expand Down Expand Up @@ -62,6 +62,7 @@ use uucore::{
format_usage,
fs::display_permissions,
parse_size::parse_size_u64,
shortcut_value_parser::ShortcutValueParser,
version_cmp::version_cmp,
};
use uucore::{help_about, help_section, help_usage, parse_glob, show, show_error, show_warning};
Expand Down Expand Up @@ -1203,7 +1204,7 @@ pub fn uu_app() -> Command {
Arg::new(options::FORMAT)
.long(options::FORMAT)
.help("Set the display format.")
.value_parser([
.value_parser(ShortcutValueParser::new([
"long",
"verbose",
"single-column",
Expand All @@ -1212,7 +1213,7 @@ pub fn uu_app() -> Command {
"across",
"horizontal",
"commas",
])
]))
.hide_possible_values(true)
.require_equals(true)
.overrides_with_all([
Expand Down Expand Up @@ -1303,9 +1304,11 @@ pub fn uu_app() -> Command {
Arg::new(options::HYPERLINK)
.long(options::HYPERLINK)
.help("hyperlink file names WHEN")
.value_parser([
"always", "yes", "force", "auto", "tty", "if-tty", "never", "no", "none",
])
.value_parser(ShortcutValueParser::new([
PossibleValue::new("always").alias("yes").alias("force"),
PossibleValue::new("auto").alias("tty").alias("if-tty"),
PossibleValue::new("never").alias("no").alias("none"),
]))
.require_equals(true)
.num_args(0..=1)
.default_missing_value("always")
Expand Down Expand Up @@ -1351,15 +1354,15 @@ pub fn uu_app() -> Command {
Arg::new(options::QUOTING_STYLE)
.long(options::QUOTING_STYLE)
.help("Set quoting style.")
.value_parser([
"literal",
"shell",
"shell-always",
"shell-escape",
"shell-escape-always",
"c",
"escape",
])
.value_parser(ShortcutValueParser::new([
PossibleValue::new("literal"),
PossibleValue::new("shell"),
PossibleValue::new("shell-escape"),
PossibleValue::new("shell-always"),
PossibleValue::new("shell-escape-always"),
PossibleValue::new("c").alias("c-maybe"),
PossibleValue::new("escape"),
]))
.overrides_with_all([
options::QUOTING_STYLE,
options::quoting::LITERAL,
Expand Down Expand Up @@ -1434,9 +1437,11 @@ pub fn uu_app() -> Command {
\tbirth time: birth, creation;",
)
.value_name("field")
.value_parser([
"atime", "access", "use", "ctime", "status", "birth", "creation",
])
.value_parser(ShortcutValueParser::new([
PossibleValue::new("atime").alias("access").alias("use"),
PossibleValue::new("ctime").alias("status"),
PossibleValue::new("birth").alias("creation"),
]))
.hide_possible_values(true)
.require_equals(true)
.overrides_with_all([options::TIME, options::time::ACCESS, options::time::CHANGE]),
Expand Down Expand Up @@ -1496,7 +1501,7 @@ pub fn uu_app() -> Command {
.long(options::SORT)
.help("Sort by <field>: name, none (-U), time (-t), size (-S), extension (-X) or width")
.value_name("field")
.value_parser(["name", "none", "time", "size", "version", "extension", "width"])
.value_parser(ShortcutValueParser::new(["name", "none", "time", "size", "version", "extension", "width"]))
.require_equals(true)
.overrides_with_all([
options::SORT,
Expand Down Expand Up @@ -1744,9 +1749,11 @@ pub fn uu_app() -> Command {
Arg::new(options::COLOR)
.long(options::COLOR)
.help("Color output based on file type.")
.value_parser([
"always", "yes", "force", "auto", "tty", "if-tty", "never", "no", "none",
])
.value_parser(ShortcutValueParser::new([
PossibleValue::new("always").alias("yes").alias("force"),
PossibleValue::new("auto").alias("tty").alias("if-tty"),
PossibleValue::new("never").alias("no").alias("none"),
]))
.require_equals(true)
.num_args(0..=1),
)
Expand All @@ -1757,7 +1764,7 @@ pub fn uu_app() -> Command {
"Append indicator with style WORD to entry names: \
none (default), slash (-p), file-type (--file-type), classify (-F)",
)
.value_parser(["none", "slash", "file-type", "classify"])
.value_parser(ShortcutValueParser::new(["none", "slash", "file-type", "classify"]))
.overrides_with_all([
options::indicator_style::FILE_TYPE,
options::indicator_style::SLASH,
Expand Down Expand Up @@ -1788,9 +1795,11 @@ pub fn uu_app() -> Command {
--dereference-command-line-symlink-to-dir options are specified.",
)
.value_name("when")
.value_parser([
"always", "yes", "force", "auto", "tty", "if-tty", "never", "no", "none",
])
.value_parser(ShortcutValueParser::new([
PossibleValue::new("always").alias("yes").alias("force"),
PossibleValue::new("auto").alias("tty").alias("if-tty"),
PossibleValue::new("never").alias("no").alias("none"),
]))
.default_missing_value("always")
.require_equals(true)
.num_args(0..=1)
Expand Down
9 changes: 8 additions & 1 deletion src/uu/numfmt/src/numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use units::{IEC_BASES, SI_BASES};
use uucore::display::Quotable;
use uucore::error::UResult;
use uucore::ranges::Range;
use uucore::shortcut_value_parser::ShortcutValueParser;
use uucore::{format_usage, help_about, help_section, help_usage, show, show_error};

pub mod errors;
Expand Down Expand Up @@ -340,7 +341,13 @@ pub fn uu_app() -> Command {
.help("use METHOD for rounding when scaling")
.value_name("METHOD")
.default_value("from-zero")
.value_parser(["up", "down", "from-zero", "towards-zero", "nearest"]),
.value_parser(ShortcutValueParser::new([
"up",
"down",
"from-zero",
"towards-zero",
"nearest",
])),
)
.arg(
Arg::new(options::SUFFIX)
Expand Down
3 changes: 2 additions & 1 deletion src/uu/od/src/od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use clap::{crate_version, parser::ValueSource, Arg, ArgMatches, Command};
use uucore::display::Quotable;
use uucore::error::{UResult, USimpleError};
use uucore::parse_size::ParseSizeError;
use uucore::shortcut_value_parser::ShortcutValueParser;
use uucore::{format_usage, help_about, help_section, help_usage, show_error, show_warning};

const PEEK_BUFFER_SIZE: usize = 4; // utf-8 can be 4 bytes
Expand Down Expand Up @@ -287,7 +288,7 @@ pub fn uu_app() -> Command {
Arg::new(options::ENDIAN)
.long(options::ENDIAN)
.help("byte order to use for multi-byte formats")
.value_parser(["big", "little"])
.value_parser(ShortcutValueParser::new(["big", "little"]))
.value_name("big|little"),
)
.arg(
Expand Down
5 changes: 3 additions & 2 deletions src/uu/shred/src/shred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::path::{Path, PathBuf};
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::parse_size::parse_size_u64;
use uucore::shortcut_value_parser::ShortcutValueParser;
use uucore::{format_usage, help_about, help_section, help_usage, show_error, show_if_err};

const ABOUT: &str = help_about!("shred.md");
Expand Down Expand Up @@ -315,11 +316,11 @@ pub fn uu_app() -> Command {
Arg::new(options::REMOVE)
.long(options::REMOVE)
.value_name("HOW")
.value_parser([
.value_parser(ShortcutValueParser::new([
options::remove::UNLINK,
options::remove::WIPE,
options::remove::WIPESYNC,
])
]))
.num_args(0..=1)
.require_equals(true)
.default_missing_value(options::remove::WIPESYNC)
Expand Down
9 changes: 5 additions & 4 deletions src/uu/sort/src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use uucore::display::Quotable;
use uucore::error::{set_exit_code, strip_errno, UError, UResult, USimpleError, UUsageError};
use uucore::line_ending::LineEnding;
use uucore::parse_size::{ParseSizeError, Parser};
use uucore::shortcut_value_parser::ShortcutValueParser;
use uucore::version_cmp::version_cmp;
use uucore::{format_usage, help_about, help_section, help_usage};

Expand Down Expand Up @@ -1297,14 +1298,14 @@ pub fn uu_app() -> Command {
.arg(
Arg::new(options::modes::SORT)
.long(options::modes::SORT)
.value_parser([
.value_parser(ShortcutValueParser::new([
"general-numeric",
"human-numeric",
"month",
"numeric",
"version",
"random",
])
]))
.conflicts_with_all(options::modes::ALL_SORT_MODES),
)
.arg(make_sort_mode_arg(
Expand Down Expand Up @@ -1363,11 +1364,11 @@ pub fn uu_app() -> Command {
.long(options::check::CHECK)
.require_equals(true)
.num_args(0..)
.value_parser([
.value_parser(ShortcutValueParser::new([
options::check::SILENT,
options::check::QUIET,
options::check::DIAGNOSE_FIRST,
])
]))
.conflicts_with(options::OUTPUT)
.help("check for sorted input; do not sort"),
)
Expand Down
3 changes: 2 additions & 1 deletion src/uu/tail/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::io::IsTerminal;
use std::time::Duration;
use uucore::error::{UResult, USimpleError, UUsageError};
use uucore::parse_size::{parse_size_u64, ParseSizeError};
use uucore::shortcut_value_parser::ShortcutValueParser;
use uucore::{format_usage, help_about, help_usage, show_warning};

const ABOUT: &str = help_about!("tail.md");
Expand Down Expand Up @@ -494,7 +495,7 @@ pub fn uu_app() -> Command {
.default_missing_value("descriptor")
.num_args(0..=1)
.require_equals(true)
.value_parser(["descriptor", "name"])
.value_parser(ShortcutValueParser::new(["descriptor", "name"]))
.overrides_with(options::FOLLOW)
.help("Print the file as it grows"),
)
Expand Down
5 changes: 3 additions & 2 deletions src/uu/tee/src/tee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::io::{copy, stdin, stdout, Error, ErrorKind, Read, Result, Write};
use std::path::PathBuf;
use uucore::display::Quotable;
use uucore::error::UResult;
use uucore::shortcut_value_parser::ShortcutValueParser;
use uucore::{format_usage, help_about, help_section, help_usage, show_error};

// spell-checker:ignore nopipe
Expand Down Expand Up @@ -119,15 +120,15 @@ pub fn uu_app() -> Command {
.long(options::OUTPUT_ERROR)
.require_equals(true)
.num_args(0..=1)
.value_parser([
.value_parser(ShortcutValueParser::new([
PossibleValue::new("warn")
.help("produce warnings for errors writing to any output"),
PossibleValue::new("warn-nopipe")
.help("produce warnings for errors that are not pipe errors (ignored on non-unix platforms)"),
PossibleValue::new("exit").help("exit on write errors to any output"),
PossibleValue::new("exit-nopipe")
.help("exit on write errors to any output that are not pipe errors (equivalent to exit on non-unix platforms)"),
])
]))
.help("set write error behavior")
.conflicts_with(options::IGNORE_PIPE_ERRORS),
)
Expand Down
Loading

0 comments on commit 652c65f

Please sign in to comment.