blob: 35f3c4f7a50777c5a753c58078a8cec83374ba89 [file] [log] [blame]
Matthew Maurer32e76692020-06-02 11:15:15 -07001//! Example on how the `rename_all` parameter works.
2//!
3//! `rename_all` can be used to override the casing style used during argument
4//! generation. By default the `kebab-case` style will be used but there are a wide
5//! variety of other styles available.
6//!
7//! ## Supported styles overview:
8//!
9//! - **Camel Case**: Indicate word boundaries with uppercase letter, excluding
10//! the first word.
11//! - **Kebab Case**: Keep all letters lowercase and indicate word boundaries
12//! with hyphens.
13//! - **Pascal Case**: Indicate word boundaries with uppercase letter,
14//! including the first word.
15//! - **Screaming Snake Case**: Keep all letters uppercase and indicate word
16//! boundaries with underscores.
17//! - **Snake Case**: Keep all letters lowercase and indicate word boundaries
18//! with underscores.
19//! - **Verbatim**: Use the original attribute name defined in the code.
20
21use structopt::StructOpt;
22
23#[derive(StructOpt, Debug)]
24#[structopt(name = "rename_all", rename_all = "screaming_snake_case")]
25enum Opt {
26 // This subcommand will be named `FIRST_COMMAND`. As the command doesn't
27 // override the initial casing style, ...
28 /// A screaming loud first command. Only use if necessary.
29 FirstCommand {
30 // this flag will be available as `--FOO` and `-F`.
31 /// This flag will even scream louder.
32 #[structopt(long, short)]
33 foo: bool,
34 },
35
36 // As we override the casing style for this variant the related subcommand
37 // will be named `SecondCommand`.
38 /// Not nearly as loud as the first command.
39 #[structopt(rename_all = "pascal_case")]
40 SecondCommand {
41 // We can also override it again on a single field.
42 /// Nice quiet flag. No one is annoyed.
43 #[structopt(rename_all = "snake_case", long)]
44 bar_option: bool,
45
46 // Renaming will not be propagated into subcommand flagged enums. If
47 // a non default casing style is required it must be defined on the
48 // enum itself.
49 #[structopt(subcommand)]
50 cmds: Subcommands,
51
52 // or flattened structs.
53 #[structopt(flatten)]
54 options: BonusOptions,
55 },
56}
57
58#[derive(StructOpt, Debug)]
59enum Subcommands {
60 // This one will be available as `first-subcommand`.
61 FirstSubcommand,
62}
63
64#[derive(StructOpt, Debug)]
65struct BonusOptions {
66 // And this one will be available as `baz-option`.
67 #[structopt(long)]
68 baz_option: bool,
69}
70
71fn main() {
72 let opt = Opt::from_args();
73 println!("{:?}", opt);
74}