-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathparser.rs
More file actions
154 lines (130 loc) · 5.11 KB
/
Copy pathparser.rs
File metadata and controls
154 lines (130 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use dialoguer::{Confirm, Input, Select};
use super::Error;
pub struct ArgParser {
skip_prompt: bool,
}
impl ArgParser {
pub fn get_constructor_args(
skip_prompt: bool,
contract_name: &str,
wasm: &[u8],
) -> Result<Option<String>, Error> {
let entries = soroban_spec_tools::contract::Spec::new(wasm)?.spec;
let spec = soroban_spec_tools::Spec::new(entries.clone());
// Check if constructor function exists
let Ok(func) = spec.find_function("__constructor") else {
return Ok(None);
};
if func.inputs.is_empty() {
return Ok(None);
}
// Build the custom command for the constructor
let cmd = super::build_custom_cmd("__constructor", &spec)?;
let parser = Self { skip_prompt };
println!("\n📋 Contract '{contract_name}' requires constructor arguments:");
let args = cmd
.get_arguments()
.filter(|arg| !arg.get_id().as_str().ends_with("-file-path"))
.filter_map(|arg| parser.handle_constructor_argument(arg).transpose())
.collect::<Result<Vec<_>, _>>()?
.join(" ");
Ok((!args.is_empty()).then_some(args))
}
fn handle_constructor_argument(&self, arg: &clap::Arg) -> Result<Option<String>, Error> {
let arg_name = arg.get_id().as_str();
let help_text = arg.get_long_help().or(arg.get_help()).map_or_else(
|| "No description available".to_string(),
ToString::to_string,
);
let value_name = arg
.get_value_names()
.map_or_else(|| "VALUE".to_string(), |names| names.join(" "));
// Display help text before the prompt
println!("\n --{arg_name}");
if value_name != "bool" && !help_text.is_empty() {
println!(" {help_text}");
}
if value_name == "bool" {
self.handle_bool_argument(arg_name)
} else if value_name.contains('|') && is_simple_enum(&value_name) {
self.handle_simple_enum_argument(arg_name, &value_name)
} else {
// For all other types (complex enums, structs, strings), use string input
self.handle_formatted_input(arg_name)
}
}
fn handle_formatted_input(&self, arg_name: &str) -> Result<Option<String>, Error> {
let input_result: String = if self.skip_prompt {
String::new()
} else {
Input::new()
.with_prompt(format!("Enter value for --{arg_name}"))
.allow_empty(true)
.interact()?
};
let value = input_result.trim();
let value = if value.is_empty() {
"# TODO: <Fill in value>"
} else {
// Check if the value is already quoted
let is_already_quoted = (value.starts_with('"') && value.ends_with('"'))
|| (value.starts_with('\'') && value.ends_with('\''));
// Only wrap in quotes if it's not already quoted and contains special characters or spaces
if !is_already_quoted
&& (value.contains(' ')
|| value.contains('{')
|| value.contains('[')
|| value.contains('"'))
{
&format!("'{value}'")
} else {
value
}
};
Ok(Some(format!("--{arg_name} {value}")))
}
fn handle_simple_enum_argument(
&self,
arg_name: &str,
value_name: &str,
) -> Result<Option<String>, Error> {
// Parse the values from "a | b | c" format and add numeric options
let values: Vec<_> = value_name.split('|').collect();
if self.skip_prompt {
return Ok(Some(format!(
"--{arg_name} TODO: Pick One <{}>",
values.join(" | ")
)));
}
let mut select = Select::new()
.with_prompt(format!("Select value for --{arg_name}"))
.default(0); // This will show the cursor on the first option initially
// Add "Skip" option
select = select.item("(Skip - leave blank)");
for value in &values {
select = select.item(format!("Value: {value}"));
}
let selection = select.interact()?;
Ok((selection > 0).then(|| {
// User selected an actual value (not skip)
let selected_value = values[selection - 1];
format!("--{arg_name} {selected_value}")
}))
}
fn handle_bool_argument(&self, arg_name: &str) -> Result<Option<String>, Error> {
if self.skip_prompt {
return Ok(Some(format!("TODO add or remove <--{arg_name}>")));
}
let bool_value = Confirm::new()
.with_prompt(format!("Set --{arg_name} to true?"))
.default(false)
.interact()?;
Ok(bool_value.then(|| format!("--{arg_name}")))
}
}
fn is_simple_enum(value_name: &str) -> bool {
value_name.split('|').all(|part| {
let trimmed = part.trim();
trimmed.parse::<i32>().is_ok() || trimmed.chars().all(|c| c.is_alphabetic() || c == '_')
})
}