diff --git a/cli/main.py b/cli/main.py index 204ab27..dd986c4 100644 --- a/cli/main.py +++ b/cli/main.py @@ -142,8 +142,13 @@ def version(): print(f"[red]{messages.get('error', 'Error:')}[/] {e}") +# SPICE ANALYZE COMMAND @app.command() -def analyze(file: str, all: bool = typer.Option(False, "--all", help="Analyze all stats without selection menu")): +def analyze( + file: str, + all: bool = typer.Option(False, "--all", help="Analyze all stats without selection menu"), + json_output: bool = typer.Option(False, "--json", help="Output results in JSON format") +): """ Analyze the given file. """ @@ -169,41 +174,59 @@ def analyze(file: str, all: bool = typer.Option(False, "--all", help="Analyze al if all: selected_stat_keys = available_stats else: - # print checkbox menu to select which stats to show - selected_stats = inquirer.checkbox( - message=messages.get("select_stats", "Select stats to display:"), - choices=[stats_labels[stat] for stat in available_stats], - pointer="> ", - default=[stats_labels[stat] for stat in available_stats], # All selected by default - instruction=messages.get("checkbox_hint", "(Use space to select, enter to confirm)") - ).execute() - - # if no stats were selected - if not selected_stats: - print(messages.get("no_stats_selected", "No stats selected. Analysis cancelled.")) - return - - # create a mapping from displayed labels back to stat keys - reverse_mapping = {v: k for k, v in stats_labels.items()} - - # convert selected labels back to stat keys - selected_stat_keys = [reverse_mapping[label] for label in selected_stats] + # Don't show interactive menu in JSON mode (assumes all stats) + if json_output: + selected_stat_keys = available_stats + else: + # print checkbox menu to select which stats to show + selected_stats = inquirer.checkbox( + message=messages.get("select_stats", "Select stats to display:"), + choices=[stats_labels[stat] for stat in available_stats], + pointer="> ", + default=[stats_labels[stat] for stat in available_stats], # All selected by default + instruction=messages.get("checkbox_hint", "(Use space to select, enter to confirm)") + ).execute() + + # if no stats were selected + if not selected_stats: + if json_output: + import json + print(json.dumps({"error": messages.get("no_stats_selected", "No stats selected. Analysis cancelled.")})) + else: + print(messages.get("no_stats_selected", "No stats selected. Analysis cancelled.")) + return + + # create a mapping from displayed labels back to stat keys + reverse_mapping = {v: k for k, v in stats_labels.items()} + + # convert selected labels back to stat keys + selected_stat_keys = [reverse_mapping[label] for label in selected_stats] # try to analyze and if error then print the error try: - # show analyzing message - print(f"{messages['analyzing_file']}: {file}") + # show analyzing message if not in JSON mode + if not json_output: + print(f"{messages['analyzing_file']}: {file}") # get analysis results from analyze_file results = analyze_file(file, selected_stats=selected_stat_keys) - # only print the selected stats - for stat in selected_stat_keys: - if stat in results: - print(messages[stat].format(count=results[stat])) + # output in JSON format if flag + if json_output: + import json + print(json.dumps(results, indent=2)) + else: + # only print the selected stats in normal mode + for stat in selected_stat_keys: + if stat in results: + print(messages[stat].format(count=results[stat])) except Exception as e: - print(f"[red]{messages['error']}[/] {e}") + if json_output: + import json + print(json.dumps({"error": str(e)})) + else: + print(f"[red]{messages['error']}[/] {e}") def main():