File: enum_in_args_with_strum.rs

package info (click to toggle)
rust-structopt 0.3.26-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 796 kB
  • sloc: makefile: 2
file content (42 lines) | stat: -rw-r--r-- 982 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! Running this example with --help prints this message:
//! -----------------------------------------------------
//! structopt 0.3.25
//! 
//! USAGE:
//!     enum_in_args_with_strum [OPTIONS]
//! 
//! FLAGS:
//!     -h, --help       Prints help information
//!     -V, --version    Prints version information
//! 
//! OPTIONS:
//!         --format <format>     [default: txt]  [possible values: txt, md, html]
//! -----------------------------------------------------

use structopt::StructOpt;
use strum::{EnumString, EnumVariantNames, VariantNames};

const DEFAULT: &str = "txt";

#[derive(StructOpt, Debug)]
struct Opt {
    #[structopt(
        long,
        possible_values = Format::VARIANTS,
        case_insensitive = true,
        default_value = DEFAULT,
    )]
    format: Format,
}

#[derive(EnumString, EnumVariantNames, Debug)]
#[strum(serialize_all = "kebab_case")]
enum Format {
    Txt,
    Md,
    Html,
}

fn main() {
    println!("{:?}", Opt::from_args());
}