Skip to content

Support automatic naming and kebab-casing of fields/subcommands #18

@williamyaoh

Description

@williamyaoh

Description

Have a derive like this:

#[derive(StructOpt)]
struct Opt {
    #[structopt(long)]
    dry_run: bool,
    #[structopt(long)]
    config: Option<String>
}

automatically produce two command-line flags: --dry-run and --config. Currently, a #[structopt(long = "$NAME")] would have to added to both of them to make them usable.

Have a derive like this:

#[derive(StructOpt)]
enum Cmd {
    Loop,
    Lutz,
    Salchow,
    ToeLoop
}

automatically produce the subcommands loop, lutz, salchow, and toe-loop. As of #17, a #[structopt(name = "$NAME")] would be required here, otherwise CamelCase names would have to passed on the command line.

Semantics

Let the result of to-kebab-case(id) be id'. It's implemented in https://crates.io/crates/heck

For any field in a derive(StructOpt)'d type, if it does not have a name attribute, the name attribute will default to to-kebab-case($FIELD_NAME). If it does have a long attribute with no value , the long attribute will default to the same thing.

For any enum variant in a derive(StructOpt)'d enum, if it does not have a name attribute, the name attribute will default to to-kebab-case($VARIANT_NAME).

Purpose

To make structopt more convenient to use by reducing the amount of typing needed for common cases. It should not make structopt less intuitive to use.

Concerns

  • While convenient, implementing this can mean that a user of structopt will have to pass names and flags on the command line that they technically never specified in their source code. Does the convenience outweigh the possible confusion from "magic" code?
  • The name attribute can be easily overridden by a user. However, as far as I can see, there isn't a way to "unset" a .long() call on a clap::Arg. So optional arguments will end up being usable through long flags, even if the user, say, only wants short flags. How common is this scenario?
  • The default mechanism for lowercasing in std::char isn't specialised for any given locale. Should structopt support customised lowercasing based on locale, or should it remain untailored for simplicity of use?

Alternatives

  • Only automatically set the name attribute, rather than the long attribute. This avoids the problem with unremovable long flag names mentioned above.
  • Don't do anything. Undesirable.

Anything else?

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking-changeThis change is breaking and, if implemented, must be postponed until the next minor releaseenhancementWe would love to have this feature! Feel free to supply a PR

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions