diff --git a/ChangeLog b/ChangeLog
index d124fa2..a362436 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2024-01-05  Dirk Baechle <dl9obn@darc.de>
+
+	* src/gengetopt.cc: fixed option checks when using flag options
+        in groups and provided a simple test for this case
+
 2019-05-30  Tomas Volf <wolf@wolfsden.cz>
 
 	* --unamed command line option renamed to --unnamed, old still
diff --git a/THANKS b/THANKS
index e005fb2..bcb1b3b 100644
--- a/THANKS
+++ b/THANKS
@@ -9,6 +9,7 @@ list of these people. Help me keep it complete and exempt of errors.
 Ben Asselstine
 Lorenzo Bettini
 Pierre Bacquet
+Dirk Baechle
 Max Bane
 Nelson H. F. Beebe
 N. Becker
diff --git a/configure.ac b/configure.ac
index 0ee89db..02190d8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -146,6 +146,7 @@ AC_CONFIG_TEST_SH([tests/test_conf_parser_save.sh])
 AC_CONFIG_TEST_SH([tests/test_default_values.sh])
 AC_CONFIG_TEST_SH([tests/test_dep.sh])
 AC_CONFIG_TEST_SH([tests/test_groups.sh])
+AC_CONFIG_TEST_SH([tests/test_groups_flags.sh])
 AC_CONFIG_TEST_SH([tests/test_manual_help.sh])
 AC_CONFIG_TEST_SH([tests/test_manual_help_args.sh])
 AC_CONFIG_TEST_SH([tests/test_modes.sh])
diff --git a/src/gengetopt.cc b/src/gengetopt.cc
index 4c49fdc..82b99f4 100644
--- a/src/gengetopt.cc
+++ b/src/gengetopt.cc
@@ -705,6 +705,25 @@ gengetopt_check_option (gengetopt_option *n, bool groupoption, bool modeoption)
   // reset the description
   gengetopt_set_text(0);
 
