structopt_flags/
config.rs

1use crate::GetWithDefault;
2use std::fmt;
3use std::path::PathBuf;
4use structopt::StructOpt;
5
6#[derive(StructOpt, Debug, Clone)]
7pub struct ConfigFile {
8    /// Set the configuration file
9    #[structopt(name = "config_file", long = "config", short = "c", parse(from_os_str))]
10    filename: PathBuf,
11}
12
13impl ConfigFile {
14    pub fn get_filename(&self) -> PathBuf {
15        self.filename.clone()
16    }
17}
18
19impl fmt::Display for ConfigFile {
20    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21        write!(
22            f,
23            "{}",
24            self.filename.to_str().unwrap_or("not unicode filename")
25        )
26    }
27}
28
29#[derive(StructOpt, Debug, Clone)]
30pub struct ConfigFileNoDef {
31    /// Set the configuration file
32    #[structopt(
33        name = "config_file",
34        long = "config",
35        short = "c",
36        parse(from_os_str),
37        global = true
38    )]
39    filename: Option<PathBuf>,
40}
41
42impl GetWithDefault for ConfigFileNoDef {
43    type Item = PathBuf;
44    fn get_with_default<T: Into<Self::Item>>(&self, default: T) -> Self::Item {
45        match &self.filename {
46            Some(x) => x.clone(),
47            None => default.into(),
48        }
49    }
50}
51
52impl fmt::Display for ConfigFileNoDef {
53    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54        match &self.filename {
55            Some(x) => write!(f, "{}", x.to_str().unwrap_or("not unicode filename")),
56            None => write!(f, "None"),
57        }
58    }
59}