CommandLine.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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
17typedef struct option RawLongOption;
18
21{
22public:
23 LongOption();
24 explicit LongOption(const RawLongOption &);
25 LongOption(const LongOption&);
28
29private:
30 void copy(const RawLongOption &);
31};
32
35{
36public:
38 CommandLine(int argc, char *argv[], const char *shortRules, const RawLongOption *longRules);
39 CommandLine(const CommandLine &them);
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
72private:
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
Manages arguments passed to a program (i.e., main(argc, argv) parameters).
Definition: CommandLine.h:35
std::vector< char * > argv_
raw main() parameters, including argv[0] and a nil argv[argc]
Definition: CommandLine.h:77
int argc() const
Definition: CommandLine.h:61
void Visitor(const int optId, const char *optValue)
Definition: CommandLine.h:52
bool hasOption(const int optId, const char **optValue=nullptr) const
Definition: CommandLine.cc:71
CommandLine & operator=(const CommandLine &)
Definition: CommandLine.cc:52
const RawLongOption * longOptions() const
Definition: CommandLine.h:73
void forEachOption(Visitor) const
calls Visitor for each of the configured command line option
Definition: CommandLine.cc:89
std::vector< LongOption > longOptions_
long –option rules
Definition: CommandLine.h:81
const char * shortOptions_
single-dash, single-letter (-x) option rules
Definition: CommandLine.h:80
void resetArg0(const char *programName)
replaces argv[0] with the new value
Definition: CommandLine.cc:115
CommandLine(int argc, char *argv[], const char *shortRules, const RawLongOption *longRules)
expects main() input plus getopt_long(3) grammar rules for parsing argv
Definition: CommandLine.cc:24
const char * arg0() const
Definition: CommandLine.h:58
void pushFrontOption(const char *name, const char *value=nullptr)
inserts a (possibly duplicated) option at the beginning of options (just after argv[0])
Definition: CommandLine.cc:123
bool nextOption(int &optId) const
Definition: CommandLine.cc:101
char ** argv() const
Definition: CommandLine.h:64
A struct option C++ wrapper, helps with option::name copying/freeing.
Definition: CommandLine.h:21
void copy(const RawLongOption &)
Definition: CommandLine.cc:161
LongOption & operator=(const LongOption &)
Definition: CommandLine.cc:153

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors