[clap] Multiple args beginning with the same letter

Hi
This might be answered somewhere, but I have not found it.
When using clap, I have a struct with my argument that looks like this:

struct Args {
    #[arg(short, long)]
    artist: String,

    #[arg(short, long)]
    album: String,

    #[arg(short, long)]
    title: String,

    #[arg(short, long)]
    filename: std::path::PathBuf,
}

As you can see, two of those start with the letter 'a'. Is there a way I can get that to work without getting a runtime panic like:

Short option names must be unique for each argument, but '-a' is in use by both 'artist' and 'album'

Specify one of the conflicting short arguments explicitly using #[arg(short = "x")] instead. (Replace the letter X with whatever you want it to be, naturally.)

1 Like

That gave me a compile error I don't understand:

the trait `IntoResettable<char>` is not implemented for `&str`

Do I have to do more than

use clap::Parser;

for that to work?

No. The error message tells you that you've got a string whereas something that has to do with a char was expected. You can infer from this that instead of a string literal, you should specify a character literal for the value of the attribute, i.e., 'x' rather than "x". (I wasn't sure whether or not non-string attributes are already stable; you could only write strings at that position for a long time.)

Right. Because using 'x' and it works. Huh. That was one of the more subtle ones. I guess long arguments might then have to work with strings. I guess I will have to test that one out.
Thanks!
Now I finally found it in the docs as well, when I knew what to look for.
For future reference: here.

1 Like

For custom macro/derive attributes, the grammar is now # [ $path ( $($tt)* ) ] — literally anything can appear between the parenthesis. The macro/derive will of course have further restrictions on what it knows how to interpret, but the language doesn't put any restrictions here anymore.

At this point, it's been this permissive definition for the majority of the time custom attributes have been possible.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.