diff --git a/include/configuration_parser.h b/include/configuration_parser.h index 3cd9394..59c7646 100644 --- a/include/configuration_parser.h +++ b/include/configuration_parser.h @@ -46,17 +46,23 @@ #include #include +enum class ArgResult { + Success, Help, Error +}; + class ConfigurationParser { public: + ConfigurationParser() = default; ~ConfigurationParser() = default; bool ParseEnvironmentVariables(); bool ParseConfigFile(const std::string& path); - bool ParseArgV(int argc, char* const argv[]); + ArgResult ParseArgV(int argc, char* const argv[]); inline bool isHeadless() { return _headless; } inline std::shared_ptr XmlHandle() { return _config; } inline std::string getInitScriptPath() { return _init_script_path; } inline std::string getModelName() { return _model_name; } + static void PrintHelpMessage(char *argv[]); private: TiXmlDocument _doc; diff --git a/src/configuration_parser.cpp b/src/configuration_parser.cpp index dfa66df..7a82a13 100644 --- a/src/configuration_parser.cpp +++ b/src/configuration_parser.cpp @@ -40,6 +40,8 @@ #include "configuration_parser.h" +#include + bool ConfigurationParser::ParseEnvironmentVariables() { if (const char* headless_char = std::getenv("HEADLESS")) { _headless = !std::strcmp(headless_char, "1"); @@ -47,25 +49,31 @@ bool ConfigurationParser::ParseEnvironmentVariables() { return true; } -bool ConfigurationParser::ParseArgV(int argc, char* const argv[]) { - // TODO: Parse HITL Variables - if (argc < 5) { - std::cout << "This is a JSBSim integration for PX4 SITL/HITL simulations" << std::endl; - std::cout << " Usage: " << argv[0] << " " << std::endl; - std::cout << " : Aircraft directory path which the definition is located e.g. " - "`models/Rascal`" - << std::endl; - std::cout << " : Aircraft file to use inside the e.g. Rascal110-JSBSim" - << std::endl; - std::cout << " : Simulation config file name under the `configs` directory e.g. rascal" << std::endl; - std::cout << " : Location / scene where the vehicle should be spawned in e.g. LSZH" << std::endl; - return false; - } +ArgResult ConfigurationParser::ParseArgV(int argc, char* const argv[]) { + static const struct option options[] = { + {"scene", required_argument, nullptr, 's'}, + }; - // TODO: Switch to getopt - _init_script_path = std::string(argv[4]); + int c; + while ((c = getopt_long(argc, argv, "s:h", options, nullptr)) >= 0) { + switch (c) { + case 'h': { + return ArgResult::Help; + break; + } + case 's': { + _init_script_path = std::string(optarg); + break; + } + case '?': + default: { + std::cout << "Unknown Options" << std::endl; + return ArgResult::Error; + } + } + } - return true; + return ArgResult::Success; } bool ConfigurationParser::ParseConfigFile(const std::string& path) { @@ -87,3 +95,9 @@ bool ConfigurationParser::ParseConfigFile(const std::string& path) { return true; } +void ConfigurationParser::PrintHelpMessage(char *argv[]) { + std::cout << argv[0] << " aircraft [options]\n\n" + << " aircraft Aircraft config file name e.g. rascal" + << " -h | --help Print available options\n" + << " -s | --scene Location / scene where the vehicle should be spawned in e.g. LSZH\n"; +} diff --git a/src/main.cpp b/src/main.cpp index 2e2aabe..d524de5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,14 +42,29 @@ #include "jsbsim_bridge.h" int main(int argc, char *argv[]) { - // Path to config file - std::string path = std::string(JSBSIM_ROOT_DIR) + "/configs/" + std::string(argv[3]) + ".xml"; // Parse Configurations ConfigurationParser config; + if (argc > 1) { + // Path to config file + std::string path = std::string(JSBSIM_ROOT_DIR) + "/configs/" + std::string(argv[1]) + ".xml"; + config.ParseConfigFile(path); + } + switch (config.ParseArgV(argc, argv)) { + case ArgResult::Success : { + break; + } + case ArgResult::Help : { + ConfigurationParser::PrintHelpMessage(argv); + return 0; + } + default: + case ArgResult::Error : { + ConfigurationParser::PrintHelpMessage(argv); + return 1; + } + } config.ParseEnvironmentVariables(); - config.ParseArgV(argc, argv); - config.ParseConfigFile(path); // Configure JSBSim JSBSim::FGFDMExec *fdmexec = new JSBSim::FGFDMExec();