P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
options.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef LIB_OPTIONS_H_
18#define LIB_OPTIONS_H_
19
20#include <functional>
21#include <ostream>
22#include <stdexcept>
23#include <string>
24#include <vector>
25
26#include "cstring.h"
27#include "error.h"
28#include "map.h"
29
30namespace Util {
31
32// Command-line options processing
33class Options {
34 public:
38
40 Hide = 1 << 0,
41
48 };
49
50 // return true if processing is successful
51 typedef std::function<bool(const char *optarg)> OptionProcessor;
52
53 protected:
54 struct Option {
55 cstring option;
56 const char *argName; // nullptr if argument is not required
57 const char *description;
58 OptionProcessor processor;
59 OptionFlags flags;
60 };
61 const char *binaryName;
62 cstring message;
63 // Build date and compile command required in couple runtime files
64 cstring compileCommand;
65 cstring buildDate;
66 std::ostream *outStream = &std::cerr;
67
68 std::map<cstring, const Option *> options;
69 std::vector<cstring> optionOrder;
70 std::vector<const char *> additionalUsage;
71 std::vector<const char *> remainingOptions; // produced as output
72 // if true unknown options are collected in remainingOptions
73 bool collectUnknownOptions = false;
74
75 void setOutStream(std::ostream *out) { outStream = out; }
76 void registerUsage(const char *msg) { additionalUsage.push_back(msg); }
77 void registerOption(const char *option, // option to register, e.g., -c or --version
78 const char *argName, // name of option argument;
79 // nullptr if no argument expected
80 OptionProcessor processor, // function to execute when option matches
81 const char *description, // option help message
82 OptionFlags flags = OptionFlags::Default); // additional flags
83
84 explicit Options(cstring message) : binaryName(nullptr), message(message), compileCommand("") {}
85
86 public:
95 virtual std::vector<const char *> *process(int argc, char *const argv[]);
96
97 virtual const char *getIncludePath() = 0;
98 cstring getCompileCommand() { return compileCommand; }
99 cstring getBuildDate() { return buildDate; }
100 cstring getBinaryName() { return cstring(binaryName); }
101 virtual void usage();
102};
103
104} // namespace Util
105
106#endif /* LIB_OPTIONS_H_ */
Definition options.h:33
virtual std::vector< const char * > * process(int argc, char *const argv[])
Definition options.cpp:43
OptionFlags
Definition options.h:35
@ Default
The default option flags.
Definition options.h:37
@ OptionalArgument
Definition options.h:47
@ Hide
Hide this option from –help message.
Definition options.h:40
Definition options.h:54
Definition cstring.h:72