Просмотр исходного кода

Use Args to parse command line

Daniele Bartolini 13 лет назад
Родитель
Сommit
c096b41f66
2 измененных файлов с 100 добавлено и 55 удалено
  1. 99 55
      src/Device.cpp
  2. 1 0
      src/Device.h

+ 99 - 55
src/Device.cpp

@@ -32,6 +32,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Renderer.h"
 #include "Types.h"
 #include "String.h"
+#include "Args.h"
 #include <cstdlib>
 
 namespace crown
@@ -184,77 +185,120 @@ void Device::Frame()
 //-----------------------------------------------------------------------------
 bool Device::ParseCommandLine(int argc, char** argv)
 {
-	if (argc == 2 && string::strcmp(argv[1], "-help") == 0)
-	{
-		os::printf("Usage: %s [options]\n", argv[0]);
-		os::printf("Options:\n\n");
-		os::printf("All of the following options take precedence over\nenvironment variables and configuration files.\n\n");
-		// Print32_t options
-		os::printf("  -help\t\t\t\t\tShow this help\n");
-		os::printf("  -root-path <path>\t\t\tUse <path> as the filesystem root path\n");
-		os::printf("  -user-path <path>\t\t\tUse <path> as the filesystem user path\n");
-		os::printf("  -metrics <width> <height>\t\tSet the <width> and <height> of the render window\n");
-		os::printf("  -fullscreen\t\t\t\tStart in fullscreen\n");
+	int32_t fullscreen = 0;
 
-		return false;
-	}
-
-	// Parse command line arguments
-	int32_t i = 1;
-	while (i < argc)
+	ArgsOption options[] = 
+	{
+		"help",       AOA_NO_ARGUMENT,       NULL,        'i',
+		"root-path",  AOA_REQUIRED_ARGUMENT, NULL,        'r',
+		"user-path",  AOA_REQUIRED_ARGUMENT, NULL,        'u',
+		"width",      AOA_REQUIRED_ARGUMENT, NULL,        'w',
+		"height",     AOA_REQUIRED_ARGUMENT, NULL,        'h',
+		"fullscreen", AOA_NO_ARGUMENT,       &fullscreen,  1,
+		NULL, 0, NULL, 0
+	};
+
+	Args args(argc, argv, "", options);
+
+	while (1)
 	{
-		if (string::strcmp(argv[i], "-root-path") == 0)
+		int32_t ret = args.next_option();
+
+		switch (ret)
 		{
-			if (argc < i + 2)
+			case -1:
 			{
-				os::printf("%s: error: missing absolute path after `-root-path`\n", argv[0]);
-				return false;
+				return true;
 			}
-			string::strcpy(mPreferredRootPath, argv[i + 1]);
-			// Two arguments crunched
-			i += 2;
-			continue;
-		}
-		if (string::strcmp(argv[i], "-user-path") == 0)
-		{
-			if (argc < i + 2)
+			case 0:
 			{
-				os::printf("%s: error: missing absolute path after `-user-path`\n", argv[0]);
-				return false;
+				mPreferredWindowFullscreen = fullscreen;
+
+				break;
 			}
-			string::strcpy(mPreferredUserPath, argv[i + 1]);
-			// Two arguments crunched
-			i += 2;
-			continue;
-		}
-		if (string::strcmp(argv[i], "-metrics") == 0)
-		{
-			if (argc < i + 3)
+			// Help
+			case 'i':
 			{
-				os::printf("%s: error: wrong number of arguments after `-metrics`\n", argv[0]);
+				PrintHelpMessage();
 				return false;
 			}
-			mPreferredWindowWidth = atoi(argv[i + 1]);
-			mPreferredWindowHeight = atoi(argv[i + 2]);
-			// Three arguments crunched
-			i += 3;
-			continue;
-		}
-		if (string::strcmp(argv[i], "-fullscreen") == 0)
-		{
-			mPreferredWindowFullscreen = true;
-			// One argument crunched
-			i++;
-			continue;
-		}
+			// Root path
+			case 'r':
+			{
+				if (args.option_argument() == NULL)
+				{
+					os::printf("%s: error: missing absolute path after `-root-path`\n", argv[0]);
+					return false;
+				}
 
-		// Next argument
-		i++;
+				string::strcpy(mPreferredRootPath, args.option_argument());
+
+				break;
+			}
+			// User path
+			case 'u':
+			{
+				if (args.option_argument() == NULL)
+				{
+					os::printf("%s: error: missing absolute path after `--user-path`\n", argv[0]);
+					return false;
+				}
+
+				string::strcpy(mPreferredUserPath, args.option_argument());
+
+				break;
+			}
+			// Window width
+			case 'w':
+			{
+				if (args.option_argument() == NULL)
+				{
+					os::printf("%s: error: missing width value after `--width`\n", argv[0]);
+					return false;
+				}
+
+				mPreferredWindowWidth = atoi(args.option_argument());
+				break;
+			}
+			// Window height
+			case 'h':
+			{
+				if (args.option_argument() == NULL)
+				{
+					os::printf("%s: error: missing height value after `--height`\n", argv[0]);
+					return false;
+				}
+
+				mPreferredWindowHeight = atoi(args.option_argument());
+				break;
+			}
+			default:
+			{
+				break;
+			}
+		}
 	}
 
 	return true;
 }
 
+//-----------------------------------------------------------------------------
+void Device::PrintHelpMessage()
+{
+	os::printf("Usage: crown [options]\n");
+	os::printf("Options:\n\n");
+
+	os::printf("All of the following options take precedence over\n");
+	os::printf("environment variables and configuration files.\n\n");
+
+	os::printf("  --help                Show this help.\n");
+	os::printf("  --root-path <path>    Use <path> as the filesystem root path.\n");
+	os::printf("  --user-path <path>    Use <path> as the filesystem user path.\n");
+	os::printf("  --width <width>       Set the <width> of the render window.\n");
+	os::printf("  --height <width>      Set the <height> of the render window.\n");
+	os::printf("  --fullscreen          Start in fullscreen.\n");
+}
+
 Device device;
 Device* GetDevice()
 {

+ 1 - 0
src/Device.h

@@ -59,6 +59,7 @@ public:
 private:
 
 	bool					ParseCommandLine(int argc, char** argv);
+	void					PrintHelpMessage();
 
 	static const uint16_t	CROWN_MAJOR;
 	static const uint16_t	CROWN_MINOR;