def get_menu_option(option_text, delay = true)
      option_regex = /\s(\d+)\)\s*#{option_text}\s/i

      temporary_prompt MENU_PROMPT do
        # give the prompt a few seconds to draw.
        if delay
          wait_for_prompt(nil, 4, false)
        end

        # See if we have a menu already.
        menu_regex = /(?<MENU>\s0\)(?:.|\r|\n(?!\s0\)))*)#{MENU_PROMPT}[ \t]*$/
        match = menu_regex.match(output)
        menu = match ? match['MENU'] : nil

        discard_local_buffer do
          if menu.nil?
            # We want to redraw the menu.
            # In order to do that, we need to send a command that is not valid.
            # A blank line equates to a zero, which is (probably) the logout option.
            # So we'll send a -1 to redraw the menu without actually running any commands.
            debug 'Redrawing menu...'
            menu = exec('-1', command_timeout: 5, timeout_error: false)

            if last_exit_code == :timeout
              # If for some reason the shell is/was running, we need to exit it to return to the menu.
              # This time we will raise an error.
              menu = exec('exit', command_timeout: 5)
            end
          end

          # Ok, so now we have our menu options.
          debug "Locating 'XX) #{option_text}' menu option..."
          match = option_regex.match(menu)
          if match
            return match[1].to_i
          else
            return nil
          end
        end
      end
    end
def get_menu_option(option_text, delay = true)
      option_regex = /\s(\d+)\)\s*#{option_text}\s/i

      temporary_prompt MENU_PROMPT do
        # give the prompt a few seconds to draw.
        if delay
          wait_for_prompt(nil, 4, false)
        end

        # See if we have a menu already.
        menu_regex = /(?<MENU>\s0\)(?:.|\r|\n(?!\s0\)))*)#{MENU_PROMPT}[ \t]*$/
        match = menu_regex.match(output)
        menu = match ? match['MENU'] : nil

        discard_local_buffer do
          if menu.nil?
            # We want to redraw the menu.
            # In order to do that, we need to send a command that is not valid.
            # A blank line equates to a zero, which is (probably) the logout option.
            # So we'll send a -1 to redraw the menu without actually running any commands.
            debug 'Redrawing menu...'
            menu = exec('-1', command_timeout: 5, timeout_error: false)

            if last_exit_code == :timeout
              # If for some reason the shell is/was running, we need to exit it to return to the menu.
              # This time we will raise an error.
              menu = exec('exit', command_timeout: 5)
            end
          end

          # Ok, so now we have our menu options.
          debug "Locating 'XX) #{option_text}' menu option..."
          match = option_regex.match(menu)
          if match
            return match[1].to_i
          else
            return nil
          end
        end
      end
    end
Processes the pfSense console menu to determine the option to send.