use structopt::StructOpt;
use structopt::clap;
#[derive(Debug, StructOpt, PartialEq)]
#[structopt(name = "cao", about = "IP Update")]
pub enum Args {
#[structopt(about = "Record operation")]
Record {
#[structopt(short, long)]
provider: String,
#[structopt(short, long)]
key: String,
#[structopt(short, long)]
domain: String,
#[structopt(subcommand)]
cmd: RecordCmds,
},
#[structopt(about = "List interfaces")]
Interface {
#[structopt(short, long)]
interface: Option<String>
},
}
#[derive(Debug, StructOpt, PartialEq)]
pub enum RecordCmds {
#[structopt(about = "Add a record")]
Add {
#[structopt(short, long = "sub")]
sub_domain: String,
#[structopt(short = "t", long = "type")]
record_type: String,
#[structopt(short = "l", long = "line")]
record_line: String,
#[structopt(short, long)]
value: Option<String>,
#[structopt(long = "if")]
interface: Option<String>,
},
#[structopt(about = "List records")]
List {
#[structopt(short, long)]
offset: Option<i32>,
#[structopt(short, long)]
length: Option<i32>,
#[structopt(short, long)]
sub_domain: Option<String>,
},
#[structopt(about = "Modify a record")]
Modify {
#[structopt(short = "i", long = "id")]
record_id: i32,
#[structopt(short, long = "sub")]
sub_domain: Option<String>,
#[structopt(short = "t", long = "type")]
record_type: String,
#[structopt(short = "l", long = "line")]
record_line: String,
#[structopt(short, long)]
value: Option<String>,
#[structopt(long = "if")]
interface: Option<String>,
},
#[structopt(about = "Delete a record")]
Delete {
#[structopt(short = "i", long = "id")]
record_id: i32,
},
}
fn missing_if_or_value() -> clap::Error {
clap::Error{
message: String::from("error: Missing one of following required arguments:\n --if or --value"),
kind: clap::ErrorKind::MissingRequiredArgument,
info: None,
}
}
impl Args {
pub fn args() -> Result<Self, clap::Error> {
let args = Self::from_args_safe()?;
match &args {
Args::Record{cmd, ..} => {
match cmd {
RecordCmds::Add{value, interface, ..} => {
if value.is_none() && interface.is_none() {
return Err(missing_if_or_value());
}
},
RecordCmds::Modify{value, interface, ..} => {
if value.is_none() && interface.is_none() {
return Err(missing_if_or_value());
}
},
_ => {}
}
},
_ => {}
};
Ok(args)
}
}