Use clap's required_unless to enforce input file path

This commit is contained in:
David Tolnay 2020-01-27 22:55:55 -08:00
parent 7eb9c6b2a0
commit 4aea275788
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -4,14 +4,13 @@ mod syntax;
use gen::include;
use std::io::{self, Write};
use std::path::PathBuf;
use std::process;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "cxxbridge", author)]
struct Opt {
/// Input Rust source file containing #[cxx::bridge]
#[structopt(parse(from_os_str))]
#[structopt(parse(from_os_str), required_unless = "header")]
input: Option<PathBuf>,
/// Emit header with declarations only
@ -19,22 +18,17 @@ struct Opt {
header: bool,
}
fn write(content: impl AsRef<[u8]>) {
let _ = io::stdout().lock().write_all(content.as_ref());
}
fn main() {
let opt = Opt::from_args();
if let Some(input) = opt.input {
let gen = if opt.header {
gen::do_generate_header
} else {
gen::do_generate_bridge
};
let bridge = gen(&input);
let _ = io::stdout().lock().write_all(bridge.as_ref());
} else if opt.header {
let header = include::HEADER;
let _ = io::stdout().lock().write_all(header.as_ref());
} else {
let _ = Opt::clap().after_help("").print_help();
process::exit(1);
match (opt.input, opt.header) {
(Some(input), true) => write(gen::do_generate_header(&input)),
(Some(input), false) => write(gen::do_generate_bridge(&input)),
(None, true) => write(include::HEADER),
(None, false) => unreachable!(), // enforced by required_unless
}
}