uutils / uutils/coreutils

Making clap args more legible

Open
#8,819 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
24.1k
Forks
2k
Avg merge
1d 5h
Merged PRs (30d)
365

Description

To make the clap argument building process more compact, a trait and a macro could be beneficial.

In uucore

trait CliOptions {
    fn to_option_str(&self) -> &'static str;
    fn to_short_option_char(&self) -> char;
}

macro_rules! clap_arg {
   ...
}

In some other file

pub enum IpcType {
    SysVQueue,
    SysVSemaphore,
    SysVShmem,
}

impl CliOptions for IpcType {
    fn to_option_str(&self) -> &'static str {
        match self {
            IpcType::SysVShmem => "shmem",
            IpcType::SysVSemaphore => "semaphore",
            IpcType::SysVQueue => "queue",
        }
    }

    fn to_short_option_char(&self) -> char {
        match self {
            IpcType::SysVShmem => 'm',
            IpcType::SysVSemaphore => 's',
            IpcType::SysVQueue => 'q',
        }
    }
}


pub fn uu_app() -> Command {
    Command::new(uucore::util_name())
        .arg(clap_arg!(IpcType::Queue, "create {}"))
        .arg(clap_arg!(IpcType::Semaphore, "create {} array with <number> elements", "number", 1))
...

We can define a macro clap_arg in uucore as something that expands the IpcType::Queue using the to_option_str into the arg! (provided by clap)

Thus clap_arg!(IpcType::Queue, "create {}") will output

arg!(IpcType::Queue.to_option_str() IpcType::Queue.to_short_option_char IpcType::Queue.to_option_str() translate!(format!("create {}", IpcType::Queue.to_option_str())))

(The arg macro takes input in the form [explicit name] [short] [long] [value names] [...] [help string])

This kindof shifts away from the typical C coding pattern to a more modular one. The current utils typically have a mod options in them which define args like

Arg::new(options::OUTPUT)
.short('o')
.long("output")
.help("output columns")
.action(ArgAction::Set)

The proposed way could shift the redundant style to clap_arg!

The trait CliOptions can be made const when const trait has been stabilized (see https://rust-lang.github.io/rust-project-goals/2025h1/const-trait.html)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by surveying uucore and the current utils' mod options definitions, then compare their repeated clap Arg construction with clap's arg! syntax. A complete change would need an agreed trait and clap_arg! design, adoption in representative utilities, and tests confirming the generated argument names, short options, long options, and help text.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.