advgetopt 2.0.49
Parse complex command line arguments and configuration files in C++.
option_info_ref.cpp
Go to the documentation of this file.
1// Copyright (c) 2006-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/advgetopt
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
28// self
29//
31
32#include "advgetopt/exception.h"
35
36
37// cppthread
38//
39#include <cppthread/log.h>
40
41
42// libutf8
43//
44#include <libutf8/libutf8.h>
45
46
47// last include
48//
49#include <snapdev/poison.h>
50
51
52
53namespace advgetopt
54{
55
56
57
76
77
89{
90 if(f_opt->is_defined())
91 {
92 return f_opt->get_value().empty();
93 }
94
95 return true;
96}
97
98
111{
112 if(f_opt->is_defined())
113 {
114 return f_opt->get_value().length();
115 }
116
117 return f_opt->get_default().length();
118}
119
120
129{
130 return length();
131}
132
133
148{
149 if(f_opt->is_defined())
150 {
151 return f_opt->get_long();
152 }
153
154 if(!f_opt->has_default())
155 {
156 return 0;
157 }
158
159 std::int64_t v;
160 if(!validator_integer::convert_string(f_opt->get_default(), v))
161 {
162 cppthread::log << cppthread::log_level_t::error
163 << "invalid default value for a number ("
164 << f_opt->get_default()
165 << ") in parameter --"
166 << f_opt->get_name()
167 << " at offset 0."
168 << cppthread::end;
169 return -1;
170 }
171
172 return v;
173}
174
175
191{
192 if(f_opt->is_defined())
193 {
194 return f_opt->get_double();
195 }
196
197 if(!f_opt->has_default())
198 {
199 return 0.0;
200 }
201
202 double v;
203 if(!validator_double::convert_string(f_opt->get_default(), v))
204 {
205 cppthread::log << cppthread::log_level_t::error
206 << "invalid default value as a double number ("
207 << f_opt->get_default()
208 << ") in parameter --"
209 << f_opt->get_name()
210 << " at offset 0."
211 << cppthread::end;
212 return -1.0;
213 }
214
215 return v;
216}
217
218
226option_info_ref::operator std::string () const
227{
228 if(f_opt->is_defined())
229 {
230 return f_opt->get_value();
231 }
232
233 return f_opt->get_default();
234}
235
236
249{
250 std::string v;
251 if(value != '\0')
252 {
253 v += value;
254 }
255 f_opt->set_value(0, v); // source considered DIRECT
256 return *this;
257}
258
259
272{
273 std::string v;
274 if(value != U'\0')
275 {
276 v = libutf8::to_u8string(value);
277 }
278 f_opt->set_value(0, v); // source considered DIRECT
279 return *this;
280}
281
282
293{
294 if(value == nullptr)
295 {
296 f_opt->set_value(0, std::string()); // source considered DIRECT
297 }
298 else
299 {
300 f_opt->set_value(0, value); // source considered DIRECT
301 }
302 return *this;
303}
304
305
316{
317 f_opt->set_value(0, value); // source considered DIRECT
318 return *this;
319}
320
321
332{
333 f_opt->set_value(0, value); // source considered DIRECT
334 return *this;
335}
336
337
352{
353 std::string v;
354 if(value != '\0')
355 {
356 v += value;
357 }
358 f_opt->set_value(0, static_cast<std::string>(*this) + v); // source considered DIRECT
359 return *this;
360}
361
362
373{
374 std::string v;
375 if(value != '\0')
376 {
377 v += libutf8::to_u8string(value);
378 }
379 f_opt->set_value(0, static_cast<std::string>(*this) + v); // source considered DIRECT
380 return *this;
381}
382
383
394{
395 std::string v;
396 if(value != nullptr)
397 {
398 v = value;
399 }
400 f_opt->set_value(0, static_cast<std::string>(*this) + v); // source considered DIRECT
401 return *this;
402}
403
404
415{
416 f_opt->set_value(0, static_cast<std::string>(*this) + value); // source considered DIRECT
417 return *this;
418}
419
420
431{
432 f_opt->set_value( // source considered DIRECT
433 0
434 , static_cast<std::string>(*this) + static_cast<std::string>(value));
435 return *this;
436}
437
438
452std::string option_info_ref::operator + (char value) const
453{
454 return static_cast<std::string>(*this) + value;
455}
456
457
467std::string option_info_ref::operator + (char32_t value) const
468{
469 return static_cast<std::string>(*this) + libutf8::to_u8string(value);
470}
471
472
482std::string option_info_ref::operator + (char const * value) const
483{
484 if(value == nullptr)
485 {
486 return *this;
487 }
488 return static_cast<std::string>(*this) + value;
489}
490
491
501std::string option_info_ref::operator + (std::string const & value) const
502{
503 return static_cast<std::string>(*this) + value;
504}
505
506
516std::string option_info_ref::operator + (option_info_ref const & value) const
517{
518 return static_cast<std::string>(*this)
519 + static_cast<std::string>(value);
520}
521
522
533std::string operator + (char value, option_info_ref const & rhs)
534{
535 return value + static_cast<std::string>(rhs);
536}
537
538
549std::string operator + (char32_t value, option_info_ref const & rhs)
550{
551 return libutf8::to_u8string(value) + static_cast<std::string>(rhs);
552}
553
554
565std::string operator + (char const * value, option_info_ref const & rhs)
566{
567 if(value == nullptr)
568 {
569 return rhs;
570 }
571 return value + static_cast<std::string>(rhs);
572}
573
574
585std::string operator + (std::string const & value, option_info_ref const & rhs)
586{
587 return value + static_cast<std::string>(rhs);
588}
589
590
597option_info_ref::operator bool () const
598{
599 return !empty();
600}
601
602
610{
611 return empty();
612}
613
614
623bool option_info_ref::operator == (char const * value) const
624{
625 if(value == nullptr)
626 {
627 return empty();
628 }
629 return static_cast<std::string>(*this) == value;
630}
631
632
641bool option_info_ref::operator == (std::string const & value) const
642{
643 return static_cast<std::string>(*this) == value;
644}
645
646
657{
658 return static_cast<std::string>(*this) == static_cast<std::string>(value);
659}
660
661
670bool option_info_ref::operator != (char const * value) const
671{
672 if(value == nullptr)
673 {
674 return !empty();
675 }
676 return static_cast<std::string>(*this) != value;
677}
678
679
688bool option_info_ref::operator != (std::string const & value) const
689{
690 return static_cast<std::string>(*this) != value;
691}
692
693
704{
705 return static_cast<std::string>(*this) != static_cast<std::string>(value);
706}
707
708
717bool option_info_ref::operator < (char const * value) const
718{
719 if(value == nullptr)
720 {
721 return false;
722 }
723 return static_cast<std::string>(*this) < value;
724}
725
726
735bool option_info_ref::operator < (std::string const & value) const
736{
737 return static_cast<std::string>(*this) < value;
738}
739
740
751{
752 return static_cast<std::string>(*this) < static_cast<std::string>(value);
753}
754
755
764bool option_info_ref::operator <= (char const * value) const
765{
766 if(value == nullptr)
767 {
768 return empty();
769 }
770 return static_cast<std::string>(*this) <= value;
771}
772
773
782bool option_info_ref::operator <= (std::string const & value) const
783{
784 return static_cast<std::string>(*this) <= value;
785}
786
787
798{
799 return static_cast<std::string>(*this) <= static_cast<std::string>(value);
800}
801
802
811bool option_info_ref::operator > (char const * value) const
812{
813 if(value == nullptr)
814 {
815 return !empty();
816 }
817 return static_cast<std::string>(*this) > value;
818}
819
820
829bool option_info_ref::operator > (std::string const & value) const
830{
831 return static_cast<std::string>(*this) > value;
832}
833
834
845{
846 return static_cast<std::string>(*this) > static_cast<std::string>(value);
847}
848
849
858bool option_info_ref::operator >= (char const * value) const
859{
860 if(value == nullptr)
861 {
862 return true;
863 }
864 return static_cast<std::string>(*this) >= value;
865}
866
867
876bool option_info_ref::operator >= (std::string const & value) const
877{
878 return static_cast<std::string>(*this) >= value;
879}
880
881
892{
893 return static_cast<std::string>(*this) >= static_cast<std::string>(value);
894}
895
896
897
908bool operator == (char const * value, option_info_ref const & rhs)
909{
910 if(value == nullptr)
911 {
912 return rhs.empty();
913 }
914 return value == static_cast<std::string>(rhs);
915}
916
917
928bool operator == (std::string const & value, option_info_ref const & rhs)
929{
930 return value == static_cast<std::string>(rhs);
931}
932
933
944bool operator != (char const * value, option_info_ref const & rhs)
945{
946 if(value == nullptr)
947 {
948 return !rhs.empty();
949 }
950 return value != static_cast<std::string>(rhs);
951}
952
953
964bool operator != (std::string const & value, option_info_ref const & rhs)
965{
966 return value != static_cast<std::string>(rhs);
967}
968
969
980bool operator < (char const * value, option_info_ref const & rhs)
981{
982 if(value == nullptr)
983 {
984 return !rhs.empty();
985 }
987}
988
989
1000bool operator < (std::string const & value, option_info_ref const & rhs)
1001{
1003}
1004
1005
1016bool operator <= (char const * value, option_info_ref const & rhs)
1017{
1018 if(value == nullptr)
1019 {
1020 return true;
1021 }
1023}
1024
1025
1036bool operator <= (std::string const & value, option_info_ref const & rhs)
1037{
1039}
1040
1041
1052bool operator > (char const * value, option_info_ref const & rhs)
1053{
1054 if(value == nullptr)
1055 {
1056 return false;
1057 }
1058 return value > static_cast<std::string>(rhs);
1059}
1060
1061
1072bool operator > (std::string const & value, option_info_ref const & rhs)
1073{
1074 return value > static_cast<std::string>(rhs);
1075}
1076
1077
1088bool operator >= (char const * value, option_info_ref const & rhs)
1089{
1090 if(value == nullptr)
1091 {
1092 return rhs.empty();
1093 }
1094 return value >= static_cast<std::string>(rhs);
1095}
1096
1097
1108bool operator >= (std::string const & value, option_info_ref const & rhs)
1109{
1110 return value >= static_cast<std::string>(rhs);
1111}
1112
1113
1114
1115} // namespace advgetopt
1116
1117
1118
1119// vim: ts=4 sw=4 et
size_t length() const
Return the length of the option's value.
size_t size() const
Return the length of the option's value.
friend bool operator>(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
bool operator!() const
Check whether the value is an empty string or not.
friend bool operator<=(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
long get_long() const
Retrieve the referenced option as a long.
double get_double() const
Retrieve the referenced option as a double.
bool empty() const
Retrieve the length of the option's value.
option_info::pointer_t f_opt
option_info_ref & operator+=(char value)
Append the character value to this option's value.
friend bool operator<(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
friend bool operator!=(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
friend std::string operator+(char value, option_info_ref const &rhs)
Concatenate a character and an option reference value.
friend bool operator==(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
option_info_ref(option_info::pointer_t opt)
Initialize a reference to an option_info object.
option_info_ref & operator=(char value)
Set the option value to value.
friend bool operator>=(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
std::shared_ptr< option_info > pointer_t
Definition option_info.h:78
static bool convert_string(std::string const &number, double &result)
Convert a string to a double value.
static bool convert_string(std::string const &number, std::int64_t &result)
Convert a string to an std::int64_t value.
Definitions of the advanced getopt exceptions.
The advgetopt environment to parse command line options.
Definition version.h:37
bool operator<(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
constexpr flag_t option_flags_merge()
Definition flags.h:87
bool operator==(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
bool operator>=(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
std::string operator+(char value, option_info_ref const &rhs)
Concatenate a character and an option reference value.
bool operator<=(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
bool operator>(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
bool operator!=(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
Declaration of the option_info class used to record available options.
Declaration of validators which can be used to verify the parameters.
Declaration of validators which can be used to verify the parameters.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.