diff --git a/src/include/getopt_long.h b/src/include/getopt_long.h
index e23c21f..71a884c 100644
--- a/src/include/getopt_long.h
+++ b/src/include/getopt_long.h
@@ -23,6 +23,7 @@ struct option
 
 #define no_argument 0
 #define required_argument 1
+#define optional_argument 2
 #endif
 
 #ifndef HAVE_GETOPT_LONG
diff --git a/src/port/getopt_long.c b/src/port/getopt_long.c
index b099091..029a359 100644
--- a/src/port/getopt_long.c
+++ b/src/port/getopt_long.c
@@ -100,7 +100,8 @@ getopt_long(int argc, char *const argv[],
 				if (strlen(longopts[i].name) == namelen
 					&& strncmp(place, longopts[i].name, namelen) == 0)
 				{
-					if (longopts[i].has_arg)
+					if (longopts[i].has_arg == required_argument ||
+						longopts[i].has_arg == optional_argument)
 					{
 						if (place[namelen] == '=')
 							optarg = place + namelen + 1;
@@ -113,13 +114,19 @@ getopt_long(int argc, char *const argv[],
 						{
 							if (optstring[0] == ':')
 								return BADARG;
-							if (opterr)
+
+							if (opterr &&
+								longopts[i].has_arg == required_argument)
 								fprintf(stderr,
 								   "%s: option requires an argument -- %s\n",
 										argv[0], place);
+
 							place = EMSG;
 							optind++;
-							return BADCH;
+							if (longopts[i].has_arg == required_argument)
+								return BADCH;
+
+							optarg = NULL;
 						}
 					}
 					else
