Allow --config anywhere in CLI
Since clap doesn't support required + global, do our own insisting.
This commit is contained in:
1 parent
fdd50f9dc0
commit
81b148f6cd
2 files changed
+12
-7
No files matched your search
+7
-3
@@ -3,10 +3,14 @@ use structopt::{clap::AppSettings, StructOpt};
|
||||
|
||||
/// A standards-compliant bridge to Reolink IP cameras
|
||||
#[derive(StructOpt, Debug)]
|
||||
#[structopt(name = "neolink", setting(AppSettings::ArgRequiredElseHelp))]
|
||||
#[structopt(
|
||||
name = "neolink",
|
||||
setting(AppSettings::ArgRequiredElseHelp),
|
||||
setting(AppSettings::UnifiedHelpMessage)
|
||||
)]
|
||||
pub struct Opt {
|
||||
#[structopt(short, long, parse(from_os_str))]
|
||||
pub config: PathBuf,
|
||||
#[structopt(short, long, global(true), parse(from_os_str))]
|
||||
pub config: Option<PathBuf>,
|
||||
#[structopt(subcommand)]
|
||||
pub cmd: Option<Command>,
|
||||
}
|
||||
|
||||
+5
-4
@@ -36,15 +36,16 @@ fn main() -> Result<()> {
|
||||
|
||||
let opt = Opt::from_args();
|
||||
|
||||
let conf_path = opt.config.context("Must supply --config file")?;
|
||||
let config: Config = toml::from_str(
|
||||
&fs::read_to_string(&opt.config)
|
||||
.with_context(|| format!("Failed to read {:?}", &opt.config))?,
|
||||
&fs::read_to_string(&conf_path)
|
||||
.with_context(|| format!("Failed to read {:?}", conf_path))?,
|
||||
)
|
||||
.with_context(|| format!("Failed to parse the {:?} config file", &opt.config))?;
|
||||
.with_context(|| format!("Failed to parse the {:?} config file", conf_path))?;
|
||||
|
||||
config
|
||||
.validate()
|
||||
.with_context(|| format!("Failed to validate the {:?} config file", &opt.config))?;
|
||||
.with_context(|| format!("Failed to validate the {:?} config file", conf_path))?;
|
||||
|
||||
match opt.cmd {
|
||||
None => {
|
||||
|
||||
Reference in new issue
Block a user