Daniele Bartolini 10 anni fa
parent
commit
6c46ac4d0f
2 ha cambiato i file con 102 aggiunte e 38 eliminazioni
  1. 71 15
      src/device_options.cpp
  2. 31 23
      src/device_options.h

+ 71 - 15
src/device_options.cpp

@@ -5,6 +5,8 @@
 
 #include "device_options.h"
 #include "command_line.h"
+#include "log.h"
+#include "path.h"
 
 namespace crown
 {
@@ -13,10 +15,10 @@ static void help(const char* msg = NULL)
 {
 	if (msg)
 	{
-		printf("Error: %s\n", msg);
+		CE_LOGE("Error: %s\n", msg);
 	}
 
-	printf(
+	CE_LOGI(
 		"Usage: crown [options]\n"
 		"Options:\n\n"
 
@@ -43,7 +45,9 @@ static void help(const char* msg = NULL)
 }
 
 DeviceOptions::DeviceOptions(int argc, char** argv)
-	: _source_dir(NULL)
+	: _argc(argc)
+	, _argv(argv)
+	, _source_dir(NULL)
 	, _bundle_dir(NULL)
 	, _project(NULL)
 	, _platform(NULL)
@@ -57,24 +61,76 @@ DeviceOptions::DeviceOptions(int argc, char** argv)
 	, _window_width(CROWN_DEFAULT_WINDOW_WIDTH)
 	, _window_height(CROWN_DEFAULT_WINDOW_HEIGHT)
 {
-	CommandLine cmd(argc, argv);
+}
+
+int DeviceOptions::parse()
+{
+	CommandLine cl(_argc, _argv);
 
-	_source_dir = cmd.get_parameter("source-dir");
-	_bundle_dir = cmd.get_parameter("bundle-dir");
-	_project = cmd.get_parameter("project");
-	_platform = cmd.get_parameter("platform");
+	if (cl.has_argument("help", 'h'))
+	{
+		help();
+		return EXIT_FAILURE;
+	}
 
-	_wait_console = cmd.has_argument("wait-console");
-	_do_compile = cmd.has_argument("compile");
-	_do_continue = cmd.has_argument("continue");
+	if (cl.has_argument("version", 'v'))
+	{
+		CE_LOGI(CROWN_VERSION_STRING);
+		return EXIT_FAILURE;
+	}
 
-	const char* parent = cmd.get_parameter("parent-window");
-	if (parent)
+	_source_dir = cl.get_parameter("source-dir");
+	if (_source_dir == NULL)
+	{
+		help("Source dir must be specified.");
+		return EXIT_FAILURE;
+	}
+
+	_bundle_dir = cl.get_parameter("bundle-dir");
+	if (_bundle_dir == NULL)
+	{
+		help("Bundle dir must be specified.");
+		return EXIT_FAILURE;
+	}
+
+	if (!path::is_absolute(_source_dir))
+	{
+		help("Source dir must be absolute");
+		return EXIT_FAILURE;
+	}
+
+	if (!path::is_absolute(_bundle_dir))
+	{
+		help("Bundle dir must be absolute");
+		return EXIT_FAILURE;
+	}
+
+	_do_compile = cl.has_argument("compile");
+	_platform = cl.get_parameter("platform");
+	if (_do_compile != NULL && _platform == NULL)
+	{
+		help("Platform must be specified.");
+		return EXIT_FAILURE;
+	}
+
+	_do_continue = cl.has_argument("continue");
+
+	_project = cl.get_parameter("project");
+	_wait_console = cl.has_argument("wait-console");
+
+	const char* parent = cl.get_parameter("parent-window");
+	if (parent != NULL)
+	{
 		_parent_window = parse_uint(parent);
+	}
 
-	const char* port = cmd.get_parameter("console-port");
-	if (port)
+	const char* port = cl.get_parameter("console-port");
+	if (port != NULL)
+	{
 		_console_port = parse_uint(port);
+	}
+
+	return EXIT_SUCCESS;
 }
 
 }

+ 31 - 23
src/device_options.h

@@ -16,10 +16,39 @@
 namespace crown
 {
 
-struct DeviceOptions
+/// Holds device options.
+///
+/// @ingroup Device
+class DeviceOptions
 {
+	int _argc;
+	char** _argv;
+	const char* _source_dir;
+	const char* _bundle_dir;
+	const char* _project;
+	const char* _platform;
+	bool _wait_console;
+	bool _do_compile;
+	bool _do_continue;
+	uint32_t _parent_window;
+	uint16_t _console_port;
+	uint16_t _window_x;
+	uint16_t _window_y;
+	uint16_t _window_width;
+	uint16_t _window_height;
+
+#if CROWN_PLATFORM_ANDROID
+	AAssetManager* _asset_manager;
+#endif // CROWN_PLATFORM_ANDROID
+
+public:
+
 	DeviceOptions(int argc, char** argv);
 
+	/// Parses the command line and returns
+	/// EXIT_SUCCESS if no error is found.
+	int parse();
+
 	const char* source_dir() const { return _source_dir; }
 	const char* bundle_dir() const { return _bundle_dir; }
 	const char* project() const { return _project; }
@@ -35,30 +64,9 @@ struct DeviceOptions
 	uint16_t window_height() const { return _window_height; }
 
 #if CROWN_PLATFORM_ANDROID
+	void set_asset_manager(AAssetManager* am) { _asset_manager = am; }
 	const AAssetManager* asset_manager() const { return _asset_manager; }
 #endif // CROWN_PLATFORM_ANDROID
-
-private:
-
-	const char* _source_dir;
-	const char* _bundle_dir;
-	const char* _project;
-	const char* _platform;
-	bool _wait_console;
-	bool _do_compile;
-	bool _do_continue;
-	uint32_t _parent_window;
-	uint16_t _console_port;
-	uint16_t _window_x;
-	uint16_t _window_y;
-	uint16_t _window_width;
-	uint16_t _window_height;
-
-public:
-
-#if CROWN_PLATFORM_ANDROID
-	AAssetManager* _asset_manager;
-#endif // CROWN_PLATFORM_ANDROID
 };
 
 } // namespace crown