Skip to content

Commit

Permalink
Merge pull request #1060 from dtolnay/clap
Browse files Browse the repository at this point in the history
Update to clap 3.2
  • Loading branch information
dtolnay committed Jun 18, 2022
2 parents f1d7bff + 2a606a3 commit 52dfbe1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 44 deletions.
2 changes: 1 addition & 1 deletion gen/cmd/Cargo.toml
Expand Up @@ -21,7 +21,7 @@ path = "src/main.rs"
experimental-async-fn = []

[dependencies]
clap = { version = "3.1", default-features = false, features = ["std", "suggestions"] }
clap = { version = "3.2", default-features = false, features = ["std", "suggestions"] }
codespan-reporting = "0.11"
proc-macro2 = { version = "1.0.39", default-features = false, features = ["span-locations"] }
quote = { version = "1.0", default-features = false }
Expand Down
55 changes: 24 additions & 31 deletions gen/cmd/src/app.rs
Expand Up @@ -6,10 +6,11 @@ use super::{Opt, Output};
use crate::cfg::{self, CfgValue};
use crate::gen::include::Include;
use crate::syntax::IncludeKind;
use clap::builder::{ArgAction, ValueParser};
use clap::{Arg, Command};
use std::collections::{BTreeMap as Map, BTreeSet as Set};
use std::ffi::OsStr;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, PoisonError};
use syn::parse::Parser;

