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 protected:
55 struct Option {
56 cstring option;
57 const char *argName; // nullptr if argument is not required
58 const char *description;
59 OptionProcessor processor;
60 OptionFlags flags;
61 };
62 const char *binaryName;
63 cstring message;
64 // Build date and compile command required in couple runtime files
65 cstring compileCommand;
66 cstring buildDate;
67 std::ostream *outStream = &std::cerr;
68
69 std::map<cstring, const Option *> options;
70 std::vector<cstring> optionOrder;
71 std::vector<const char *> additionalUsage;
72 std::vector<const char *> remainingOptions; // produced as output
73 // if true unknown options are collected in remainingOptions
74 bool collectUnknownOptions = false;
75
76 void setOutStream(std::ostream *out) { outStream = out; }
77 void registerUsage(const char *msg) { additionalUsage.push_back(msg); }
78 void registerOption(const char *option, // option to register, e.g., -c or --version
79 const char *argName, // name of option argument;
80 // nullptr if no argument expected
81 OptionProcessor processor, // function to execute when option matches
82 const char *description, // option help message
83 OptionFlags flags = OptionFlags::Default); // additional flags
84
85 explicit Options(std::string_view message)
86 : binaryName(nullptr), message(message), compileCommand("") {}
87
90 [[nodiscard]] virtual bool validateOptions() const;
91
92 public:
101 virtual std::vector<const char *> *process(int argc, char *const argv[]);
102
103 [[nodiscard]] virtual const char *getIncludePath() const = 0;
104 cstring getCompileCommand() { return compileCommand; }
105 cstring getBuildDate() { return buildDate; }
106 cstring getBinaryName() { return cstring(binaryName); }
107 virtual void usage();
108};
109
110} // namespace P4::Util
111
112#endif /* LIB_OPTIONS_H_ */
Definition lib/options.h:34
virtual bool validateOptions() const
Definition lib/options.cpp:167
virtual std::vector< const char * > * process(int argc, char *const argv[])
Definition lib/options.cpp:45
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:55
Definition cstring.h:85