Skip to content

Commit 5fd1570

Browse files
committed
cargo-apk: Reimplement "default" subcommand trailing args for cargo with -- separator
`clap` [does not currently support] parsing unknown arguments into a side `Vec`, which is exactly what `cargo apk --` needs to parse a few known `cargo` arguments (such as `--target` and `-p`) but forward the rest verbatim to the underlying `cargo` subcommand. `allow_hyphen_values = true` could partially help us out with this, but it parses all remaining arguments into that `Vec` upon encountering the first unknown flag/arg, resulting in all known flags after that to also be treated as "unknown" instead of filling up our `args: Args` struct. Since [a workaround for this isn't currently functioning], introduce pure trailing args with an additional `--` separator to make it clear which arguments go to `cargo-apk` (and are almost all, except `--device`, forwarded to `cargo`) and which are only passed to `cargo <subcommand>`. [does not currently support]: clap-rs/clap#1404 [a workaround for this isn't currently functioning]: clap-rs/clap#1404 (comment)
1 parent 107f03e commit 5fd1570

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

cargo-apk/src/apk.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<'a> ApkBuilder<'a> {
306306
Ok(())
307307
}
308308

309-
pub fn default(&self, cargo_cmd: &str) -> Result<(), Error> {
309+
pub fn default(&self, cargo_cmd: &str, additional_args: &[String]) -> Result<(), Error> {
310310
for target in &self.build_targets {
311311
let mut cargo = cargo_ndk(
312312
&self.ndk,
@@ -322,6 +322,10 @@ impl<'a> ApkBuilder<'a> {
322322
cargo.arg("--target").arg(triple);
323323
}
324324

325+
for additional_arg in additional_args {
326+
cargo.arg(additional_arg);
327+
}
328+
325329
if !cargo.status()?.success() {
326330
return Err(NdkError::CmdFailed(cargo).into());
327331
}

cargo-apk/src/main.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ struct Args {
2727
}
2828

2929
#[derive(clap::Subcommand)]
30-
#[clap(trailing_var_arg = true)]
3130
enum ApkSubCmd {
3231
/// Analyze the current package and report errors, but don't build object files nor an apk
3332
#[clap(visible_alias = "c")]
@@ -44,9 +43,20 @@ enum ApkSubCmd {
4443
/// Invoke `cargo` under the detected NDK environment
4544
#[clap(name = "--")]
4645
Ndk {
46+
/// `cargo` subcommand to run
4747
cargo_cmd: String,
48+
49+
/// Arguments used to deduce the current environment. Also passed to `cargo`
4850
#[clap(flatten)]
4951
args: Args,
52+
53+
/// Additional arguments passed to `cargo`
54+
// TODO: Arguments in this vec should be intermixable with other arguments;
55+
// this is somewhat possible with `allow_hyphen_values = true` but any argument
56+
// after the first unknown arg/flag ends up inside `cargo_args` instead of being
57+
// parsed into `args: Args`.
58+
#[clap(trailing_var_arg = true, last = true)]
59+
cargo_args: Vec<String>,
5060
},
5161
/// Run a binary or example apk of the local package
5262
#[clap(visible_alias = "r")]
@@ -84,10 +94,14 @@ fn main() -> anyhow::Result<()> {
8494
builder.build(artifact)?;
8595
}
8696
}
87-
ApkSubCmd::Ndk { cargo_cmd, args } => {
97+
ApkSubCmd::Ndk {
98+
cargo_cmd,
99+
args,
100+
cargo_args,
101+
} => {
88102
let cmd = Subcommand::new(args.subcommand_args)?;
89103
let builder = ApkBuilder::from_subcommand(&cmd, args.device)?;
90-
builder.default(&cargo_cmd)?;
104+
builder.default(&cargo_cmd, &cargo_args)?;
91105
}
92106
ApkSubCmd::Run { args, no_logcat } => {
93107
let cmd = Subcommand::new(args.subcommand_args)?;

0 commit comments

Comments
 (0)