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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use super::GetWithDefault;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

/// This struct provides the `--host` and `-H` cli option to get an IPv4 address
///
/// No default is provided
///
/// ```rust
/// extern crate structopt_flags;
/// #[macro_use]
/// extern crate structopt;
///
/// use std::net::Ipv4Addr;
/// use structopt::StructOpt;
/// use structopt_flags::GetWithDefault; // to access get_ipv4_addr
///
/// #[derive(Debug, StructOpt)]
/// #[structopt(name = "ipv4_opt", about = "An example using the HostV4Opt option")]
/// struct Opt {
///     #[structopt(flatten)]
///     host_ip: structopt_flags::HostV4Opt,
/// }
///
/// fn main() {
///     let opt = Opt::from_args();
///     let ipv4 = opt.host_ip.get_with_default(Ipv4Addr::new(127,0,0,1));
/// }
/// ```

#[derive(StructOpt, Debug, Clone)]
pub struct HostV4Opt {
    /// Set the host IP (Ipv4 only)
    #[structopt(
        name = "hostv4",
        long = "host",
        short = "-H",
        raw(global = "true")
    )]
    host_addr: Option<Ipv4Addr>,
}

impl GetWithDefault for HostV4Opt {
    type Item = Ipv4Addr;
    fn get_with_default(&self, default: Self::Item) -> Self::Item {
        self.host_addr.unwrap_or(default)
    }
}

/// This struct provides the `--host` and `-H` cli option to get an IPv6 address
///
/// No default is provided
///
/// ```rust
/// extern crate structopt_flags;
/// #[macro_use]
/// extern crate structopt;
///
/// use std::net::Ipv6Addr;
/// use structopt::StructOpt;
/// use structopt_flags::GetWithDefault; // to access get_ipv4_addr
///
/// #[derive(Debug, StructOpt)]
/// #[structopt(name = "ipv6_opt", about = "An example using the HostV6Opt option")]
/// struct Opt {
///     #[structopt(flatten)]
///     host_ip: structopt_flags::HostV6Opt,
/// }
///
/// fn main() {
///     let opt = Opt::from_args();
///     let ipv6 = opt.host_ip.get_with_default(Ipv6Addr::new(0,0,0,0,0,0,0,1));
/// }
/// ```

#[derive(StructOpt, Debug, Clone)]
pub struct HostV6Opt {
    /// Set the host IP (Ipv6 only)
    #[structopt(
        name = "hostv6",
        long = "host",
        short = "-H",
        raw(global = "true")
    )]
    host_addr: Option<Ipv6Addr>,
}

impl GetWithDefault for HostV6Opt {
    type Item = Ipv6Addr;
    fn get_with_default(&self, default: Self::Item) -> Self::Item {
        self.host_addr.unwrap_or(default)
    }
}

/// This struct provides the `--host` and `-H` cli option to get ageneric IP address
///
/// No default is provided
///
/// ```rust
/// extern crate structopt_flags;
/// #[macro_use]
/// extern crate structopt;
///
/// use std::net::{IpAddr,Ipv6Addr};
/// use structopt::StructOpt;
/// use structopt_flags::GetWithDefault; // to access get_ipv4_addr
///
/// #[derive(Debug, StructOpt)]
/// #[structopt(name = "ip_opt", about = "An example using the HostOpt option")]
/// struct Opt {
///     #[structopt(flatten)]
///     host_ip: structopt_flags::HostOpt,
/// }
///
/// fn main() {
///     let opt = Opt::from_args();
///     let ip = opt.host_ip.get_with_default(IpAddr::V6(Ipv6Addr::new(0,0,0,0,0,0,0,1)));
/// }
/// ```

#[derive(StructOpt, Debug, Clone)]
pub struct HostOpt {
    /// Set the host IP (both IpV4 and IpV6 are supported)
    #[structopt(
        name = "host",
        long = "host",
        short = "-H",
        raw(global = "true")
    )]
    host_addr: Option<IpAddr>,
}

impl GetWithDefault for HostOpt {
    type Item = IpAddr;
    fn get_with_default(&self, default: Self::Item) -> Self::Item {
        self.host_addr.unwrap_or(default)
    }
}