Skip to content

Commit d9f5a6e

Browse files
committed
add is_false() helper function and logical operator overloads to ArgumentValue and ArgumentValueList
1 parent 8446ca0 commit d9f5a6e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

argparse.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ ArgumentValue ArgumentValue::operator--(int){
6262
// Accessors {{{2
6363
bool ArgumentValue::is_none() const{return *this == NONE;}
6464
bool ArgumentValue::is_true() const{return *this == TRUE;}
65+
bool ArgumentValue::is_false() const{return *this != TRUE;}
6566

6667
// === ARGUMENT VALUE LIST === {{{1
6768
//
@@ -131,6 +132,12 @@ ArgumentValueList ArgumentValueList::operator--(int){
131132
return temp;
132133
}
133134

135+
// Stream Operators {{{2
136+
std::ostream& operator<<(std::ostream& os, const ArgumentValueList& arglist){
137+
os << arglist.str();
138+
return os;
139+
}
140+
134141
// Modifiers {{{2
135142
void ArgumentValueList::str(const std::string& val){at(0) = val;}
136143

@@ -139,6 +146,7 @@ std::string ArgumentValueList::str() const{return at(0);}
139146
const char* ArgumentValueList::c_str() const{return at(0).c_str();}
140147
bool ArgumentValueList::is_none() const{return at(0) == NONE;}
141148
bool ArgumentValueList::is_true() const{return at(0) == TRUE;}
149+
bool ArgumentValueList::is_false() const{return at(0) != TRUE;}
142150
std::vector<std::string> ArgumentValueList::vec() const{
143151
std::vector<std::string> result;
144152
for (auto& val : *this) result.push_back((std::string)val);

argparse.h

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef ARGPARSE_H
22
#define ARGPARSE_H
3-
#define ARGPARSE_VERSION 2.2.5
3+
#define ARGPARSE_VERSION 2.2.6
44
#include <iostream>
55
#include <sstream>
66
#include <fstream>
@@ -57,6 +57,19 @@ namespace argparse{
5757
operator float() const;
5858
operator double() const;
5959
operator bool() const;
60+
61+
// Logical Operators
62+
bool operator!() const{
63+
return !(bool)*this;
64+
}
65+
template <typename T>
66+
bool operator&&(const T& other) const{
67+
return (bool)*this && other;
68+
}
69+
template <typename T>
70+
bool operator||(const T& other) const{
71+
return (bool)*this || other;
72+
}
6073

6174
// Comparison Operators
6275
template <typename T>
@@ -127,6 +140,7 @@ namespace argparse{
127140
// Accessors
128141
bool is_none() const;
129142
bool is_true() const;
143+
bool is_false() const;
130144
};
131145

132146

@@ -162,6 +176,19 @@ namespace argparse{
162176
operator bool() const;
163177
operator std::string() const;
164178

179+
// Logical Operators
180+
bool operator!() const{
181+
return !(bool)at(0);
182+
}
183+
template <typename T>
184+
bool operator&&(const T& other) const{
185+
return (bool)at(0) && other;
186+
}
187+
template <typename T>
188+
bool operator||(const T& other) const{
189+
return (bool)at(0) || other;
190+
}
191+
165192
// Comparison Operators
166193
template <typename T>
167194
bool operator==(const T& other) const{
@@ -243,12 +270,9 @@ namespace argparse{
243270
const char* c_str() const;
244271
bool is_none() const;
245272
bool is_true() const;
273+
bool is_false() const;
246274
std::vector<std::string> vec() const;
247275
};
248-
std::ostream& operator<<(std::ostream& os, const ArgumentValueList& arglist){
249-
os << arglist.str();
250-
return os;
251-
}
252276
typedef std::map<std::string, ArgumentValueList> ArgumentMap;
253277
std::string format_args(ArgumentMap args);
254278
void print_args(ArgumentMap args, std::ostream& out=std::cout);

0 commit comments

Comments
 (0)