1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-28 18:03:36 +00:00

main: basic option processing

This commit is contained in:
Mirek Kratochvil 2013-01-11 11:33:17 +01:00
parent 36cace8763
commit 59a0e24c67

@ -16,8 +16,82 @@
* along with Codecrypt. If not, see <http://www.gnu.org/licenses/>.
*/
int main()
#include <iostream>
/*
* main module. Parse options, fire up stuff, pass commands to it.
*/
#define out(x) std::cout << x << std::endl
#define outeol std::cout << std::endl
#define err(x) std::cerr << x << std::endl
#define erreol std::cerr << std::endl
void print_version()
{
out ("codecrypt " PACKAGE_VERSION);
}
void print_help (char*pname)
{
print_version();
outeol;
out ("Usage: " << pname << " [options]");
outeol;
out ("Options consist of:");
out (" -h, --help display this help");
out (" -V,--version display version information");
}
#include <getopt.h>
int main (int argc, char**argv)
{
bool do_help = false;
bool do_version = false;
bool has_opt = false;
//process options
int c, option_index;
for (;;) {
static struct option long_opts[] = {
{"help", 0, 0, 'h' },
{"version", 0, 0, 'V' },
{0, 0, 0, 0 }
};
c = getopt_long (argc, argv, "hV", long_opts, &option_index);
if (c == -1) break;
has_opt = true;
switch (c) {
case '?':
case ':':
case 'h':
do_help = true;
break;
case 'V':
do_version = true;
break;
default: //which doesn't just happen.
break;
}
}
if (optind != argc) {
err (argv[0] << ": unmatched non-option parameters");
do_help = true;
}
if ( (!has_opt) || do_help) {
print_help (argv[0]);
return 0;
}
if (do_version) {
print_version();
return 0;
}
return 0;
}