<commit_before>#include <boost/program_options.hpp>
#include <qi/application.hpp>

#include "qicli.hpp"

namespace po = boost::program_options;

std::map<std::string, SubCmd> subCmdMap;

void init()
{
  subCmdMap["info"] = &subCmd_info;
  subCmdMap["call"] = &subCmd_call;
  subCmdMap["post"] = &subCmd_post;
  subCmdMap["watch"] = &subCmd_watch;
  subCmdMap["get"] = &subCmd_get;
  subCmdMap["set"] = &subCmd_set;
  subCmdMap["trace"] = &subCmd_trace;
  subCmdMap["top"] = &subCmd_top;
}

int                 main(int argc, char **argv)
{
  qi::Application app(argc, argv);
  int             subCmdArgc = 0;
  char            **subCmdArgv = 0;
  SubCmd          subCmd = 0;

  init();
  for (int i = 0; i < argc; ++i)
  {
    std::map<std::string, SubCmd>::const_iterator it = subCmdMap.find(argv[i]);
    if (it != subCmdMap.end())
    {
      subCmdArgc = argc - i;
      subCmdArgv = &argv[i];
      subCmd = it->second;
      argc = i + 1;
      break;
    }
  }

  po::options_description desc("Usage: qicli [OPTIONS] SUBCMD [-h] [OPTIONS] [ARGS]");

  MainOptions options;

  desc.add_options()
      ("address,a", po::value<std::string>(&options.address)->default_value("tcp://127.0.0.1:9559"), "The address of the service directory")
      ("command,c", "sub command to execute")
      ("help,h", "Print this help message and exit");

  po::positional_options_description positionalOptions;
  positionalOptions.add("command", 1);
  po::variables_map vm;

  try {
    po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
    po::notify(vm);
  } catch (po::error &e) {
    std::cerr << e.what() << std::endl;
    return false;
  }

  if (vm.count("help") || subCmdArgc == 0)
  {
    showHelp(desc);
    return 0;
  }

  // Accept and fill missing protocol and/or port from address
  qi::Url url(options.address, "tcp", 9559);
  if (url.isValid())
    options.address = url.str();

  int ret;
  try {
    ret = subCmd(subCmdArgc, subCmdArgv, options);
  } catch (const std::exception& e)
  {
    printError(e.what());
    return 1;
  } catch (...) {
    printError("unknown error catched");
    return 1;
  }
  return ret;
}
<commit_msg>qicli: modify command line parsing semantic<commit_after>#include <boost/program_options.hpp>
#include <qi/application.hpp>

#include "qicli.hpp"

namespace po = boost::program_options;

std::map<std::string, SubCmd> subCmdMap;

void init()
{
  subCmdMap["info"] = &subCmd_info;
  subCmdMap["call"] = &subCmd_call;
  subCmdMap["post"] = &subCmd_post;
  subCmdMap["watch"] = &subCmd_watch;
  subCmdMap["get"] = &subCmd_get;
  subCmdMap["set"] = &subCmd_set;
  subCmdMap["trace"] = &subCmd_trace;
  subCmdMap["top"] = &subCmd_top;
}

int                 main(int argc, char **argv)
{
  qi::Application app(argc, argv);
  int             subCmdArgc = 0;
  char            **subCmdArgv = 0;
  SubCmd          subCmd = 0;

  init();
  for (int i = 0; i < argc; ++i)
  {
    std::map<std::string, SubCmd>::const_iterator it = subCmdMap.find(argv[i]);
    if (it != subCmdMap.end())
    {
      subCmdArgc = argc - i;
      subCmdArgv = &argv[i];
      subCmd = it->second;
      argc = i + 1;
      break;
    }
  }
  po::options_description desc("Usage: qicli [OPTIONS] SUBCMD [-h] [OPTIONS] [ARGS]");

  MainOptions options;

  desc.add_options()
      ("address,a", po::value<std::string>(&options.address)->default_value("tcp://127.0.0.1:9559"), "The address of the service directory")
      ("help,h", "Print this help message and exit");

  po::positional_options_description positionalOptions;
  positionalOptions.add("", 1);
  po::variables_map vm;

  try {
    po::store(po::command_line_parser(argc, argv).options(desc).positional(positionalOptions).run(), vm);
    po::notify(vm);
  } catch (po::error &e) {
    std::cerr << e.what() << std::endl;
    return false;
  }

  if (vm.count("help") || subCmdArgc == 0)
  {
    showHelp(desc);
    return 0;
  }

  // Accept and fill missing protocol and/or port from address
  qi::Url url(options.address, "tcp", 9559);
  if (url.isValid())
    options.address = url.str();

  int ret;
  try {
    ret = subCmd(subCmdArgc, subCmdArgv, options);
  } catch (const std::exception& e)
  {
    printError(e.what());
    return 1;
  } catch (...) {
    printError("unknown error catched");
    return 1;
  }
  return ret;
}

{'fraction_non_alphanumeric': 0.1695150115473441, 'fraction_numerical': 0.011085450346420323, 'mean_word_length': 3.3266733266733266, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2531498', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1557, 'n_words': 338}