summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/bash_shared/menu_system.sh119
-rwxr-xr-xapps/compiler/compiler.sh19
-rw-r--r--apps/installer/includes/modules-manager/modules.sh11
-rw-r--r--apps/installer/main.sh2
4 files changed, 96 insertions, 55 deletions
diff --git a/apps/bash_shared/menu_system.sh b/apps/bash_shared/menu_system.sh
index e7ae8b5646..7477ad9b42 100644
--- a/apps/bash_shared/menu_system.sh
+++ b/apps/bash_shared/menu_system.sh
@@ -102,13 +102,6 @@ function menu_direct_execute() {
local user_input="$1"
shift
- # Handle help requests directly
- if [[ "$user_input" == "--help" || "$user_input" == "help" || "$user_input" == "-h" ]]; then
- echo "Available commands:"
- printf '%s\n' "${_MENU_OPTIONS[@]}"
- return 0
- fi
-
# Disable numeric selection in direct mode
if [[ "$user_input" =~ ^[0-9]+$ ]]; then
echo "Invalid option. Numeric selection is not allowed when passing arguments."
@@ -118,11 +111,25 @@ function menu_direct_execute() {
# Find command and execute
local idx
- idx=$(menu_find_index "$user_input")
+ # try-catch
+ {
+ idx=$(menu_find_index "$user_input")
+ } ||
+ {
+ idx=-1
+ }
+
if [[ $idx -ge 0 ]]; then
"$callback" "${_MENU_KEYS[$idx]}" "$@"
return $?
else
+ # Handle help requests directly
+ if [[ "$user_input" == "--help" || "$user_input" == "help" || "$user_input" == "-h" ]]; then
+ echo "Available commands:"
+ printf '%s\n' "${_MENU_OPTIONS[@]}"
+ return 0
+ fi
+
echo "Invalid option. Use --help to see available commands." >&2
return 1
fi
@@ -138,20 +145,32 @@ function menu_interactive() {
menu_display "$title"
read -r -p "Please enter your choice: " REPLY
- # Handle help request
- if [[ "$REPLY" == "--help" || "$REPLY" == "help" || "$REPLY" == "h" ]]; then
- echo "Available commands:"
- printf '%s\n' "${_MENU_OPTIONS[@]}"
- echo ""
- continue
- fi
+ # Parse input to separate command from arguments
+ local input_parts=()
+ read -r -a input_parts <<< "$REPLY"
+ local user_command="${input_parts[0]}"
+ local user_args=("${input_parts[@]:1}")
# Find and execute command
local idx
- idx=$(menu_find_index "$REPLY")
+ idx=$(menu_find_index "$user_command")
if [[ $idx -ge 0 ]]; then
- "$callback" "${_MENU_KEYS[$idx]}"
+ # Pass the command key and any additional arguments
+ "$callback" "${_MENU_KEYS[$idx]}" "${user_args[@]}"
+ local exit_code=$?
+ # Exit loop if callback returns 0 (e.g., quit command)
+ if [[ $exit_code -eq 0 && "${_MENU_KEYS[$idx]}" == "quit" ]]; then
+ break
+ fi
else
+ # Handle help request
+ if [[ "$REPLY" == "--help" || "$REPLY" == "help" || "$REPLY" == "h" ]]; then
+ echo "Available commands:"
+ printf '%s\n' "${_MENU_OPTIONS[@]}"
+ echo ""
+ continue
+ fi
+
echo "Invalid option. Please try again or use 'help' for available commands." >&2
echo ""
fi
@@ -159,35 +178,65 @@ function menu_interactive() {
}
# Main menu runner function
-# Usage: menu_run "Menu Title" callback_function menu_item1 menu_item2 ... "$@"
+# Usage: menu_run "Menu Title" callback_function "$@"
+# The menu items array should be defined globally before calling this function
function menu_run() {
local title="$1"
local callback="$2"
shift 2
- # Extract menu items (all arguments until we find command line args)
+ # Define menu from globally available menu items array
+ # This expects the calling script to have set up the menu items
+
+ # Handle direct execution if arguments provided
+ if [[ $# -gt 0 ]]; then
+ menu_direct_execute "$callback" "$@"
+ return $?
+ fi
+
+ # Run interactive menu
+ menu_interactive "$callback" "$title"
+}
+
+# Alternative menu runner that accepts menu items directly
+# Usage: menu_run_with_items "Menu Title" callback_function -- "${menu_items_array[@]}" -- "$@"
+function menu_run_with_items() {
+ local title="$1"
+ local callback="$2"
+ shift 2
+
+ # Parse parameters: menu items are between first and second "--"
local menu_items=()
- local found_args=false
-
- # Separate menu items from command line arguments
- while [[ $# -gt 0 ]]; do
- if [[ "$1" =~ \| ]]; then
- # This looks like a menu item (contains pipe)
- menu_items+=("$1")
- shift
- else
- # This is a command line argument
- found_args=true
- break
- fi
+ local script_args=()
+
+ # Skip first "--"
+ if [[ "$1" == "--" ]]; then
+ shift
+ else
+ echo "Error: menu_run_with_items requires -- separator before menu items" >&2
+ return 1
+ fi
+
+ # Collect menu items until second "--"
+ while [[ $# -gt 0 && "$1" != "--" ]]; do
+ menu_items+=("$1")
+ shift
done
- # Define menu from collected items
+ # Skip second "--" if present
+ if [[ "$1" == "--" ]]; then
+ shift
+ fi
+
+ # Remaining args are script arguments
+ script_args=("$@")
+
+ # Define menu from provided array
menu_define "${menu_items[@]}"
# Handle direct execution if arguments provided
- if [[ $found_args == true ]]; then
- menu_direct_execute "$callback" "$@"
+ if [[ ${#script_args[@]} -gt 0 ]]; then
+ menu_direct_execute "$callback" "${script_args[@]}"
return $?
fi
diff --git a/apps/compiler/compiler.sh b/apps/compiler/compiler.sh
index aa845b6998..e27b35c2de 100755
--- a/apps/compiler/compiler.sh
+++ b/apps/compiler/compiler.sh
@@ -49,7 +49,7 @@ function handle_compiler_command() {
;;
"quit")
echo "Closing compiler menu..."
- exit 0
+ return 0
;;
*)
echo "Invalid option. Use --help to see available commands."
@@ -61,20 +61,5 @@ function handle_compiler_command() {
# Hook support (preserved from original)
runHooks "ON_AFTER_OPTIONS" # you can create your custom options
-# Legacy switch function (preserved for compatibility)
-function _switch() {
- local reply="$1"
- local opt="$2"
-
- case "$reply" in
- ""|"--help")
- menu_show_help
- ;;
- *)
- run_option "$reply" "$opt"
- ;;
- esac
-}
-
# Run the menu system
-menu_run "ACORE COMPILER" handle_compiler_command "${comp_menu_items[@]}" "$@"
+menu_run_with_items "ACORE COMPILER" handle_compiler_command -- "${comp_menu_items[@]}" -- "$@"
diff --git a/apps/installer/includes/modules-manager/modules.sh b/apps/installer/includes/modules-manager/modules.sh
index 7612329649..88cadf01e2 100644
--- a/apps/installer/includes/modules-manager/modules.sh
+++ b/apps/installer/includes/modules-manager/modules.sh
@@ -25,6 +25,7 @@
CURRENT_PATH=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd )
source "$CURRENT_PATH/../../../bash_shared/includes.sh"
+source "$CURRENT_PATH/../includes.sh"
source "$AC_PATH_APPS/bash_shared/menu_system.sh"
# Module management menu definition
@@ -65,7 +66,7 @@ function handle_module_command() {
;;
"quit")
echo "Exiting module manager..."
- exit 0
+ return 0
;;
*)
echo "Invalid option. Use 'help' to see available commands."
@@ -129,7 +130,7 @@ function inst_module_list() {
function inst_module() {
# If no arguments provided, start interactive menu
if [[ $# -eq 0 ]]; then
- menu_run "MODULE MANAGER" handle_module_command "${module_menu_items[@]}"
+ menu_run_with_items "MODULE MANAGER" handle_module_command -- "${module_menu_items[@]}" --
return $?
fi
@@ -727,6 +728,12 @@ function inst_module_install {
# Update one or more modules
function inst_module_update {
+ # Handle help request
+ if [[ "$1" == "--help" || "$1" == "-h" ]]; then
+ inst_module_help
+ return 0
+ fi
+
# Support multiple modules and the --all flag; prompt if none specified.
local args=("$@")
local use_all=false
diff --git a/apps/installer/main.sh b/apps/installer/main.sh
index 3bdd99b225..b1cba33e8a 100644
--- a/apps/installer/main.sh
+++ b/apps/installer/main.sh
@@ -104,4 +104,4 @@ function handle_menu_command() {
}
# Run the menu system
-menu_run "ACORE DASHBOARD" handle_menu_command "${menu_items[@]}" "$@"
+menu_run_with_items "ACORE DASHBOARD" handle_menu_command -- "${menu_items[@]}" -- "$@"