Support counter in args parsing #28

Merged
dyfet merged 1 commits from :args-counter into master 1 year ago
  1. 6
      CHANGELOG.md
  2. 2
      CMakeLists.txt
  3. 35
      server/args.hpp
  4. 2
      server/logger.cpp
  5. 14
      server/main.cpp

6
CHANGELOG.md

@ -1,15 +1,13 @@
# Release Notes
## v0.0.7
## v0.0.6
- Migration to gitea
- Docker migration to quay
- Osip2 vendoring updated to 5.2.1
## v0.0.6
- Use release for osip\_free
- Improve linting
- System init support
- Added Dockerfile to produce coventry containers
- Replace debug -x option with multiple -v
## v0.0.5
- Added docgen target

2
CMakeLists.txt

@ -9,7 +9,7 @@
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
cmake_minimum_required(VERSION 3.1)
project(Coventry VERSION 0.0.7 LANGUAGES CXX)
project(Coventry VERSION 0.0.6 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)

35
server/args.hpp

@ -98,24 +98,24 @@ private:
class args final {
public:
using value_t = std::variant<bool, const char *>;
using value_t = std::variant<bool, const char *, unsigned>;
using text_t = std::initializer_list<std::string_view>;
using char_p = const char *;
struct option {
friend class args;
explicit option(char_p help) noexcept :
value_(false), code_(0), usage_(nullptr), id_(nullptr), help_(help), next_(nullptr) {
value_(false), code_(0), counter_(false), usage_(nullptr), id_(nullptr), help_(help), next_(nullptr) {
add_option();
}
option(char ch, char_p id, char_p help) noexcept :
value_(false), code_(ch), usage_(nullptr), id_(id), help_(help), next_(nullptr) {
option(char ch, char_p id, char_p help, bool counter = false) noexcept :
value_(false), code_(ch), counter_(counter), usage_(nullptr), id_(id), help_(help), next_(nullptr) {
add_option();
}
option(char ch, char_p id, char_p help, char_p usage) noexcept :
value_(false), code_(ch), usage_(usage), id_(id), help_(help), next_(nullptr) {
value_(false), code_(ch), counter_(false), usage_(usage), id_(id), help_(help), next_(nullptr) {
add_option();
}
@ -127,13 +127,17 @@ public:
}
auto operator *() const {
return value_.index() < 1 ? nullptr : std::get<const char *>(value_);
return (value_.index() != 1) ? nullptr : std::get<const char *>(value_);
}
auto operator!() const {
return value_.index() < 1 && !std::get<bool>(value_);
}
auto count() const {
return (value_.index() < 2) ? ((value_.index() > 0 || std::get<bool>(value_)) ? 1U : 0U) : std::get<unsigned>(value_);
}
auto value(long default_value = 0) const {
return value_.index() > 0 ? bad_arg::str_value(std::get<const char *>(value_)) : default_value;
}
@ -156,6 +160,7 @@ public:
private:
value_t value_;
char code_;
bool counter_;
char_p usage_, id_, help_;
option *next_;
@ -224,6 +229,15 @@ public:
continue;
}
if(op->counter_) {
if(op->value_.index() < 1)
op->value_ = 1U;
else
op->value_ = op->count() + 1U;
break;
}
if(op->value_.index() > 0 || std::get<bool>(op->value_))
throw bad_arg("already used", keyword);
@ -264,6 +278,15 @@ public:
if(!op)
throw bad_arg("unknown option", arg[pos]);
if(op->counter_) {
if(op->value_.index() < 1)
op->value_ = 1U;
else
op->value_ = op->count() + 1U;
++pos;
continue;
}
if(op->value_.index() > 0 || std::get<bool>(op->value_))
throw bad_arg("already used", arg[pos]);

2
server/logger.cpp

@ -57,7 +57,7 @@ logger::info::~info()
logger::debug::~debug()
{
if(current_ <= level()) {
syslog(LOG_DEBUG, "%s", str().c_str());
// syslog(LOG_DEBUG, "%s", str().c_str());
if(errlog) {
std::lock_guard<std::mutex> lock(synclock);
std::cerr << "debug: " << str() << std::endl;

14
server/main.cpp

@ -55,12 +55,7 @@ args::option opt_ipv6('6', "--ipv6", "enable ipv6");
args::option opt_config('c', "--config", "config file path");
args::option helpflag('h', "--help", "display this list");
args::option opt_prefix('p', "--prefix", "working directory", "path");
args::option verbose('v', "--verbose", "display verbose output");
#ifdef DEBUG
args::option debug('x', "--debug", "show debug output", "level (1-9)");
#else
args::option debug(0, nullptr, nullptr);
#endif
args::option verbose('v', "--verbose", "display verbose output", true);
args::option althelp('?', nullptr, nullptr);
auto reload()
@ -225,11 +220,8 @@ auto main(int argc, const char **argv) -> int
umask(077); // FlawFinder: root access for root user
// setup logging
if(is(debug)) {
logger::debug::level(static_cast<unsigned>(debug.value()));
logger::init(SERVER_ID, LOG_DEBUG, true);
}
else if(is(verbose))
logger::debug::level(verbose.count() - 1);
if(is(verbose))
logger::init(SERVER_ID, LOG_INFO, true);
else
logger::init(SERVER_ID, LOG_NOTICE, false);

Loading…
Cancel
Save