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
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
use crate::GetWithDefault;
use std::fmt;
use std::path::PathBuf;
use structopt::StructOpt;

/// This struct provides the `--config_file` cli option
///
/// The option is mandatory and require a filename
///
/// ```should_panic
/// extern crate structopt_flags;
/// #[macro_use]
/// extern crate structopt;
///
/// use structopt::StructOpt;
/// use structopt_flags::ConfigFile; // to access get_log_level
///
/// #[derive(Debug, StructOpt)]
/// #[structopt(name = "config-file", about = "An example using config_file option")]
/// struct Opt {
///     #[structopt(flatten)]
///     config: structopt_flags::ConfigFile,
/// }
///
/// fn main() {
///     let opt = Opt::from_args();
///     let config_file = opt.config.get_filename();
///     // use the config file
/// }
/// ```
#[derive(StructOpt, Debug, Clone)]
pub struct ConfigFile {
    /// Set the configuration file
    #[structopt(name = "config_file", long = "config", short = "c", parse(from_os_str))]
    filename: PathBuf,
}

impl ConfigFile {
    pub fn get_filename(&self) -> PathBuf {
        self.filename.clone()
    }
}

impl fmt::Display for ConfigFile {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            self.filename.to_str().unwrap_or("not unicode filename")
        )
    }
}

/// This struct provides the `--config_file` cli option
///
/// The option is no mandatory, but a default value can be provided wit the get_with_default()
///
/// ```rust
/// extern crate structopt_flags;
/// #[macro_use]
/// extern crate structopt;
///
/// use structopt::StructOpt;
/// use structopt_flags::GetWithDefault; // to access get_log_level
/// use std::path::PathBuf;
///
/// #[derive(Debug, StructOpt)]
/// #[structopt(name = "config-file", about = "An example using config_file option")]
/// struct Opt {
///     #[structopt(flatten)]
///     config: structopt_flags::ConfigFileNoDef,
/// }
///
/// fn main() {
///     let opt = Opt::from_args();
///     let config_file = opt.config.get_with_default("config-file.toml");
///     // use the config file
/// }
/// ```
#[derive(StructOpt, Debug, Clone)]
pub struct ConfigFileNoDef {
    /// Set the configuration file
    #[structopt(
        name = "config_file",
        long = "config",
        short = "c",
        parse(from_os_str),
        raw(global = "true")
    )]
    filename: Option<PathBuf>,
}

impl GetWithDefault for ConfigFileNoDef {
    type Item = PathBuf;
    fn get_with_default<T: Into<Self::Item>>(&self, default: T) -> Self::Item {
        match &self.filename {
            Some(x) => x.clone(),
            None => default.into(),
        }
    }
}

impl fmt::Display for ConfigFileNoDef {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.filename {
            Some(x) => write!(f, "{}", x.to_str().unwrap_or("not unicode filename")),
            None => write!(f, "None"),
        }
    }
}