From 1ab12960cb8d3476e9663d536fb705bb4d28e1c7 Mon Sep 17 00:00:00 2001
From: Matthias Ringwald <matthias@ringwald.ch>
Date: Thu, 5 Dec 2024 15:05:12 +0100
Subject: [PATCH] port/posix-h4: allow multi stage argv parsing

---
 port/posix-h4/main.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/port/posix-h4/main.c b/port/posix-h4/main.c
index 191f86e40..17f19b95f 100644
--- a/port/posix-h4/main.c
+++ b/port/posix-h4/main.c
@@ -287,7 +287,7 @@ static void local_version_information_handler(uint8_t * packet){
     }
 }
 
-static char short_options[] = "hu:l:rb:";
+static char short_options[] = "+hu:l:rb:";
 
 static struct option long_options[] = {
         {"help",        no_argument,        NULL,   'h'},
@@ -329,7 +329,8 @@ int main(int argc, const char * argv[]){
 
     // set default device path
     config.device_name = "/dev/tty.usbmodemEF437DF524C51";
-
+    int oldopterr = opterr;
+    opterr = 0;
     // parse command line parameters
     while(true){
         int c = getopt_long( argc, (char* const *)argv, short_options, long_options, NULL );
@@ -337,7 +338,7 @@ int main(int argc, const char * argv[]){
             break;
         }
         if (c == '?'){
-            break;
+            continue;
         }
         switch (c) {
             case 'u':
@@ -355,9 +356,13 @@ int main(int argc, const char * argv[]){
             case 'h':
             default:
                 usage(argv[0]);
-                return EXIT_FAILURE;
+                break;
         }
     }
+    // reset getopt parsing, so it works as intended from btstack_main
+    optind = 1;
+    opterr = oldopterr;
+
     /// GET STARTED with BTstack ///
 	btstack_memory_init();
     btstack_run_loop_init(btstack_run_loop_posix_get_instance());