Apache Mesos
flag.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_FLAGS_FLAG_HPP__
14 #define __STOUT_FLAGS_FLAG_HPP__
15 
16 #include <ostream>
17 #include <string>
18 #include <vector>
19 
20 #include <stout/error.hpp>
21 #include <stout/lambda.hpp>
22 #include <stout/nothing.hpp>
23 #include <stout/try.hpp>
24 
25 namespace flags {
26 
27 // Forward declaration.
28 class FlagsBase;
29 
30 
31 struct Name
32 {
33  Name()
34  : deprecated(false) {}
35 
36  Name(const std::string& _value)
37  : value(_value), deprecated(false) {}
38 
39  Name(const char* _value)
40  : value(_value), deprecated(false) {}
41 
42  bool operator==(const Name& other) const
43  {
44  return value == other.value;
45  }
46 
47  std::string value;
48  bool deprecated;
49 };
50 
51 
52 inline Name DeprecatedName(const std::string& name)
53 {
54  Name name_(name);
55  name_.deprecated = true;
56  return name_;
57 }
58 
59 
60 // NOTE: Move this to `warning.hpp` if this can be used elsewhere.
61 struct Warning
62 {
63 public:
64  explicit Warning(const std::string& _message) : message(_message) {}
65 
66  const std::string message;
67 };
68 
69 
70 // Convenience wrapper.
71 struct Warnings
72 {
73  std::vector<Warning> warnings;
74 };
75 
76 
77 struct Flag
78 {
81 
82  // This is the name that the user uses to specifically load the flag (e.g, via
83  // command line `--foo=val`). This is optional because a flag might not be
84  // explicitly loaded by the user (e.g., flag with a default value). Note that
85  // this name should be one of `name` or `alias`.
87 
88  std::string help;
89  bool boolean;
90  lambda::function<Try<Nothing>(FlagsBase*, const std::string&)> load;
91  lambda::function<Option<std::string>(const FlagsBase&)> stringify;
92  lambda::function<Option<Error>(const FlagsBase&)> validate;
93  bool required;
94 
95  // This is the name of the flag that the user loads. If the loading is
96  // implicit this defaults to the `name`.
97  const Name& effective_name() const
98  {
99  return loaded_name.isSome() ? loaded_name.get() : name;
100  }
101 };
102 
103 
104 // Allows to hide the contents of a file located at `path` when serializing
105 // a flag. This is important for files containing sensitive information.
107 {
109  std::string value;
110 };
111 
112 
113 inline std::ostream& operator<<(
114  std::ostream &stream,
115  const SecurePathOrValue& flag)
116 {
117  if (flag.path.isSome()) {
118  stream << flag.path.get();
119  } else {
120  stream << flag.value;
121  }
122  return stream;
123 }
124 
125 } // namespace flags {
126 
127 #endif // __STOUT_FLAGS_FLAG_HPP__
bool boolean
Definition: flag.hpp:89
Option< Error > validate(const std::string &imageDir)
Definition: option.hpp:29
std::vector< Warning > warnings
Definition: flag.hpp:73
const std::string message
Definition: flag.hpp:66
bool deprecated
Definition: flag.hpp:48
std::string value
Definition: flag.hpp:109
Option< Path > path
Definition: flag.hpp:108
bool isSome() const
Definition: option.hpp:116
Definition: flag.hpp:61
Name()
Definition: flag.hpp:33
Name(const char *_value)
Definition: flag.hpp:39
std::string help
Definition: flag.hpp:88
Definition: flag.hpp:77
Definition: flags.hpp:44
Warning(const std::string &_message)
Definition: flag.hpp:64
const T & get() const &
Definition: option.hpp:119
Option< Name > alias
Definition: flag.hpp:80
Name(const std::string &_value)
Definition: flag.hpp:36
Name name
Definition: flag.hpp:79
bool required
Definition: flag.hpp:93
std::string value
Definition: flag.hpp:47
Name DeprecatedName(const std::string &name)
Definition: flag.hpp:52
Definition: flag.hpp:71
std::ostream & operator<<(std::ostream &stream, const SecurePathOrValue &flag)
Definition: flag.hpp:113
Definition: flag.hpp:106
bool operator==(const Name &other) const
Definition: flag.hpp:42
Option< Name > loaded_name
Definition: flag.hpp:86
Definition: flag.hpp:31
std::string stringify(int flags)
const Name & effective_name() const
Definition: flag.hpp:97
Definition: parse.hpp:33
constexpr const char * name
Definition: shell.hpp:41