From 58db41bb354e938fd88a44116952db9024a6fda0 Mon Sep 17 00:00:00 2001 From: sushi-shi <47691267+sushi-shi@users.noreply.github.com> Date: Sun, 29 May 2022 21:24:45 +0000 Subject: [PATCH] cli: Allow passing arguments to a script (#1914) --- CHANGELOG.md | 1 + cli/src/lib.rs | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b96be7e7..85d3f34bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ com/project-serum/anchor/pull/1841)). * cli: Add `b` and `t` aliases for `build` and `test` respectively ([#1823](https://github.com/project-serum/anchor/pull/1823)). * spl: Add more derived traits to `TokenAccount` to `Mint` ([#1818](https://github.com/project-serum/anchor/pull/1818)). * spl: Add `sync_native` token program CPI wrapper function ([#1833](https://github.com/project-serum/anchor/pull/1833)). +* cli: Allow passing arguments to an underlying script with `anchor run` ([#1914](https://github.com/project-serum/anchor/pull/1914)). * ts: Implement a coder for system program ([#1920](https://github.com/project-serum/anchor/pull/1920)). * ts: Add `program.coder.types` for encoding/decoding user-defined types ([#1931](https://github.com/project-serum/anchor/pull/1931)). * client: Add send_with_spinner_and_config function to RequestBuilder ([#1926](https://github.com/project-serum/anchor/pull/1926)). diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 0dfc86ffe..29a6dd90d 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -240,6 +240,14 @@ pub enum Command { Run { /// The name of the script to run. script: String, + /// Argument to pass to the underlying script. + #[clap( + required = false, + takes_value = true, + multiple_values = true, + last = true + )] + script_args: Vec, }, /// Saves an api token from the registry locally. Login { @@ -469,7 +477,10 @@ pub fn entry(opts: Opts) -> Result<()> { Command::Airdrop { .. } => airdrop(&opts.cfg_override), Command::Cluster { subcmd } => cluster(subcmd), Command::Shell => shell(&opts.cfg_override), - Command::Run { script } => run(&opts.cfg_override, script), + Command::Run { + script, + script_args, + } => run(&opts.cfg_override, script, script_args), Command::Login { token } => login(&opts.cfg_override, token), Command::Publish { program, @@ -2809,16 +2820,17 @@ fn shell(cfg_override: &ConfigOverride) -> Result<()> { }) } -fn run(cfg_override: &ConfigOverride, script: String) -> Result<()> { +fn run(cfg_override: &ConfigOverride, script: String, script_args: Vec) -> Result<()> { with_workspace(cfg_override, |cfg| { let url = cluster_url(cfg, &cfg.test_validator); let script = cfg .scripts .get(&script) .ok_or_else(|| anyhow!("Unable to find script"))?; + let script_with_args = format!("{script} {}", script_args.join(" ")); let exit = std::process::Command::new("bash") .arg("-c") - .arg(&script) + .arg(&script_with_args) .env("ANCHOR_PROVIDER_URL", url) .env("ANCHOR_WALLET", cfg.provider.wallet.to_string()) .stdout(Stdio::inherit())