Skip to content
options.cc 1.37 KiB
Newer Older
Seblu's avatar
Seblu committed
#include "slc.hh"
#include "options.hh"

#include <getopt.h>

Options::Options() {
  port = 18136;
}

void Options::usage(const char *name, ostream &out) const {
  out << "usage: " << name
      << " [-h|--help] [-l|--login name] [-v|--verbose] server [port]" << std::endl;
  out << "-h | --help        : Print this help." << std::endl;
  out << "-l | --login name  : Define login to name." << std::endl;
}

Options &Options::load(int argc, char *argv[]) {
  struct option lopt[] = {
    {"help", 0, 0, 'h'},
    {"login", 0, 0, 'l'},
    {0, 0, 0, 0},
  };
  int option;

  while ((option = getopt_long(argc, argv, "hl:", lopt, 0)) != -1) {
    switch (option) {
    case 'h':
      usage(*argv, std::cout);
      break;
    case 'l':
      printf("login %s\n", optarg);
      login = optarg;
      break;
    case '?':
      usage(*argv, std::cerr);
      exit(ERR_USAGE);
    }
  }

  if (argv[optind] == 0) {
    usage(*argv, std::cerr);
    exit(ERR_USAGE);
  }

  server = argv[optind++];

  if (argv[optind] != 0) {
    port = atoi(argv[optind++]);
  }

  if (argv[optind] != 0) {
    usage(*argv, std::cerr);
    exit(ERR_USAGE);
  }

  return *this;
}

ostream &operator<< (ostream &s, const Options &o) {
  return s << "server: " << o.server << std::endl
	   << "port: " << o.port << std::endl
	   << "host login: " << o.login << std::endl
	   << "host pass: " << o.pass << std::endl;
}