P4C
The P4 Compiler
Loading...
Searching...
No Matches
lib/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 <iostream>
22#include <ostream>
23#include <stdexcept>
24#include <string>
25#include <vector>
26
27#include "cstring.h"
28#include "error.h"
29#include "map.h"
30
31namespace P4::Util {
32
33// Command-line options processing
34class Options {
35 public:
39
41 Hide = 1 << 0,
42
49 };
50
51 // return true if processing is successful
52 using OptionProcessor = std::function<bool(const char *)>;
53
54 private:
55 void shortUsage();
56
57 protected:
58 struct Option {
59 cstring option;
60 const char *argName; // nullptr if argument is not required
61 const char *description;
62 OptionProcessor processor;
63 OptionFlags flags;
64 };
65 const char *binaryName;
66 cstring message;
67 // Build date and compile command required in couple runtime files
68 cstring compileCommand;
69 cstring buildDate;
70 std::ostream *outStream = &std::cerr;
71
72 std::map<cstring, const Option *> options;
73 std::vector<cstring> optionOrder;
74 std::vector<const char *> additionalUsage;
75 std::vector<const char *> remainingOptions; // produced as output
76 // if true unknown options are collected in remainingOptions
77 bool collectUnknownOptions = false;
78
79 void setOutStream(std::ostream *out) { outStream = out; }
80 void registerUsage(const char *msg) { additionalUsage.push_back(msg); }
81 void registerOption(const char *option, // option to register, e.g., -c or --version
82 const char *argName, // name of option argument;
83 // nullptr if no argument expected
84 OptionProcessor processor, // function to execute when option matches
85 const char *description, // option help message
86 OptionFlags flags = OptionFlags::Default); // additional flags
87
88 explicit Options(std::string_view message)
89 : binaryName(nullptr), message(message), compileCommand("") {}
90 virtual ~Options() = default;
91
94 [[nodiscard]] virtual bool validateOptions() const;
95
96 public:
107 virtual std::vector<const char *> *process(int argc, char *const argv[]);
108
118 virtual std::vector<const char *> *process_options(int argc, char *const argv[]);
119
120 [[nodiscard]] virtual const char *getIncludePath() const = 0;
121 cstring getCompileCommand() { return compileCommand; }
122 cstring getBuildDate() { return buildDate; }
123 cstring getBinaryName() { return cstring(binaryName); }
124 virtual void usage();
125};
126
127} // namespace P4::Util
128
129#endif /* LIB_OPTIONS_H_ */
virtual std::vector< const char * > * process_options(int argc, char *const argv[])
Definition lib/options.cpp:68
virtual bool validateOptions() const
Definition lib/options.cpp:175
virtual std::vector< const char * > * process(int argc, char *const argv[])
Definition lib/options.cpp:49
OptionFlags
Definition lib/options.h:36
@ Default
The default option flags.
Definition lib/options.h:38
@ Hide
Hide this option from –help message.
Definition lib/options.h:41
@ OptionalArgument
Definition lib/options.h:48
Definition lib/options.h:58
Definition cstring.h:85