Update CLI to take files as input

This commit is contained in:
Pranav Gaddamadugu 2023-10-30 10:38:40 -04:00
parent 8a7f8394a9
commit adddd2ade2
2 changed files with 58 additions and 6 deletions

View File

@ -16,7 +16,7 @@
use super::*;
use snarkvm::cli::Execute as SnarkVMExecute;
use snarkvm::{cli::Execute as SnarkVMExecute, prelude::Parser as SnarkVMParser};
/// Build, Prove and Run Leo program with inputs
#[derive(Parser, Debug)]
@ -35,6 +35,9 @@ pub struct Execute {
)]
endpoint: String,
#[arg(short, long, help = "The inputs to the program, from a file. Overrides the INPUTS argument.")]
file: Option<String>,
#[clap(flatten)]
pub(crate) compiler_options: BuildOptions,
}
@ -57,8 +60,31 @@ impl Command for Execute {
// Compose the `execute` command.
let mut arguments = vec![SNARKVM_COMMAND.to_string(), self.name];
// Add the program inputs to the arguments.
arguments.append(&mut inputs);
// Add the inputs to the arguments.
match self.file {
Some(file) => {
// Get the contents from the file.
let path = context.dir()?.join(file);
let raw_content = std::fs::read_to_string(&path)
.map_err(|err| PackageError::failed_to_read_file(path.display(), err))?;
// Parse the values from the file.
let mut content = raw_content.as_str();
let mut values = vec![];
while let Ok((remaining, value)) = snarkvm::prelude::Value::<CurrentNetwork>::parse(content) {
content = remaining;
values.push(value);
}
// Check that the remaining content is empty.
if !content.trim().is_empty() {
return Err(PackageError::failed_to_read_input_file(path.display()).into());
}
// Convert the values to strings.
let mut inputs_from_file = values.into_iter().map(|value| value.to_string()).collect::<Vec<String>>();
// Add the inputs from the file to the arguments.
arguments.append(&mut inputs_from_file);
}
None => arguments.append(&mut inputs),
}
// Add the compiler options to the arguments.
if self.compiler_options.offline {

View File

@ -16,7 +16,7 @@
use super::*;
use snarkvm::cli::Run as SnarkVMRun;
use snarkvm::{cli::Run as SnarkVMRun, prelude::Parser as SnarkVMParser};
/// Build, Prove and Run Leo program with inputs
#[derive(Parser, Debug)]
@ -27,6 +27,9 @@ pub struct Run {
#[clap(name = "INPUTS", help = "The inputs to the program.")]
pub(crate) inputs: Vec<String>,
#[arg(short, long, help = "The inputs to the program, from a file. Overrides the INPUTS argument.")]
file: Option<String>,
#[clap(flatten)]
pub(crate) compiler_options: BuildOptions,
}
@ -49,8 +52,31 @@ impl Command for Run {
// Compose the `run` command.
let mut arguments = vec![SNARKVM_COMMAND.to_string(), self.name];
// Add the program inputs to the arguments.
arguments.append(&mut inputs);
// Add the inputs to the arguments.
match self.file {
Some(file) => {
// Get the contents from the file.
let path = context.dir()?.join(file);
let raw_content = std::fs::read_to_string(&path)
.map_err(|err| PackageError::failed_to_read_file(path.display(), err))?;
// Parse the values from the file.
let mut content = raw_content.as_str();
let mut values = vec![];
while let Ok((remaining, value)) = snarkvm::prelude::Value::<CurrentNetwork>::parse(content) {
content = remaining;
values.push(value);
}
// Check that the remaining content is empty.
if !content.trim().is_empty() {
return Err(PackageError::failed_to_read_input_file(path.display()).into());
}
// Convert the values to strings.
let mut inputs_from_file = values.into_iter().map(|value| value.to_string()).collect::<Vec<String>>();
// Add the inputs from the file to the arguments.
arguments.append(&mut inputs_from_file);
}
None => arguments.append(&mut inputs),
}
// Open the Leo build/ directory
let path = context.dir()?;