+  // now we have to check for flag options
+  if (n->type == ARG_FLAG)
+  {
+    if (n->flagstat < 0)
+      return SPECIFY_FLAG_STAT;
+
+    if (n->default_string || n->multiple || n->arg_is_optional
+        || n->type_str || n->acceptedvalues || n->required_set)
+      return NOT_VALID_SPECIFICATION;
+
+    n->required = 0;
+    n->required_set = true;
+  }
+  else
+  {
+    if (n->flagstat >= 0)
+      return NOT_VALID_SPECIFICATION;
+  }
+
   if (n->group_value != 0)
   {
       if (! groupoption)
@@ -745,25 +764,6 @@ gengetopt_check_option (gengetopt_option *n, bool groupoption, bool modeoption)
   if (n->group_value && n->mode_value)
       return FOUND_BUG;
 
-  // now we have to check for flag options
-  if (n->type == ARG_FLAG)
-  {
-    if (n->flagstat < 0)
-      return SPECIFY_FLAG_STAT;
-
-    if (n->default_string || n->multiple || n->arg_is_optional
-        || n->type_str || n->acceptedvalues || n->required_set)
-      return NOT_VALID_SPECIFICATION;
-
-    n->required = 0;
-    n->required_set = true;
-  }
-  else
-  {
-    if (n->flagstat >= 0)
-      return NOT_VALID_SPECIFICATION;
-  }
-
   // enum type can only be specified with options with values
   if (n->type == ARG_ENUM && !(n->acceptedvalues)) {
       return INVALID_ENUM_TYPE_USE;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 6e962b8..090e08b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -87,6 +87,7 @@ VALGRINDTESTS = \
 	test_default_values.sh \
 	test_dep.sh \
 	test_groups.sh \
+	test_groups_flags.sh \
 	test_groups_option_help \
 	test_manual_help.sh \
 	test_manual_help_args.sh \
@@ -137,6 +138,7 @@ check_PROGRAMS = \
 	test_conf_parser_ov3 \
 	test_conf_parser_ov4 \
 	test_groups \
+	test_groups_flags \
 	test_groups_option_help \
 	test_manual_help \
 	test_manual_help_args \
@@ -173,6 +175,7 @@ default_values_cmd.h: default_values_cmd.c
 cmdline.h: cmdline.c
 test_all_opts_cmd.h: test_all_opts_cmd.c
 test_groups_cmd.h: test_groups_cmd.c
+test_groups_flags_cmd.h: test_groups_flags_cmd.c
 test_modes_cmd.h: test_modes_cmd.c
 test_conf_parser_cmd.h: test_conf_parser_cmd.c
 test_multiple_cmd.h: test_multiple_cmd.c
@@ -235,6 +238,9 @@ no_prog_name_SOURCES = \
 test_groups_SOURCES = \
 	test_groups.c \
 	test_groups_cmd.ggo
+test_groups_flags_SOURCES = \
+	test_groups_flags.c \
+	test_groups_flags_cmd.ggo
 test_modes_SOURCES = \
 	test_modes.c \
 	test_modes_cmd.ggo
@@ -351,6 +357,8 @@ CHECKDIFFFILES = \
 	test_first_cmdline_cmd.h \
 	test_groups_cmd.c \
 	test_groups_cmd.h \
+	test_groups_flags_cmd.c \
+	test_groups_flags_cmd.h \
 	test_main_cmdline_cmd.c \
 	test_main_cmdline_cmd.h \
 	test_manual_help_args_cmd.c \
@@ -481,6 +489,7 @@ EXTRA_DIST = \
 	test_default_values.sh.in \
 	test_dep.sh.in \
 	test_groups.sh.in \
+	test_groups_flags.sh.in \
 	test_manual_help.sh.in \
 	test_manual_help_args.sh.in \
 	test_manual_help_args_cmd.ggo \
diff --git a/tests/test_groups_flags.c b/tests/test_groups_flags.c
new file mode 100644
index 0000000..9bc4556
--- /dev/null
+++ b/tests/test_groups_flags.c
@@ -0,0 +1,39 @@
+/* test_groups.c test */
+
+/* test all kinds of options */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "test_groups_flags_cmd.h"
+
+static struct gengetopt_args_info args_info;
+
+int
+main (int argc, char **argv)
+{  
+  unsigned int i;
+  if (test_groups_flags_cmd_parser (argc, argv, &args_info) != 0)
+    exit(1) ;
+
+  if (args_info.dumb_given)
+    {
+      printf("Flag dumb was given\n");
+      printf("dumb flag: %d\n", args_info.dumb_flag);
+    }
+  printf("dumb flag: %d\n", args_info.dumb_flag);
+
+  if (args_info.extended_given)
+    {
+      printf("Flag extended was given\n");
+    }
+  printf("extended flag: %d\n", args_info.extended_flag);
+
+  test_groups_flags_cmd_parser_free (&args_info);
+
+  return 0;
+}
diff --git a/tests/test_groups_flags.sh.in b/tests/test_groups_flags.sh.in
new file mode 100644
index 0000000..465a569
--- /dev/null
+++ b/tests/test_groups_flags.sh.in
@@ -0,0 +1,14 @@
+#! @SHELL@
+
+set -ex
+
+! "$@" ./test_groups_flags
+! "$@" ./test_groups_flags -a
+! "$@" ./test_groups_flags -d -e
+! "$@" ./test_groups_flags --dumb -e
+! "$@" ./test_groups_flags -d --extended
+! "$@" ./test_groups_flags --dumb -e -a
+"$@" ./test_groups_flags -d
+"$@" ./test_groups_flags -e
+"$@" ./test_groups_flags --dumb
+"$@" ./test_groups_flags --extended
diff --git a/tests/test_groups_flags_cmd.ggo b/tests/test_groups_flags_cmd.ggo
new file mode 100644
index 0000000..0cb0f9c
--- /dev/null
+++ b/tests/test_groups_flags_cmd.ggo
@@ -0,0 +1,6 @@
+package "TestGroupFlags"
+version "1.0"
+
+defgroup "TestFlags" yes
+groupoption "dumb" d "Enable dumb debugging output" group="TestFlags" flag off
+groupoption "extended" e "Enable extended debugging output" group="TestFlags" flag on
