Newer
Older
/*
This file is part of SLC.
Copyright (C) 2008 Sebastien LUTTRINGER (<contact@seblu.net> )
SLC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SLC; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 US
*/
#include "slc.hh"
#include "options.hh"
#include <getopt.h>
history_size = 100;
history_file = (string) getenv("HOME") + "/.slc_history";
/*!
** Print usage on @param out. The program name is @param name.
*/
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;
}
/*!
** Parse argline arguments and fill options accordingly
**
** @param argc argc of your program
** @param argv argv of your program
**
** @return
*/
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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;
}
Options &Options::loadenv() {
const char *senv;
// Get history path
if ((senv = getenv("SLC_HISTORY_FILE")) != 0 && *senv != 0)
history_file = senv;
// Get history size
if ((senv = getenv("SLC_HISTORY_SIZE")) != 0 && *senv != 0)
history_size = atoi(senv);
// Get log size
if ((senv = getenv("SLC_LOG_SIZE")) != 0 && *senv != 0)
log_size = atoi(senv);