CommandLine.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_COMMANDLINE_H
10 #define SQUID_COMMANDLINE_H
11 
12 #if HAVE_GETOPT_H
13 #include <getopt.h>
14 #endif
15 #include <vector>
16 
17 typedef struct option RawLongOption;
18 
20 class LongOption : public RawLongOption
21 {
22 public:
23  LongOption();
24  explicit LongOption(const RawLongOption &);
25  LongOption(const LongOption&);
27  ~LongOption();
28 
29 private:
30  void copy(const RawLongOption &);
31 };
32 
35 {
36 public:
38  CommandLine(int argc, char *argv[], const char *shortRules, const RawLongOption *longRules);
39  CommandLine(const CommandLine &them);
40  ~CommandLine();
41 
43 
48  bool hasOption(const int optId, const char **optValue = nullptr) const;
49 
52  typedef void Visitor(const int optId, const char *optValue);
53 
55  void forEachOption(Visitor) const;
56 
58  const char *arg0() const { return argv_[0]; }
59 
61  int argc() const { return static_cast<int>(argv_.size()) - 1; }
62 
64  char **argv() const { return const_cast<char**>(argv_.data()); }
65 
67  void resetArg0(const char *programName);
68 
70  void pushFrontOption(const char *name, const char *value = nullptr);
71 
72 private:
73  const RawLongOption *longOptions() const { return longOptions_.size() ? longOptions_.data() : nullptr; }
74  bool nextOption(int &optId) const;
75 
77  std::vector<char *> argv_;
78 
79  /* getopt_long() grammar rules */
80  const char *shortOptions_;
81  std::vector<LongOption> longOptions_;
82 };
83 
84 #endif /* SQUID_COMMANDLINE_H */
85 
std::vector< char * > argv_
raw main() parameters, including argv[0] and a nil argv[argc]
Definition: CommandLine.h:77
const RawLongOption * longOptions() const
Definition: CommandLine.h:73
int argc() const
Definition: CommandLine.h:61
const char * arg0() const
Definition: CommandLine.h:58
A struct option C++ wrapper, helps with option::name copying/freeing.
Definition: CommandLine.h:20
char ** argv() const
Definition: CommandLine.h:64
std::vector< LongOption > longOptions_
long –option rules
Definition: CommandLine.h:81
void Visitor(const int optId, const char *optValue)
Definition: CommandLine.h:52
LongOption & operator=(const LongOption &)
Definition: CommandLine.cc:153
const char * shortOptions_
single-dash, single-letter (-x) option rules
Definition: CommandLine.h:80
Manages arguments passed to a program (i.e., main(argc, argv) parameters).
Definition: CommandLine.h:34
void copy(const RawLongOption &)
Definition: CommandLine.cc:161

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors