21
21
#include < stdlib.h>
22
22
#include < rwlock.h>
23
23
24
- #include " CLI11.hpp"
25
-
26
24
using namespace std ;
27
25
using namespace rwlock ;
28
26
29
- static const char * BIN_NAME = " mcs-load-brm-from-file" ;
27
+ #include < boost/program_options.hpp>
28
+ namespace po = boost::program_options;
30
29
31
30
std::string getShmemLocksList ()
32
31
{
@@ -85,31 +84,66 @@ int lockOp(size_t minLockId, size_t maxLockId, bool lock, bool read)
85
84
return 0 ;
86
85
}
87
86
87
+ void conflicting_options (const boost::program_options::variables_map& vm, const std::string& opt1,
88
+ const std::string& opt2)
89
+ {
90
+ if (vm.count (opt1) && !vm[opt1].defaulted () && vm.count (opt2) && !vm[opt2].defaulted ())
91
+ {
92
+ throw std::logic_error (std::string (" Conflicting options '" ) + opt1 + " ' and '" + opt2 + " '." );
93
+ }
94
+ }
95
+
96
+ template <class T >
97
+ void check_value (const boost::program_options::variables_map& vm, const std::string& opt1, T lower_bound,
98
+ T upper_bound)
99
+ {
100
+ auto value = vm[opt1].as <T>();
101
+ if (value < lower_bound || value >= upper_bound)
102
+ {
103
+ throw std::logic_error (std::string (" Option '" ) + opt1 + " ' is out of range.: " + std::to_string (value));
104
+ }
105
+ }
106
+
88
107
int main (int argc, char ** argv)
89
108
{
90
- CLI::App app{BIN_NAME};
91
- app.description (
92
- " A tool to operate or view shmem locks. If neither read nor write operation is specified, the tool will "
93
- " display the lock state." );
94
- uint8_t lockId;
109
+ int lockId;
95
110
bool debug = false ;
96
111
bool read = false ;
97
112
bool write = false ;
98
113
bool lock = false ;
99
114
bool unlock = false ;
100
115
101
- app.add_option <uint8_t >(" -i, --lock-id" , lockId, " Shmem lock numerical id: " + getShmemLocksList ())
102
- ->expected (0 , RWLockNames.size ())
103
- ->required ();
104
- app.add_flag (" -r, --read-lock" , read , " Use read lock." )->default_val (false );
105
- app.add_flag (" -w, --write-lock" , write , " Use write lock.." )->default_val (false )->excludes (" -r" );
106
- app.add_flag (" -l, --lock" , lock, " Lock the corresponding shmem lock." )->default_val (false );
107
- app.add_flag (" -u, --unlock" , unlock, " Unlock the corresponding shmem write lock." )
108
- ->default_val (false )
109
- ->excludes (" -l" );
110
- app.add_flag (" -d,--debug" , debug, " Print extra output." )->default_val (false );
116
+ po::options_description desc (
117
+ " A tool to operate or view shmem locks. If neither read nor write operation is specified, the tool "
118
+ " will "
119
+ " display the lock state." );
120
+
121
+ std::string lockid_description = std::string (" Shmem lock numerical id: " ) + getShmemLocksList ();
122
+
123
+ // clang-format off
124
+ desc.add_options ()(" help" , " produce help message" )
125
+ (" lock-id,i" , po::value<int >(&lockId)->required (), lockid_description.c_str ())
126
+ (" read-lock,r" , po::bool_switch (&read )->default_value (false ), " Use read lock." )
127
+ (" write-lock,w" , po::bool_switch (&write )->default_value (false ), " Use write lock." )
128
+ (" lock,l" , po::bool_switch (&lock)->default_value (false ), " Lock the corresponding shmem lock." )
129
+ (" unlock,u" , po::bool_switch (&unlock)->default_value (false ), " Unlock the corresponding shmem write lock." )
130
+ (" debug,d" , po::bool_switch (&debug)->default_value (false ), " Print extra output." );
131
+ // clang-format on
132
+
133
+ po::variables_map vm;
134
+ po::store (po::parse_command_line (argc, argv, desc), vm);
111
135
112
- CLI11_PARSE (app, argc, argv);
136
+ if (argc == 1 || vm.count (" help" ))
137
+ {
138
+ cout << desc << " \n " ;
139
+ return 1 ;
140
+ }
141
+
142
+ conflicting_options (vm, " lock" , " unlock" );
143
+ conflicting_options (vm, " read-lock" , " write-lock" );
144
+ check_value<int >(vm, " lock-id" , 0 , RWLockNames.size ());
145
+
146
+ po::notify (vm);
113
147
114
148
if (!read && !write )
115
149
{
@@ -125,4 +159,3 @@ int main(int argc, char** argv)
125
159
126
160
return 0 ;
127
161
}
128
-
0 commit comments