1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
|
/* **************************************************************************
* Copyright (C) 2020-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* *************************************************************************/
// This emulates the required functionality of boost::program_options
#pragma once
#include <cinttypes>
#include <cstdio>
#include <iomanip>
#include <map>
#include <ostream>
#include <regex>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
namespace roc
{
// Regular expression for token delimiters (whitespace and commas)
extern const std::regex program_options_regex;
// Polymorphic base class to use with dynamic_cast
class value_base
{
protected:
bool m_has_actual = false;
bool m_has_default = false;
public:
virtual ~value_base() = default;
bool has_actual() const
{
return m_has_actual;
}
bool has_default() const
{
return m_has_default;
}
};
// Value parameters
template <typename T>
class value : public value_base
{
T m_var; // Variable to be modified if no pointer provided
T* m_var_ptr; // Pointer to variable to be modified
public:
// Constructor
explicit value()
: m_var_ptr(nullptr)
{
}
explicit value(T var, bool defaulted)
: m_var(var)
, m_var_ptr(nullptr)
{
m_has_actual = !defaulted;
m_has_default = defaulted;
}
explicit value(T* var_ptr)
: m_var_ptr(var_ptr)
{
}
// Allows actual_value() and default_value()
value* operator->()
{
return this;
}
// Get the value
const T& get_value() const
{
if(m_var_ptr)
return *m_var_ptr;
else
return m_var;
}
// Set actual value
value& actual_value(T val)
{
if(m_var_ptr)
*m_var_ptr = std::move(val);
else
m_var = std::move(val);
m_has_actual = true;
return *this;
}
// Set default value
value& default_value(T val)
{
if(!m_has_actual)
{
if(m_var_ptr)
*m_var_ptr = std::move(val);
else
m_var = std::move(val);
m_has_default = true;
}
return *this;
}
};
// bool_switch is a value<bool>, which is handled specially
using bool_switch = value<bool>;
class variable_value
{
std::shared_ptr<value_base> m_val;
public:
// Constructor
explicit variable_value() = default;
template <typename T>
explicit variable_value(const T& xv, bool xdefaulted)
: m_val(std::make_shared<value<T>>(xv, xdefaulted))
{
}
explicit variable_value(std::shared_ptr<value_base> val)
: m_val(val)
{
}
// Member functions
bool empty() const
{
return !m_val.get() || (!m_val->has_actual() && !m_val->has_default());
}
bool defaulted() const
{
return m_val.get() && !m_val->has_actual() && m_val->has_default();
}
template <typename T>
const T& as() const
{
if(value<T>* val = dynamic_cast<value<T>*>(m_val.get()))
return val->get_value();
else
throw std::logic_error("Internal error: Invalid cast");
}
};
using variables_map = std::map<std::string, variable_value>;
class options_description
{
// desc_option describes a particular option
class desc_option
{
std::string m_opts;
std::shared_ptr<value_base> m_val;
std::string m_desc;
public:
// Constructor with options, value and description
template <typename T>
desc_option(std::string opts, value<T> val, std::string desc)
: m_opts(std::move(opts))
, m_val(new auto(std::move(val)))
, m_desc(std::move(desc))
{
}
// Constructor with options and description
desc_option(std::string opts, std::string desc)
: m_opts(std::move(opts))
, m_val(nullptr)
, m_desc(std::move(desc))
{
}
// Accessors
const std::string& get_opts() const
{
return m_opts;
}
const std::shared_ptr<value_base> get_val() const
{
return m_val;
}
const std::string& get_desc() const
{
return m_desc;
}
// Set a value
void set_val(int& argc, char**& argv, std::string inopt) const
{
// We test all supported types with dynamic_cast and parse accordingly
bool match = false;
if(auto* ptr = dynamic_cast<value<int32_t>*>(m_val.get()))
{
int32_t val;
match = argc && sscanf(*argv, "%" SCNd32, &val) == 1;
ptr->actual_value(val);
}
else if(auto* ptr = dynamic_cast<value<uint32_t>*>(m_val.get()))
{
uint32_t val;
match = argc && sscanf(*argv, "%" SCNu32, &val) == 1;
ptr->actual_value(val);
}
else if(auto* ptr = dynamic_cast<value<int64_t>*>(m_val.get()))
{
int64_t val;
match = argc && sscanf(*argv, "%" SCNd64, &val) == 1;
ptr->actual_value(val);
}
else if(auto* ptr = dynamic_cast<value<uint64_t>*>(m_val.get()))
{
uint64_t val;
match = argc && sscanf(*argv, "%" SCNu64, &val) == 1;
ptr->actual_value(val);
}
else if(auto* ptr = dynamic_cast<value<float>*>(m_val.get()))
{
float val;
match = argc && sscanf(*argv, "%f", &val) == 1;
ptr->actual_value(val);
}
else if(auto* ptr = dynamic_cast<value<double>*>(m_val.get()))
{
double val;
match = argc && sscanf(*argv, "%lf", &val) == 1;
ptr->actual_value(val);
}
else if(auto* ptr = dynamic_cast<value<char>*>(m_val.get()))
{
char val;
match = argc && sscanf(*argv, " %c", &val) == 1;
ptr->actual_value(val);
}
else if(auto* ptr = dynamic_cast<value<bool>*>(m_val.get()))
{
// We handle bool specially, setting the value to true without argument
ptr->actual_value(true);
return;
}
else if(auto* ptr = dynamic_cast<value<std::string>*>(m_val.get()))
{
if(argc)
{
ptr->actual_value(*argv);
match = true;
}
}
else
{
throw std::logic_error("Internal error: Unsupported data type");
}
if(!match)
throw std::invalid_argument(argc ? "Invalid value for " + inopt
: "Missing required value for " + inopt);
// Skip past the argument's value
++argv;
--argc;
}
};
// Description and option list
std::string m_desc;
std::vector<desc_option> m_optlist;
// desc_optionlist allows chains of options to be parenthesized
class desc_optionlist
{
std::vector<desc_option>& m_list;
public:
explicit desc_optionlist(std::vector<desc_option>& list)
: m_list(list)
{
}
template <typename... Ts>
desc_optionlist operator()(Ts&&... arg)
{
m_list.push_back(desc_option(std::forward<Ts>(arg)...));
return *this;
}
};
// Parse an option at the current (argc, argv) position
void parse_option(int& argc, char**& argv, variables_map& vm, bool ignoreUnknown) const
{
// Iterate across all options
for(const auto& opt : m_optlist)
{
// Canonical name used for map
std::string canonical_name;
// Iterate across tokens in the opts
for(std::sregex_token_iterator tok{opt.get_opts().begin(), opt.get_opts().end(),
program_options_regex, -1};
tok != std::sregex_token_iterator(); ++tok)
{
// The first option in a list of options is the canonical name
if(!canonical_name.length())
canonical_name = tok->str();
// If the length of the option is 1, it is single-dash; otherwise double-dash
const char* prefix = tok->length() == 1 ? "-" : "--";
// If option matches
if(*argv == prefix + tok->str())
{
++argv;
--argc;
// If option has a value, set it
if(opt.get_val().get())
opt.set_val(argc, argv, prefix + tok->str());
// Add seen options to map
vm[canonical_name] = variable_value(opt.get_val());
return; // Return successfully
}
}
}
// No options were matched
if(ignoreUnknown)
{
++argv;
--argc;
}
else
throw std::invalid_argument("Option " + std::string(argv[0]) + " is not defined.");
}
public:
// Constructor
explicit options_description(std::string desc)
: m_desc(std::move(desc))
{
}
// Start a desc_optionlist chain
desc_optionlist add_options() &
{
return desc_optionlist(m_optlist);
}
// Parse all options
void parse_options(int& argc, char**& argv, variables_map& vm, bool ignoreUnknown = false) const
{
// Add options with default values to map
for(const auto& opt : m_optlist)
{
std::sregex_token_iterator tok{opt.get_opts().begin(), opt.get_opts().end(),
program_options_regex, -1};
// Canonical name used for map
std::string canonical_name = tok->str();
if(opt.get_val().get() && opt.get_val()->has_default())
vm[canonical_name] = variable_value(opt.get_val());
}
// Parse options
while(argc)
parse_option(argc, argv, vm, ignoreUnknown);
}
// Formatted output of command-line arguments description
friend std::ostream& operator<<(std::ostream& os, const options_description& d)
{
// Iterate across all options
for(const auto& opt : d.m_optlist)
{
bool first = true, printvalue = true;
const char* delim = "";
std::ostringstream left;
// Iterate across tokens in the opts
for(std::sregex_token_iterator tok{opt.get_opts().begin(), opt.get_opts().end(),
program_options_regex, -1};
tok != std::sregex_token_iterator(); ++tok, first = false, delim = " ")
{
// If the length of the option is 1, it is single-dash; otherwise double-dash
const char* prefix = tok->length() == 1 ? "-" : "--";
left << delim << (first ? "" : "|") << prefix << tok->str();
if(tok->str() == "help" || tok->str() == "h")
printvalue = false;
}
if(printvalue)
left << " <value>";
os << std::setw(26) << std::left << left.str() << " " << opt.get_desc() << " ";
left.str(std::string());
// Print the default value of the variable type if it exists
// We do not print the default value for bool
const value_base* val = opt.get_val().get();
if(val && !dynamic_cast<const value<bool>*>(val))
{
if(val->has_default())
{
// We test all supported types with dynamic_cast and print accordingly
left << " (Default value is: ";
if(dynamic_cast<const value<int32_t>*>(val))
left << dynamic_cast<const value<int32_t>*>(val)->get_value();
else if(dynamic_cast<const value<uint32_t>*>(val))
left << dynamic_cast<const value<uint32_t>*>(val)->get_value();
else if(dynamic_cast<const value<int64_t>*>(val))
left << dynamic_cast<const value<int64_t>*>(val)->get_value();
else if(dynamic_cast<const value<uint64_t>*>(val))
left << dynamic_cast<const value<uint64_t>*>(val)->get_value();
else if(dynamic_cast<const value<float>*>(val))
left << dynamic_cast<const value<float>*>(val)->get_value();
else if(dynamic_cast<const value<double>*>(val))
left << dynamic_cast<const value<double>*>(val)->get_value();
else if(dynamic_cast<const value<char>*>(val))
left << dynamic_cast<const value<char>*>(val)->get_value();
else if(dynamic_cast<const value<std::string>*>(val))
left << dynamic_cast<const value<std::string>*>(val)->get_value();
else
throw std::logic_error("Internal error: Unsupported data type");
left << ")";
}
}
os << left.str() << "\n\n";
}
return os << std::flush;
}
};
// Class representing command line parser
class parse_command_line
{
variables_map m_vm;
public:
parse_command_line(int argc, char** argv, const options_description& desc, bool ignoreUnknown = false)
{
++argv; // Skip argv[0]
--argc;
desc.parse_options(argc, argv, m_vm, ignoreUnknown);
}
// Copy the variables_map
friend void store(const parse_command_line& p, variables_map& vm)
{
vm = p.m_vm;
}
// Move the variables_map
friend void store(parse_command_line&& p, variables_map& vm)
{
vm = std::move(p.m_vm);
}
};
// We can define the notify() function as a no-op for our purposes
inline void notify(const variables_map&) {}
}
|