re-write CLI argument parsing for testability
This commit is contained in:
1 parent
8cb8d4bf69
commit
5ab17ec375
7 files changed
+552
-39
No files matched your search
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright - See the COPYRIGHT that is included with this distribution.
|
||||
* pvxs is distributed subject to a Software License Agreement found
|
||||
* in file LICENSE that is included with this distribution.
|
||||
*/
|
||||
|
||||
#ifndef CLIUTIL_H
|
||||
#define CLIUTIL_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <map> // for std::pair
|
||||
|
||||
#include <utilpvt.h>
|
||||
|
||||
namespace pvxs {
|
||||
|
||||
struct ArgVal {
|
||||
std::string value;
|
||||
bool defined = false;
|
||||
|
||||
ArgVal() = default;
|
||||
ArgVal(std::nullptr_t) {}
|
||||
ArgVal(const std::string& value) :value(value), defined(true) {}
|
||||
ArgVal(const char* value) :value(value), defined(true) {}
|
||||
|
||||
inline explicit
|
||||
operator bool() const { return defined; }
|
||||
|
||||
inline
|
||||
const std::string& operator*() const {
|
||||
if(defined)
|
||||
return value;
|
||||
throw std::logic_error("Undefined argument value");
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
inline
|
||||
V as() const {
|
||||
return parseTo<V>(**this);
|
||||
}
|
||||
};
|
||||
|
||||
bool operator==(const ArgVal& rhs, const ArgVal& lhs);
|
||||
|
||||
struct GetOpt {
|
||||
GetOpt(int argc, char *argv[], const char *spec);
|
||||
|
||||
const char *argv0;
|
||||
std::vector<std::string> positional;
|
||||
std::vector<std::pair<char, ArgVal>> arguments;
|
||||
bool success = false;
|
||||
};
|
||||
|
||||
} // namespace pvxs
|
||||
|
||||
#endif // CLIUTIL_H
|
||||
Reference in new issue
Block a user