Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame] | 1 | //! 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. |
Haibo Huang | e7bfadf | 2020-09-23 21:23:42 -0700 | [diff] [blame] | 20 | //! |
| 21 | //! - **Lower Case**: Keep all letters lowercase and remove word boundaries. |
| 22 | //! |
| 23 | //! - **Upper Case**: Keep all letters upperrcase and remove word boundaries. |
Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame] | 24 | |
| 25 | use structopt::StructOpt; |
| 26 | |
| 27 | #[derive(StructOpt, Debug)] |
| 28 | #[structopt(name = "rename_all", rename_all = "screaming_snake_case")] |
| 29 | enum Opt { |
| 30 | // This subcommand will be named `FIRST_COMMAND`. As the command doesn't |
| 31 | // override the initial casing style, ... |
| 32 | /// A screaming loud first command. Only use if necessary. |
| 33 | FirstCommand { |
| 34 | // this flag will be available as `--FOO` and `-F`. |
| 35 | /// This flag will even scream louder. |
| 36 | #[structopt(long, short)] |
| 37 | foo: bool, |
| 38 | }, |
| 39 | |
| 40 | // As we override the casing style for this variant the related subcommand |
| 41 | // will be named `SecondCommand`. |
| 42 | /// Not nearly as loud as the first command. |
| 43 | #[structopt(rename_all = "pascal_case")] |
| 44 | SecondCommand { |
| 45 | // We can also override it again on a single field. |
| 46 | /// Nice quiet flag. No one is annoyed. |
| 47 | #[structopt(rename_all = "snake_case", long)] |
| 48 | bar_option: bool, |
| 49 | |
| 50 | // Renaming will not be propagated into subcommand flagged enums. If |
| 51 | // a non default casing style is required it must be defined on the |
| 52 | // enum itself. |
| 53 | #[structopt(subcommand)] |
| 54 | cmds: Subcommands, |
| 55 | |
| 56 | // or flattened structs. |
| 57 | #[structopt(flatten)] |
| 58 | options: BonusOptions, |
| 59 | }, |
| 60 | } |
| 61 | |
| 62 | #[derive(StructOpt, Debug)] |
| 63 | enum Subcommands { |
| 64 | // This one will be available as `first-subcommand`. |
| 65 | FirstSubcommand, |
| 66 | } |
| 67 | |
| 68 | #[derive(StructOpt, Debug)] |
| 69 | struct BonusOptions { |
| 70 | // And this one will be available as `baz-option`. |
| 71 | #[structopt(long)] |
| 72 | baz_option: bool, |
| 73 | } |
| 74 | |
| 75 | fn main() { |
| 76 | let opt = Opt::from_args(); |
| 77 | println!("{:?}", opt); |
| 78 | } |