fork download
  1. #include <errno.h>
  2.  
  3. #include <functional>
  4.  
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8.  
  9. class PscArgp {
  10. public:
  11. PscArgp();
  12. ~PscArgp();
  13.  
  14. typedef std::function<void(std::string arg)> Callback;
  15.  
  16. void setName(std::string const & name);
  17. void setEmail(std::string const & email);
  18. void addOption(std::string name, std::string help, Callback f, int key, std::string arg = "");
  19. void addOptionRequired(std::string name, std::string help, Callback f, int key, std::string arg = "");
  20. void addOptionHeader(std::string header);
  21. void parse(int argc, char * argv[]);
  22.  
  23. private:
  24. void add(std::string name, std::string help, int key, std::string arg, int flags, Callback f);
  25. error_t parser(int key, char * arg, struct argp_state * state);
  26. std::vector<struct argp_option> options;
  27. std::map<int, Callback> callbacks;
  28. };
  29.  
  30. #include <vector>
  31. #include <string>
  32. #include <argp.h>
  33.  
  34. PscArgp::PscArgp() {
  35. }
  36.  
  37. PscArgp::~PscArgp() {
  38. }
  39.  
  40. //extern "C" {
  41. error_t PscArgp::parser(int key, char * arg, struct argp_state * state) {
  42. Callback f = callbacks.find(key)->second;
  43. f(arg);
  44. return 0;
  45. }
  46. //}
  47. using std::placeholders::_1;
  48. using std::placeholders::_2;
  49. using std::placeholders::_3;
  50. void PscArgp::parse(int argc, char * argv[]) {
  51. std::function<error_t (int, char*, struct argp_state*)> f = std::bind(&PscArgp::parser, this, _1, _2, _3);
  52. struct argp argp_parser;
  53. argp_parser.options = options.data();
  54. argp_parser.parser = f;
  55. //argp_parser.args_doc =
  56. //argp_parser.doc =
  57. //argp_parser.children =
  58.  
  59. //argp_parse(&argp_parser, argc, argv, 0, 0, 0);
  60. }
  61.  
  62. void PscArgp::setName(std::string const & name) {
  63. argp_program_version = name.c_str();
  64. }
  65.  
  66. void PscArgp::setEmail(std::string const & email) {
  67. argp_program_bug_address = email.c_str();
  68. }
  69.  
  70. void PscArgp::add(std::string name, std::string help, int key, std::string arg, int flags, Callback f) {
  71. struct argp_option opt = {0};
  72. opt.key = key;
  73. opt.flags = flags;
  74. if (!name.empty()) opt.name = name.c_str();
  75. if (!help.empty()) opt.doc = help.c_str();
  76.  
  77. options.push_back(opt);
  78. callbacks.insert(std::pair<int, Callback>(key, f));
  79. }
  80.  
  81. void PscArgp::addOption(std::string name, std::string help, Callback f, int key, std::string arg) {
  82. add(name, help, key, arg, OPTION_ARG_OPTIONAL, f);
  83. }
  84.  
  85. void PscArgp::addOptionHeader(std::string header) {
  86. add("", header, 0, "", 0, NULL);
  87. }
  88.  
  89. void PscArgp::addOptionRequired(std::string name, std::string help, Callback f, int key, std::string arg) {
  90. add(name, help, key, arg, 0, f);
  91. }
  92.  
  93. #include <iostream>
  94. struct config {
  95. int cpu;
  96. } config;
  97.  
  98. void parseCpu(std::string const & arg) {
  99. std::cout << "setting cpu: " << arg << std::endl;
  100. config.cpu = 1;
  101. }
  102.  
  103. int main(int argc, char *argv[])
  104. {
  105. PscArgp program;
  106. program.setName("MyWorld");
  107. program.setEmail("user@doma.cx");
  108.  
  109. std::function<void(std::string)> f = parseCpu;
  110. //program.addOption("compile", "Do something with your object", 'c', "file");
  111. program.addOptionRequired("cpu", "Specific the cpu to run on", parseCpu, 'u', "cpu");
  112. program.parse(argc, argv);
  113.  
  114. }
  115.  
  116.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function 'void PscArgp::parse(int, char**)':
prog.cpp:54:23: error: cannot convert 'std::function<int(int, char*, argp_state*)>' to 'error_t (*)(int, char*, argp_state*)' in assignment
stdout
Standard output is empty