Skip to content

Commit 98ebb19

Browse files
committed
add --json flag to analyze command
1 parent 37efe92 commit 98ebb19

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

cli/main.py

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -144,65 +144,91 @@ def version():
144144

145145
# SPICE ANALYZE COMMAND
146146
@app.command()
147-
def analyze(file: str):
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+
):
148152
"""
149153
Analyze the given file.
150154
"""
151155

152156
# load translations
153157
messages = get_translation()
154158

155-
# define available stats UPDATE THIS WHEN NEEDED PLEASE !!!!!!!!
159+
# define available stats UPDATE THIS WHEN NEEDED PLEASE !!!!!!!!
156160
available_stats = [
157161
"line_count",
158162
"function_count",
159163
"comment_line_count"
160164
]
161165

162-
# dicionary for the stats UPDATE THIS WHEN NEEDED PLEASE !!!!!!!!
166+
# dictionary for the stats UPDATE THIS WHEN NEEDED PLEASE !!!!!!!!
163167
stats_labels = {
164168
"line_count": messages.get("line_count_option", "Line Count"),
165169
"function_count": messages.get("function_count_option", "Function Count"),
166170
"comment_line_count": messages.get("comment_line_count_option", "Comment Line Count")
167171
}
168172

169-
170-
# print checkbox menu to select which stats to show
171-
selected_stats = inquirer.checkbox(
172-
message=messages.get("select_stats", "Select stats to display:"),
173-
choices=[stats_labels[stat] for stat in available_stats],
174-
pointer="> ",
175-
default=[stats_labels[stat] for stat in available_stats], # All selected by default
176-
instruction=messages.get("checkbox_hint", "(Use space to select, enter to confirm)")
177-
).execute()
178-
179-
180-
# if no stats were selected
181-
if not selected_stats:
182-
print(messages.get("no_stats_selected", "No stats selected. Analysis cancelled."))
183-
return
184-
185-
# create a mapping from displayed labels back to stat keys
186-
reverse_mapping = {v: k for k, v in stats_labels.items()}
187-
188-
# convert selected labels back to stat keys
189-
selected_stat_keys = [reverse_mapping[label] for label in selected_stats]
173+
# If --all flag is used, skip the selection menu and use all stats
174+
if all:
175+
selected_stat_keys = available_stats
176+
else:
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]
190204

191205
# try to analyze and if error then print the error
192206
try:
193-
# show analyzing message
194-
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}")
195210

196211
# get analysis results from analyze_file
197212
results = analyze_file(file, selected_stats=selected_stat_keys)
198213

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

204226
except Exception as e:
205-
print(f"[red]{messages['error']}[/] {e}")
227+
if json_output:
228+
import json
229+
print(json.dumps({"error": str(e)}))
230+
else:
231+
print(f"[red]{messages['error']}[/] {e}")
206232

207233

208234
def main():

0 commit comments

Comments
 (0)