-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.cpp
155 lines (135 loc) · 3.84 KB
/
sample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// getoptsample.cpp --- C++ getopt_long sample
// Copyright (C) 2018 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>.
// This file is public domain software.
#include <cstdio> // standard C I/O
#include <string> // for std::string
#include <vector> // for std::vector
#ifdef USE_GETOPT_PORT
#include "getopt.h" // for portable getopt_long
#else
#include <getopt.h> // for GNU getopt_long
#endif
using std::printf;
using std::fprintf;
using std::exit;
// global variables
std::string g_input_file;
std::string g_output_file;
std::vector<std::string> g_non_options;
// return value
enum RET
{
RET_SUCCESS = 0,
RET_INVALID_ARGUMENT
};
// show version info
void show_version(void)
{
printf("getoptsample version 0.4\n");
}
// show help
void show_help(void)
{
printf("getoptsample --- sample codes for GNU getopt_long\n");
printf("Usage: sample [options]...\n");
printf("\n");
printf("Options:\n");
printf("--help Show this help\n");
printf("--version Show version info\n");
printf("-i file\n");
printf("--input-file=file An input file\n");
printf("-o file\n");
printf("--output-file=file An output file\n");
}
// option info for getopt_long
struct option opts[] =
{
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 0 },
{ "input-file", required_argument, NULL, 'i' },
{ "output-file", required_argument, NULL, 'o' },
{ NULL, 0, NULL, 0 },
};
// external symbols for getopt_long
extern "C"
{
extern char *optarg;
extern int optind, opterr, optopt;
}
// parse the command line
int parse_command_line(int argc, char **argv)
{
int opt, opt_index;
std::string arg;
opterr = 0; /* NOTE: opterr == 1 is not compatible to getopt_port */
if (argc <= 1)
{
fprintf(stderr, "ERROR: no arguments. try with --help.\n");
return RET_INVALID_ARGUMENT;
}
while ((opt = getopt_long(argc, argv, "hi:o:", opts, &opt_index)) != -1)
{
switch (opt)
{
case 0:
arg = opts[opt_index].name;
if (arg == "version")
{
show_version();
exit(EXIT_SUCCESS);
}
break;
case 'h':
show_help();
exit(EXIT_SUCCESS);
break;
case 'i':
g_input_file = optarg;
break;
case 'o':
g_output_file = optarg;
break;
case '?':
default:
switch (optopt)
{
case 'i':
case 'o':
fprintf(stderr, "ERROR: option '-%c' requires a parameter.\n", optopt);
break;
case 0:
fprintf(stderr, "ERROR: invalid option.\n");
break;
default:
fprintf(stderr, "ERROR: invalid option '-%c'.\n", optopt);
break;
}
return RET_INVALID_ARGUMENT;
}
}
for (int i = optind; i < argc; ++i)
{
g_non_options.push_back(argv[i]);
}
return RET_SUCCESS;
}
// do something
int do_something(void)
{
printf("####################\n");
printf("### do_something ###\n");
printf("####################\n");
printf("input-file: %s\n", g_input_file.c_str());
printf("output-file: %s\n", g_output_file.c_str());
printf("non-option arguments:\n");
for (size_t i = 0; i < g_non_options.size(); ++i)
printf("g_non_options[%d]: %s\n", (int)i, g_non_options[i].c_str());
return EXIT_SUCCESS;
}
// the main function
int main(int argc, char **argv)
{
if (int ret = parse_command_line(argc, argv))
return EXIT_FAILURE;
return do_something();
}