Skip to content

Commit a244127

Browse files
committed
Merge #129: Use clap for parsing CLI args
8f75f35 fmt: fix formatting (Andrew Cann) f930ae3 cli: add flag for including debug symbols (Andrew Cann) Pull request description: Add clap as a dependency and use it to parse the command line arguments. Also add a --debug cli option to include debug symbols. ACKs for top commit: apoelstra: ACK 8f75f35; successfully ran local tests Tree-SHA512: 72ad9dc2b078dbf0442b8cf2d569e95565b49a309e487856f4940296555360972c0907737481f9a0cafe71f9cdb5fac2ffbc89c8fe8a8d23954b43ebebc769a4
2 parents b810cc2 + 8f75f35 commit a244127

File tree

3 files changed

+235
-45
lines changed

3 files changed

+235
-45
lines changed

Cargo.lock

+177-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ miniscript = "12.3.1"
3131
either = "1.12.0"
3232
itertools = "0.13.0"
3333
arbitrary = { version = "1", optional = true, features = ["derive"] }
34+
clap = "4.5.37"
3435

3536
[target.wasm32-unknown-unknown.dependencies]
3637
getrandom = { version = "0.2", features = ["js"] }

src/main.rs

+57-43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use base64::display::Base64Display;
22
use base64::engine::general_purpose::STANDARD;
3+
use clap::{Arg, ArgAction, Command};
34

45
use simfony::{Arguments, CompiledProgram};
56
use std::env;
@@ -13,30 +14,69 @@ fn main() {
1314
}
1415
}
1516

16-
#[cfg(feature = "serde")]
1717
fn run() -> Result<(), String> {
18-
let args: Vec<String> = env::args().collect();
18+
let command = {
19+
Command::new(env!("CARGO_BIN_NAME"))
20+
.about(
21+
"\
22+
Compile the given Simfony program and print the resulting Simplicity base64 string.\n\
23+
If a Simfony witness is provided, then use it to satisfy the program (requires \
24+
feature 'serde' to be enabled).\
25+
",
26+
)
27+
.arg(
28+
Arg::new("prog_file")
29+
.required(true)
30+
.value_name("PROGRAM_FILE")
31+
.action(ArgAction::Set)
32+
.help("Simfony program file to build"),
33+
)
34+
};
1935

20-
if args.len() < 2 {
21-
println!("Usage: {} PROGRAM_FILE [WITNESS_FILE]", args[0]);
22-
println!(
23-
"Compile the given Simfony program and print the resulting Simplicity base64 string."
24-
);
25-
println!("If a Simfony witness is provided, then use it to satisfy the program.");
26-
return Ok(());
27-
}
36+
#[cfg(feature = "serde")]
37+
let command = {
38+
command.arg(
39+
Arg::new("wit_file")
40+
.value_name("WITNESS_FILE")
41+
.action(ArgAction::Set)
42+
.help("File containing the witness data"),
43+
)
44+
};
2845

29-
let prog_file = &args[1];
46+
let matches = {
47+
command
48+
.arg(
49+
Arg::new("debug")
50+
.long("debug")
51+
.action(ArgAction::SetTrue)
52+
.help("Include debug symbols in the output"),
53+
)
54+
.get_matches()
55+
};
56+
57+
let prog_file = matches.get_one::<String>("prog_file").unwrap();
3058
let prog_path = std::path::Path::new(prog_file);
3159
let prog_text = std::fs::read_to_string(prog_path).map_err(|e| e.to_string())?;
32-
let compiled = CompiledProgram::new(prog_text, Arguments::default(), false)?;
60+
let include_debug_symbols = matches.get_flag("debug");
61+
62+
let compiled = CompiledProgram::new(prog_text, Arguments::default(), include_debug_symbols)?;
3363

34-
if args.len() >= 3 {
35-
let wit_file = &args[2];
36-
let wit_path = std::path::Path::new(wit_file);
37-
let wit_text = std::fs::read_to_string(wit_path).map_err(|e| e.to_string())?;
38-
let witness = serde_json::from_str::<simfony::WitnessValues>(&wit_text).unwrap();
64+
#[cfg(feature = "serde")]
65+
let witness_opt = {
66+
matches
67+
.get_one::<String>("wit_file")
68+
.map(|wit_file| -> Result<simfony::WitnessValues, String> {
69+
let wit_path = std::path::Path::new(wit_file);
70+
let wit_text = std::fs::read_to_string(wit_path).map_err(|e| e.to_string())?;
71+
let witness = serde_json::from_str::<simfony::WitnessValues>(&wit_text).unwrap();
72+
Ok(witness)
73+
})
74+
.transpose()?
75+
};
76+
#[cfg(not(feature = "serde"))]
77+
let witness_opt: Option<simfony::WitnessValues> = None;
3978

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

5898
Ok(())
5999
}
60-
61-
#[cfg(not(feature = "serde"))]
62-
fn run() -> Result<(), String> {
63-
let args: Vec<String> = env::args().collect();
64-
65-
if args.len() < 2 {
66-
println!("Usage: {} PROGRAM_FILE", args[0]);
67-
println!(
68-
"Compile the given Simfony program and print the resulting Simplicity base64 string."
69-
);
70-
return Ok(());
71-
}
72-
73-
let prog_file = &args[1];
74-
let prog_path = std::path::Path::new(prog_file);
75-
let prog_text = std::fs::read_to_string(prog_path).map_err(|e| e.to_string())?;
76-
let compiled = CompiledProgram::new(prog_text, Arguments::default(), false)?;
77-
78-
let program_bytes = compiled.commit().encode_to_vec();
79-
println!(
80-
"Program:\n{}",
81-
Base64Display::new(&program_bytes, &STANDARD)
82-
);
83-
84-
Ok(())
85-
}

0 commit comments

Comments
 (0)