Skip to content

Use clap for parsing CLI args #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 177 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ miniscript = "12.3.1"
either = "1.12.0"
itertools = "0.13.0"
arbitrary = { version = "1", optional = true, features = ["derive"] }
clap = "4.5.37"

[target.wasm32-unknown-unknown.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand Down
100 changes: 57 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use base64::display::Base64Display;
use base64::engine::general_purpose::STANDARD;
use clap::{Arg, ArgAction, Command};

use simfony::{Arguments, CompiledProgram};
use std::env;
Expand All @@ -13,30 +14,69 @@ fn main() {
}
}

#[cfg(feature = "serde")]
fn run() -> Result<(), String> {
let args: Vec<String> = env::args().collect();
let command = {
Command::new(env!("CARGO_BIN_NAME"))
.about(
"\
Compile the given Simfony program and print the resulting Simplicity base64 string.\n\
If a Simfony witness is provided, then use it to satisfy the program (requires \
feature 'serde' to be enabled).\
",
)
.arg(
Arg::new("prog_file")
.required(true)
.value_name("PROGRAM_FILE")
.action(ArgAction::Set)
.help("Simfony program file to build"),
)
};

if args.len() < 2 {
println!("Usage: {} PROGRAM_FILE [WITNESS_FILE]", args[0]);
println!(
"Compile the given Simfony program and print the resulting Simplicity base64 string."
);
println!("If a Simfony witness is provided, then use it to satisfy the program.");
return Ok(());
}
#[cfg(feature = "serde")]
let command = {
command.arg(
Arg::new("wit_file")
.value_name("WITNESS_FILE")
.action(ArgAction::Set)
.help("File containing the witness data"),
)
};

let prog_file = &args[1];
let matches = {
command
.arg(
Arg::new("debug")
.long("debug")
.action(ArgAction::SetTrue)
.help("Include debug symbols in the output"),
)
.get_matches()
};

let prog_file = matches.get_one::<String>("prog_file").unwrap();
let prog_path = std::path::Path::new(prog_file);
let prog_text = std::fs::read_to_string(prog_path).map_err(|e| e.to_string())?;
let compiled = CompiledProgram::new(prog_text, Arguments::default(), false)?;
let include_debug_symbols = matches.get_flag("debug");

let compiled = CompiledProgram::new(prog_text, Arguments::default(), include_debug_symbols)?;

if args.len() >= 3 {
let wit_file = &args[2];
let wit_path = std::path::Path::new(wit_file);
let wit_text = std::fs::read_to_string(wit_path).map_err(|e| e.to_string())?;
let witness = serde_json::from_str::<simfony::WitnessValues>(&wit_text).unwrap();
#[cfg(feature = "serde")]
let witness_opt = {
matches
.get_one::<String>("wit_file")
.map(|wit_file| -> Result<simfony::WitnessValues, String> {
let wit_path = std::path::Path::new(wit_file);
let wit_text = std::fs::read_to_string(wit_path).map_err(|e| e.to_string())?;
let witness = serde_json::from_str::<simfony::WitnessValues>(&wit_text).unwrap();
Ok(witness)
})
.transpose()?
};
#[cfg(not(feature = "serde"))]
let witness_opt: Option<simfony::WitnessValues> = None;

if let Some(witness) = witness_opt {
let satisfied = compiled.satisfy(witness)?;
let (program_bytes, witness_bytes) = satisfied.redeem().encode_to_vec();
println!(
Expand All @@ -57,29 +97,3 @@ fn run() -> Result<(), String> {

Ok(())
}

#[cfg(not(feature = "serde"))]
fn run() -> Result<(), String> {
let args: Vec<String> = env::args().collect();

if args.len() < 2 {
println!("Usage: {} PROGRAM_FILE", args[0]);
println!(
"Compile the given Simfony program and print the resulting Simplicity base64 string."
);
return Ok(());
}

let prog_file = &args[1];
let prog_path = std::path::Path::new(prog_file);
let prog_text = std::fs::read_to_string(prog_path).map_err(|e| e.to_string())?;
let compiled = CompiledProgram::new(prog_text, Arguments::default(), false)?;

let program_bytes = compiled.commit().encode_to_vec();
println!(
"Program:\n{}",
Base64Display::new(&program_bytes, &STANDARD)
);

Ok(())
}
Loading