File: getopt-test.cpp

package info (click to toggle)
exiv2 0.28.7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 109,216 kB
  • sloc: cpp: 77,667; python: 9,619; javascript: 237; makefile: 193; sh: 172; ansic: 51; sed: 16
file content (115 lines) | stat: -rw-r--r-- 3,364 bytes parent folder | download | duplicates (2)
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
// SPDX-License-Identifier: GPL-2.0-or-later

// 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"

#if __has_include(<unistd.h>)
#include <unistd.h>
#endif
#include <iostream>

#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 initiate 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) override {
    std::cout << "Params::option()"
              << " opt = " << opt << " optarg = " << optarg << " optopt = " << optopt << std::endl;
    return 0;
  }

  //! Handle non-option parameters.
  int nonoption(const std::string& argv) override {
    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);

  int n;

#if __has_include(<unistd.h>)
  std::cout << "standard getopt()" << std::endl;
  do {
    n = ::getopt(argc, argv, ::optstring);
    if (n >= 0) {
      auto N = static_cast<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) {
      auto N = static_cast<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 EXIT_SUCCESS;
}