const USAGE: &str = "\
Expand Down Expand Up @@ -63,11 +64,13 @@ const CFG: &str = "cfg";
pub(super) fn from_args() -> Opt {
let matches = app().get_matches();

let input = matches.value_of_os(INPUT).map(PathBuf::from);
let cxx_impl_annotations = matches.value_of(CXX_IMPL_ANNOTATIONS).map(str::to_owned);
let header = matches.is_present(HEADER);
let input = matches.get_one::<PathBuf>(INPUT).cloned();
let cxx_impl_annotations = matches
.get_one::<String>(CXX_IMPL_ANNOTATIONS)
.map(String::clone);
let header = matches.contains_id(HEADER);
let include = matches
.values_of(INCLUDE)
.get_many::<String>(INCLUDE)
.unwrap_or_default()
.map(|include| {
if include.starts_with('<') && include.ends_with('>') {
Expand All @@ -85,19 +88,19 @@ pub(super) fn from_args() -> Opt {
.collect();

let mut outputs = Vec::new();
for path in matches.values_of_os(OUTPUT).unwrap_or_default() {
outputs.push(if path == "-" {
for path in matches.get_many::<PathBuf>(OUTPUT).unwrap_or_default() {
outputs.push(if path.as_os_str() == "-" {
Output::Stdout
} else {
Output::File(PathBuf::from(path))
Output::File(path.clone())
});
}
if outputs.is_empty() {
outputs.push(Output::Stdout);
}

let mut cfg = Map::new();
for arg in matches.values_of(CFG).unwrap_or_default() {
for arg in matches.get_many::<String>(CFG).unwrap_or_default() {
let (name, value) = cfg::parse.parse_str(arg).unwrap();
cfg.entry(name).or_insert_with(Set::new).insert(value);
}
Expand All @@ -112,19 +115,11 @@ pub(super) fn from_args() -> Opt {
}
}

fn validate_utf8(arg: &OsStr) -> Result<(), &'static str> {
if arg.to_str().is_some() {
Ok(())
} else {
Err("invalid utf-8 sequence")
}
}

fn arg_input() -> Arg<'static> {
Arg::new(INPUT)
.help("Input Rust source file containing #[cxx::bridge].")
.required_unless_present(HEADER)
.allow_invalid_utf8(true)
.value_parser(ValueParser::path_buf())
}

fn arg_cxx_impl_annotations() -> Arg<'static> {
Expand All @@ -138,8 +133,7 @@ these C++ functions in another.";
.long(CXX_IMPL_ANNOTATIONS)
.takes_value(true)
.value_name("annotation")
.allow_invalid_utf8(true)
.validator_os(validate_utf8)
.value_parser(ValueParser::string())
.help(HELP)
}

Expand All @@ -159,9 +153,8 @@ into the generated C++ code as #include lines.";
.long(INCLUDE)
.short('i')
.takes_value(true)
.multiple_occurrences(true)
.allow_invalid_utf8(true)
.validator_os(validate_utf8)
.action(ArgAction::Append)
.value_parser(ValueParser::string())
.help(HELP)
}

Expand All @@ -173,32 +166,32 @@ not specified.";
.long(OUTPUT)
.short('o')
.takes_value(true)
.multiple_occurrences(true)
.allow_invalid_utf8(true)
.validator_os(validate_utf8)
.action(ArgAction::Append)
.value_parser(ValueParser::path_buf())
.help(HELP)
}

fn arg_cfg() -> Arg<'static> {
const HELP: &str = "\
Compilation configuration matching what will be used to build
the Rust side of the bridge.";
let mut bool_cfgs = Map::<String, bool>::new();
let bool_cfgs = Arc::new(Mutex::new(Map::<String, bool>::new()));
Arg::new(CFG)
.long(CFG)
.takes_value(true)
.value_name("name=\"value\" | name[=true] | name=false")
.multiple_occurrences(true)
.validator(move |arg| match cfg::parse.parse_str(arg) {
Ok((_, CfgValue::Str(_))) => Ok(()),
.action(ArgAction::Append)
.value_parser(move |arg: &str| match cfg::parse.parse_str(arg) {
Ok((_, CfgValue::Str(_))) => Ok(arg.to_owned()),
Ok((name, CfgValue::Bool(value))) => {
let mut bool_cfgs = bool_cfgs.lock().unwrap_or_else(PoisonError::into_inner);
if let Some(&prev) = bool_cfgs.get(&name) {
if prev != value {
return Err(format!("cannot have both {0}=false and {0}=true", name));
}
}
bool_cfgs.insert(name, value);
Ok(())
Ok(arg.to_owned())
}
Err(_) => Err("expected name=\"value\", name=true, or name=false".to_owned()),
})
Expand Down
8 changes: 4 additions & 4 deletions third-party/BUCK
Expand Up @@ -17,8 +17,8 @@ rust_library(

rust_library(
name = "clap",
srcs = glob(["vendor/clap-3.1.18/src/**"]),
edition = "2018",
srcs = glob(["vendor/clap-3.2.1/src/**"]),
edition = "2021",
features = ["std"],
visibility = ["PUBLIC"],
deps = [
Expand All @@ -31,8 +31,8 @@ rust_library(

rust_library(
name = "clap_lex",
srcs = glob(["vendor/clap_lex-0.2.0/src/**"]),
edition = "2018",
srcs = glob(["vendor/clap_lex-0.2.2/src/**"]),
edition = "2021",
deps = [
":os_str_bytes",
],
Expand Down
8 changes: 4 additions & 4 deletions third-party/BUILD
Expand Up @@ -20,9 +20,9 @@ rust_library(

rust_library(
name = "clap",
srcs = glob(["vendor/clap-3.1.18/src/**"]),
srcs = glob(["vendor/clap-3.2.1/src/**"]),
crate_features = ["std"],
edition = "2018",
edition = "2021",
visibility = ["//visibility:public"],
deps = [
":bitflags",
Expand All @@ -34,8 +34,8 @@ rust_library(

rust_library(
name = "clap_lex",
srcs = glob(["vendor/clap_lex-0.2.0/src/**"]),
edition = "2018",
srcs = glob(["vendor/clap_lex-0.2.2/src/**"]),
edition = "2021",
deps = [
":os_str_bytes",
],
Expand Down
8 changes: 4 additions & 4 deletions third-party/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 52dfbe1

Please sign in to comment.