commit 3316f7b8c285980ab7a83d398bba7caf0664f632
parent dc2a74089da196b2e6255ddad12e45a5198e9717
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Tue, 11 Jun 2024 12:00:24 +0200
Remove exceptions, silently skip invalid options
Diffstat:
3 files changed, 12 insertions(+), 13 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(
args
- VERSION 0.0.4
+ VERSION 0.0.5
DESCRIPTION "Command Line Argument Parser"
LANGUAGES CXX C
)
diff --git a/include/args.hpp b/include/args.hpp
@@ -12,14 +12,14 @@ namespace args {
using option_t = args_option_t;
using argp_t = args_argp_t;
-int parse(const argp_t *argp, int argc, char *argv[], void *input);
+int parse(const argp_t *argp, int argc, char *argv[], void *input) noexcept;
class Parser {
public:
void *input() const { return m_input; }
private:
- friend int parse(const argp_t *argp, int argc, char *argv[], void *input);
+ friend int parse(const argp_t *, int, char **, void *) noexcept;
Parser(const argp_t *argp, void *input);
diff --git a/src/args.cpp b/src/args.cpp
@@ -2,14 +2,13 @@
#include <algorithm>
#include <cstring>
-#include <exception>
#include <format>
#include <iostream>
#include <sstream>
namespace args {
-int parse(const argp_t *argp, int argc, char *argv[], void *input) {
+int parse(const argp_t *argp, int argc, char *argv[], void *input) noexcept {
Parser parser(argp, input);
return parser.parse(argc, argv, &parser);
}
@@ -30,13 +29,13 @@ Parser::Parser(const argp_t *argp, void *input) : argp(argp), m_input(input) {
if (!opt.key) {
if ((opt.flags & ALIAS) == 0) {
- std::cerr << "non alias without a key\n";
- throw new std::runtime_error("no key");
+ // non alias without a key, silently ignoring
+ continue;
}
if (!key_last) {
- std::cerr << "no option to alias\n";
- throw new std::runtime_error("no alias");
+ // nothing to alias, silently ignoring
+ continue;
}
trie.insert(opt.name, key_last);
@@ -47,8 +46,8 @@ Parser::Parser(const argp_t *argp, void *input) : argp(argp), m_input(input) {
help_entries.back().push(opt.name);
} else {
if (options.count(opt.key)) {
- std::cerr << std::format("duplicate key {}\n", opt.key);
- throw new std::runtime_error("duplicate key");
+ // duplicate key, silently ignoring
+ continue;
}
if (opt.name) trie.insert(opt.name, opt.key);
@@ -68,8 +67,8 @@ Parser::Parser(const argp_t *argp, void *input) : argp(argp), m_input(input) {
}
} else {
if (!key_last) {
- std::cerr << "no option to alias\n";
- throw new std::runtime_error("no alias");
+ // nothing to alias, silently ignoring
+ continue;
}
if (hidden) continue;