Skip to content

Commit 33cb8d2

Browse files
feat: Add file option for calldata input (#10397)
* Update opts.rs * Update args.rs * Update args-> final_args * Update crates/cast/src/opts.rs Co-authored-by: grandizzy <[email protected]> * test case * fmt * splitting the string * fmt --------- Co-authored-by: grandizzy <[email protected]>
1 parent b28c2b2 commit 33cb8d2

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

crates/cast/src/args.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,19 @@ pub async fn run_command(args: CastArgs) -> Result<()> {
195195
let tokens = SimpleCast::calldata_decode(&sig, &calldata, true)?;
196196
print_tokens(&tokens);
197197
}
198-
CastSubcommand::CalldataEncode { sig, args } => {
199-
sh_println!("{}", SimpleCast::calldata_encode(sig, &args)?)?;
198+
CastSubcommand::CalldataEncode { sig, args, file } => {
199+
let final_args = if let Some(file_path) = file {
200+
let contents = fs::read_to_string(file_path)?;
201+
contents
202+
.lines()
203+
.map(str::trim)
204+
.filter(|line| !line.is_empty())
205+
.map(String::from)
206+
.collect()
207+
} else {
208+
args
209+
};
210+
sh_println!("{}", SimpleCast::calldata_encode(sig, &final_args)?)?;
200211
}
201212
CastSubcommand::DecodeString { data } => {
202213
let tokens = SimpleCast::calldata_decode("Any(string)", &data, true)?;

crates/cast/src/opts.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ pub enum CastSubcommand {
391391
/// The arguments to encode.
392392
#[arg(allow_hyphen_values = true)]
393393
args: Vec<String>,
394+
395+
// Path to file containing arguments to encode.
396+
#[arg(long, value_name = "PATH")]
397+
file: Option<PathBuf>,
394398
},
395399

396400
/// Get the symbolic name of the current chain.
@@ -1147,6 +1151,19 @@ mod tests {
11471151
};
11481152
}
11491153

1154+
#[test]
1155+
fn parse_call_data_with_file() {
1156+
let args: Cast = Cast::parse_from(["foundry-cli", "calldata", "f()", "--file", "test.txt"]);
1157+
match args.cmd {
1158+
CastSubcommand::CalldataEncode { sig, file, args } => {
1159+
assert_eq!(sig, "f()".to_string());
1160+
assert_eq!(file, Some(PathBuf::from("test.txt")));
1161+
assert!(args.is_empty());
1162+
}
1163+
_ => unreachable!(),
1164+
};
1165+
}
1166+
11501167
// <https://github.com/foundry-rs/book/issues/1019>
11511168
#[test]
11521169
fn parse_signature() {

0 commit comments

Comments
 (0)