Skip to content

Commit 388bae4

Browse files
authored
Merge pull request #101 from SpiceCodeCLI/analyze-json-flag
add --json flag to analyze command
2 parents 5c47749 + f514147 commit 388bae4

1 file changed

Lines changed: 50 additions & 27 deletions

File tree

cli/main.py

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ def version():
142142
print(f"[red]{messages.get('error', 'Error:')}[/] {e}")
143143

144144

145+
# SPICE ANALYZE COMMAND
145146
@app.command()
146-
def analyze(file: str, all: bool = typer.Option(False, "--all", help="Analyze all stats without selection menu")):
147+
def analyze(
148+
file: str,
149+
all: bool = typer.Option(False, "--all", help="Analyze all stats without selection menu"),
150+
json_output: bool = typer.Option(False, "--json", help="Output results in JSON format")
151+
):
147152
"""
148153
Analyze the given file.
149154
"""
@@ -169,41 +174,59 @@ def analyze(file: str, all: bool = typer.Option(False, "--all", help="Analyze al
169174
if all:
170175
selected_stat_keys = available_stats
171176
else:
172-
# print checkbox menu to select which stats to show
173-
selected_stats = inquirer.checkbox(
174-
message=messages.get("select_stats", "Select stats to display:"),
175-
choices=[stats_labels[stat] for stat in available_stats],
176-
pointer="> ",
177-
default=[stats_labels[stat] for stat in available_stats], # All selected by default
178-
instruction=messages.get("checkbox_hint", "(Use space to select, enter to confirm)")
179-
).execute()
180-
181-
# if no stats were selected
182-
if not selected_stats:
183-
print(messages.get("no_stats_selected", "No stats selected. Analysis cancelled."))
184-
return
185-
186-
# create a mapping from displayed labels back to stat keys
187-
reverse_mapping = {v: k for k, v in stats_labels.items()}
188-
189-
# convert selected labels back to stat keys
190-
selected_stat_keys = [reverse_mapping[label] for label in selected_stats]
177+
# Don't show interactive menu in JSON mode (assumes all stats)
178+
if json_output:
179+
selected_stat_keys = available_stats
180+
else:
181+
# print checkbox menu to select which stats to show
182+
selected_stats = inquirer.checkbox(
183+
message=messages.get("select_stats", "Select stats to display:"),
184+
choices=[stats_labels[stat] for stat in available_stats],
185+
pointer="> ",
186+
default=[stats_labels[stat] for stat in available_stats], # All selected by default
187+
instruction=messages.get("checkbox_hint", "(Use space to select, enter to confirm)")
188+
).execute()
189+
190+
# if no stats were selected
191+
if not selected_stats:
192+
if json_output:
193+
import json
194+
print(json.dumps({"error": messages.get("no_stats_selected", "No stats selected. Analysis cancelled.")}))
195+
else:
196+
print(messages.get("no_stats_selected", "No stats selected. Analysis cancelled."))
197+
return
198+
199+
# create a mapping from displayed labels back to stat keys
200+
reverse_mapping = {v: k for k, v in stats_labels.items()}
201+
202+
# convert selected labels back to stat keys
203+
selected_stat_keys = [reverse_mapping[label] for label in selected_stats]
191204

192205
# try to analyze and if error then print the error
193206
try:
194-
# show analyzing message
195-
print(f"{messages['analyzing_file']}: {file}")
207+
# show analyzing message if not in JSON mode
208+
if not json_output:
209+
print(f"{messages['analyzing_file']}: {file}")
196210

197211
# get analysis results from analyze_file
198212
results = analyze_file(file, selected_stats=selected_stat_keys)
199213

200-
# only print the selected stats
201-
for stat in selected_stat_keys:
202-
if stat in results:
203-
print(messages[stat].format(count=results[stat]))
214+
# output in JSON format if flag
215+
if json_output:
216+
import json
217+
print(json.dumps(results, indent=2))
218+
else:
219+
# only print the selected stats in normal mode
220+
for stat in selected_stat_keys:
221+
if stat in results:
222+
print(messages[stat].format(count=results[stat]))
204223

205224
except Exception as e:
206-
print(f"[red]{messages['error']}[/] {e}")
225+
if json_output:
226+
import json
227+
print(json.dumps({"error": str(e)}))
228+
else:
229+
print(f"[red]{messages['error']}[/] {e}")
207230

208231

209232
def main():

0 commit comments

Comments
 (0)