mirror of
https://gitee.com/openharmony/third_party_rust_cxx
synced 2025-02-18 15:41:39 +00:00
Use clap's required_unless to enforce input file path
This commit is contained in:
parent
7eb9c6b2a0
commit
4aea275788
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user