forked from Exiv2/exiv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetopt-test.cpp
155 lines (137 loc) · 4.32 KB
/
getopt-test.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
// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2021 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
// getopt-test.cpp
// Sample program to test getopt()
// Command:
// $ getopt-test -v pr -P EIXxgklnycsvth file1 file2
//
// Output:
// standard getopt()
// 118 = v optind = 2 opterr = 1 optopt = 118 optarg = unknown
// -1 optind = 2 opterr = 1 optopt = 118 optarg = unknown
//
// homemade getopt()
// 118 = v optind = 2 opterr = 1 optopt = 118 optarg = unknown
// -1 optind = 2 opterr = 1 optopt = 118 optarg = unknown
//
// Params::option() opt = 118 optarg = optopt = 118
// Params::nonoption() pr
// Params::nonoption() -P
// Params::nonoption() EIXxgklnycsvth
// Params::nonoption() file1
// Params::nonoption() file2
// Params::getopt() rc = 0
#include <exiv2/exiv2.hpp>
// getopt.{cpp|hpp} is not part of libexiv2
#include "getopt.hpp"
#ifdef EXV_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <iostream>
#include <iomanip>
#include <cassert>
#define Safe(x) (x?x:"unknown")
const char* optstring = ":hVvqfbuktTFa:Y:O:D:r:p:P:d:e:i:c:m:M:l:S:g:K:n:Q:";
// *****************************************************************************
// class Params
class Params : public Util::Getopt {
public:
/*!
@brief Call Getopt::getopt() with optstring, to inititate command line
argument parsing, perform consistency checks after all command line
arguments are parsed.
@param argc Argument count as passed to main() on program invocation.
@param argv Argument array as passed to main() on program invocation.
@return 0 if successful, >0 in case of errors.
*/
int getopt(int argc, char** const argv)
{
int rc = Util::Getopt::getopt(argc, argv, ::optstring);
std::cout << "Params::getopt()"
<< " rc = " << rc
<< std::endl;
return rc ;
}
//! Handle options and their arguments.
int option(int opt, const std::string& optarg, int optopt)
{
std::cout << "Params::option()"
<< " opt = " << opt
<< " optarg = " << optarg
<< " optopt = " << optopt
<< std::endl;
return 0;
}
//! Handle non-option parameters.
int nonoption(const std::string& argv)
{
std::cout << "Params::nonoption()"
<< " " << argv
<< std::endl;
return 0 ;
}
}; // class Params
int main(int argc, char** const argv)
{
Exiv2::XmpParser::initialize();
::atexit(Exiv2::XmpParser::terminate);
#ifdef EXV_ENABLE_BMFF
Exiv2::enableBMFF();
#endif
int n;
#ifdef EXV_HAVE_UNISTD_H
std::cout << "standard getopt()" << std::endl;
do {
n = ::getopt(argc,argv,::optstring);
if ( n >= 0 ) {
char N = (char) n;
std::cout << n << " = " << N ;
} else {
std::cout << n ;
}
std::cout << " optind = " << ::optind
<< " opterr = " << ::opterr
<< " optopt = " << ::optopt
<< " optarg = " << Safe(::optarg)
<< std::endl;
} while ( n >= 0 );
std::cout << std::endl;
#endif
std::cout << "homemade getopt()" << std::endl;
do {
n = Util::getopt(argc,argv,::optstring);
if ( n >= 0 ) {
char N = (char) n;
std::cout << n << " = " << N ;
} else {
std::cout << n ;
}
std::cout << " optind = " << Util::optind
<< " opterr = " << Util::opterr
<< " optopt = " << Util::optopt
<< " optarg = " << Safe(Util::optarg)
<< std::endl;
} while ( n >= 0 );
std::cout << std::endl;
// Handle command line arguments
Params params;
params.getopt(argc, argv);
return 0;
}