From b69b1ded20c115e196f8feea384749df9f5eed52 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 6 Feb 2025 19:52:08 +0100 Subject: [PATCH 1/2] Change values of Handle fields --- include/argparse.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index bfa31506..3eb56637 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -50,10 +50,10 @@ namespace argparse enum class Handle { - errors_and_help, - errors, - help, - none + none = 0, + errors = 1, + help = 2, + errors_and_help = errors | help }; class parsing_error From 53c9e0121338b9156778fda52ea17779f65e4548 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 6 Feb 2025 21:33:59 +0100 Subject: [PATCH 2/2] Simplify Handle comparison --- include/argparse.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index 3eb56637..d4076e60 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -63,6 +63,11 @@ namespace argparse using runtime_error::runtime_error; }; + inline auto operator&(Handle lhs, Handle rhs) -> int + { + return static_cast(lhs) & static_cast(rhs); + } + inline auto from_string(std::string const & s, auto & t) -> bool { auto iss = std::istringstream(s); @@ -170,7 +175,7 @@ namespace argparse } catch (HelpRequested const &) { - if (m_handle == Handle::errors_and_help || m_handle == Handle::help) + if (m_handle & Handle::help) { std::cout << format_help() << std::endl; std::exit(EXIT_SUCCESS); @@ -180,7 +185,7 @@ namespace argparse } catch (VersionRequested const &) { - if (m_handle == Handle::errors_and_help || m_handle == Handle::help) + if (m_handle & Handle::help) { std::cout << format_version() << std::endl; std::exit(EXIT_SUCCESS); @@ -190,7 +195,7 @@ namespace argparse } catch (parsing_error const & e) { - if (m_handle == Handle::errors_and_help || m_handle == Handle::errors) + if (m_handle & Handle::errors) { std::cout << e.what() << '\n'; std::cout << format_help() << std::endl;