Example #1
0
void AgentConfiguration::initialize(int aArgc, const char *aArgv[])
{
  MTConnectService::initialize(aArgc, aArgv);
  
  const char *configFile = "agent.cfg";

  OptionsList optionList;
  optionList.append(new Option(0, configFile, "The configuration file", "file", false));  
  optionList.parse(aArgc, (const char**) aArgv);
  
  mConfigFile = configFile;

  try
  {
    configureLogger();
    ifstream file(mConfigFile.c_str());
    loadConfig(file);
  }
  catch (std::exception & e)
  {
    sLogger << LFATAL << "Agent failed to load: " << e.what();
    cerr << "Agent failed to load: " << e.what() << std::endl;
    optionList.usage();
  }
}
using dataTypes::OptionsList;
using dataTypes::OptionEntry;

TEST_FUNCTION(10) {
  std::vector<std::string> args({"--test-argument-value-space", "argument value", "--test-argument-value-equals",
                                 "=sample", "some/file/path", "--test-boolean"});
  OptionsList opts;
  opts.addEntry(OptionEntry(dataTypes::OptionArgumentType::OptionArgumentFilename, 0, "test-argument-value-space",
                            "Test passing argument for the value with space", nullptr, "name"));
  opts.addEntry(OptionEntry(dataTypes::OptionArgumentType::OptionArgumentFilename, 0, "test-argument-value-equals",
                            "Test passing argument for the value with equals", nullptr, "name"));
  opts.addEntry(
    OptionEntry(dataTypes::OptionArgumentType::OptionArgumentLogical, 0, "test-boolean", "Test boolean value"));

  int retVal = 0;
  ensure("Unable to parse command line", opts.parse(args, retVal));
  ensure("incorrect test-boolean", opts.getEntry("test-boolean")->value.logicalValue);
  ensure_equals("incorrect test-argument-value-equals", opts.getEntry("test-argument-value-equals")->value.textValue,
                "=sample");
  ensure_equals("incorrect test-argument-value-space", opts.getEntry("test-argument-value-space")->value.textValue,
                "argument value");
  ensure("incorrect number of leftover values", opts.pathArgs.size() == 1);
  ensure_equals("incorrect leftover value", opts.pathArgs[0], "some/file/path");
}

TEST_FUNCTION(20) {
  std::vector<std::string> args({"--test-return-value", "--test-callback", "--test-argument-value-space",
                                 "argument value", "--test-argument-value-equals", "=sample", "some/file/path",
                                 "--test-boolean"});
  OptionsList opts;
  opts.addEntry(