Skip to content

Commit

Permalink
cargo-apk: Reimplement "default" subcommand trailing args for cargo
Browse files Browse the repository at this point in the history
… 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)
  • Loading branch information
MarijnS95 committed Nov 22, 2022
1 parent b67f2f1 commit 5543702
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl<'a> ApkBuilder<'a> {
Ok(())
}

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

for additional_arg in cargo_args {
cargo.arg(additional_arg);
}

if !cargo.status()?.success() {
return Err(NdkError::CmdFailed(cargo).into());
}
Expand Down
20 changes: 17 additions & 3 deletions cargo-apk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ struct Args {
}

#[derive(clap::Subcommand)]
#[clap(trailing_var_arg = true)]
enum ApkSubCmd {
/// Analyze the current package and report errors, but don't build object files nor an apk
#[clap(visible_alias = "c")]
Expand All @@ -44,9 +43,20 @@ enum ApkSubCmd {
/// Invoke `cargo` under the detected NDK environment
#[clap(name = "--")]
Ndk {
/// `cargo` subcommand to run
cargo_cmd: String,

/// Arguments used to deduce the current environment. Also passed to `cargo`
#[clap(flatten)]
args: Args,

/// Additional arguments passed to `cargo`
// TODO: Arguments in this vec should be intermixable with other arguments;
// this is somewhat possible with `allow_hyphen_values = true` but any argument
// after the first unknown arg/flag ends up inside `cargo_args` instead of being
// parsed into `args: Args`.
#[clap(trailing_var_arg = true, last = true)]
cargo_args: Vec<String>,
},
/// Run a binary or example apk of the local package
#[clap(visible_alias = "r")]
Expand Down Expand Up @@ -84,10 +94,14 @@ fn main() -> anyhow::Result<()> {
builder.build(artifact)?;
}
}
ApkSubCmd::Ndk { cargo_cmd, args } => {
ApkSubCmd::Ndk {
cargo_cmd,
args,
cargo_args,
} => {
let cmd = Subcommand::new(args.subcommand_args)?;
let builder = ApkBuilder::from_subcommand(&cmd, args.device)?;
builder.default(&cargo_cmd)?;
builder.default(&cargo_cmd, &cargo_args)?;
}
ApkSubCmd::Run { args, no_logcat } => {
let cmd = Subcommand::new(args.subcommand_args)?;
Expand Down

0 comments on commit 5543702

Please sign in to comment.