Last active
January 7, 2017 20:49
-
-
Save alecjacobson/92fa6eed67ce1de4fe6e672d70b40f2e to your computer and use it in GitHub Desktop.
Find a parameter in argc/argv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "find_param.h" | |
#include <iostream> | |
// http://stackoverflow.com/a/868894/148668 | |
int main(int argc,const char *argv[]) | |
{ | |
// Default values: | |
double tol = std::numeric_limits<double>::max(); | |
std::string msg = "nothing to say"; | |
// Process flags/parameter-pairs of the form: -f, -p val | |
// Default values: | |
double tol = std::numeric_limits<double>::max(); | |
// Process flags/parameter-pairs of the form: -f, -p val | |
int argi = 1; | |
while(true) | |
{ | |
if(std::string(argv[argi]).compare(0,1,"-") == 0) | |
{ | |
switch(argv[argi][1]) | |
{ | |
case 'm': igl::find_param(argc,argv,argi,msg); break; | |
case 't': igl::find_param(argc,argv,argi,tol); break; | |
default: assert(false && "Unsupported parameter"); | |
} | |
}else break; | |
argi++; | |
if(argi>=argc) break; | |
} | |
std::cout<<"tol: "<<tol<<std::endl; | |
std::cout<<"msg: "<<msg<<std::endl; | |
for(;i<argc;i++) | |
{ | |
std::cout<<"remaining args: "<<argv[i]<<std::endl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <stdexcept> | |
#include <iostream> | |
#include <cassert> | |
namespace igl | |
{ | |
// Find a parameter (e.g., after a flag). This expects that argv was | |
// defined as const char * argv[], as in: | |
// | |
// int main(int argc, const char * argv[]); | |
// | |
// Inputs: | |
// argc length of argv | |
// argv array of parameters | |
// i index of flag after which we hope to find a double | |
// Outputs: | |
// i incremented if following parameter exists | |
// d set to following double value (only set if parameter is found and | |
// successfuly interpreted as a double) | |
// Returns iff success | |
template <typename T> | |
inline bool find_param(const int argc, const char ** argv,int & i, T & d); | |
// Helper function. Safely convert str to templated type. | |
// | |
// Inputs: | |
// str string to convert | |
// Output: | |
// d object to convert into | |
// Returns true iff conversion succeeds | |
template <typename T> | |
inline bool safe_sto(const char * str, T & d); | |
// Implementation | |
template <> | |
inline bool safe_sto(const char * str, double & d) | |
{ | |
// http://stackoverflow.com/a/20026548/148668 | |
try | |
{ | |
d = std::stod(str); | |
} catch (const std::invalid_argument&) | |
{ | |
std::cerr << "Argument ("<<str<<") is not a valid double"<<std::endl; | |
return false; | |
} catch (const std::out_of_range&) | |
{ | |
std::cerr << "Argument ("<<str<<") is out of range for double"<<std::endl; | |
return false; | |
} | |
return true; | |
}; | |
template <> | |
inline bool safe_sto(const char * str, std::string & s) | |
{ | |
s = str; | |
return true; | |
} | |
// Implementation | |
template <typename T> | |
inline bool find_param(const int argc, const char ** argv,int & i, T & d) | |
{ | |
assert(i+1 < argc && "t requires arguement"); | |
if(i+1>=argc) | |
{ | |
return false; | |
} | |
i++; | |
assert(safe_sto(argv[i],d) && "t's arg should be a double"); | |
return safe_sto(argv[i],d); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment