cmd_line_utils.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying file
  6. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_WAVE_LIBS_WAVE_TEST_CMD_LINE_UTILS_HPP)
  9. #define BOOST_WAVE_LIBS_WAVE_TEST_CMD_LINE_UTILS_HPP
  10. #include <string>
  11. #include <fstream>
  12. #include <vector>
  13. #include <boost/config.hpp>
  14. #include <boost/assert.hpp>
  15. #include <boost/any.hpp>
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // forward declarations only
  18. namespace cmd_line_utils
  19. {
  20. class include_paths;
  21. }
  22. namespace boost { namespace program_options
  23. {
  24. void validate(boost::any &v, std::vector<std::string> const &s,
  25. cmd_line_utils::include_paths *, int);
  26. }} // boost::program_options
  27. ///////////////////////////////////////////////////////////////////////////////
  28. #include <boost/program_options.hpp>
  29. ///////////////////////////////////////////////////////////////////////////////
  30. namespace cmd_line_utils {
  31. namespace po = boost::program_options;
  32. ///////////////////////////////////////////////////////////////////////////
  33. // Additional command line parser which interprets '@something' as an
  34. // option "config-file" with the value "something".
  35. inline std::pair<std::string, std::string>
  36. at_option_parser(std::string const& s)
  37. {
  38. if ('@' == s[0])
  39. return std::make_pair(std::string("config-file"), s.substr(1));
  40. else
  41. return std::pair<std::string, std::string>();
  42. }
  43. ///////////////////////////////////////////////////////////////////////////
  44. // class, which keeps the include file information read from the options
  45. class include_paths
  46. {
  47. public:
  48. include_paths() : seen_separator(false) {}
  49. std::vector<std::string> paths; // stores user paths
  50. std::vector<std::string> syspaths; // stores system paths
  51. bool seen_separator; // options contain a '-I-' option
  52. // Function which validates additional tokens from the given option.
  53. static void
  54. validate(boost::any &v, std::vector<std::string> const &tokens)
  55. {
  56. if (v.empty())
  57. v = boost::any(include_paths());
  58. include_paths *p = boost::any_cast<include_paths>(&v);
  59. BOOST_ASSERT(NULL != p);
  60. // Assume only one path per '-I' occurrence.
  61. std::string const& t = po::validators::get_single_string(tokens);
  62. if (t == "-") {
  63. // found -I- option, so switch behaviour
  64. p->seen_separator = true;
  65. }
  66. else if (p->seen_separator) {
  67. // store this path as a system path
  68. p->syspaths.push_back(t);
  69. }
  70. else {
  71. // store this path as an user path
  72. p->paths.push_back(t);
  73. }
  74. }
  75. };
  76. ///////////////////////////////////////////////////////////////////////////
  77. // Read all options from a given config string, parse and add them to the
  78. // given variables_map
  79. inline void
  80. read_config_options(int debuglevel, std::string const &indata,
  81. po::options_description const &desc, po::variables_map &vm)
  82. {
  83. if (9 == debuglevel) {
  84. std::cerr << "read_config_options: reading config options"
  85. << std::endl;
  86. }
  87. std::istringstream istrm(indata);
  88. std::vector<std::string> options;
  89. std::string line;
  90. while (std::getline(istrm, line)) {
  91. // skip empty lines
  92. std::string::size_type pos = line.find_first_not_of(" \t");
  93. if (pos == std::string::npos)
  94. continue;
  95. // skip comment lines
  96. if ('#' != line[pos])
  97. options.push_back(line);
  98. }
  99. if (options.size() > 0) {
  100. if (9 == debuglevel) {
  101. std::cerr << "read_config_options: options size is: "
  102. << (int)options.size() << std::endl;
  103. }
  104. // (the (int) cast is to make the True64 compiler happy)
  105. using namespace boost::program_options::command_line_style;
  106. po::store(po::command_line_parser(options)
  107. .options(desc).style((int)unix_style).run(), vm);
  108. po::notify(vm);
  109. }
  110. if (9 == debuglevel) {
  111. std::cerr << "read_config_options: succeeded to read config options"
  112. << std::endl;
  113. }
  114. }
  115. ///////////////////////////////////////////////////////////////////////////
  116. // Read all options from a given config file, parse and add them to the
  117. // given variables_map
  118. inline bool
  119. read_config_file(int debuglevel, std::string const &filename,
  120. po::options_description const &desc, po::variables_map &vm)
  121. {
  122. if (9 == debuglevel) {
  123. std::cerr << "read_config_file: reading config options"
  124. << std::endl;
  125. }
  126. std::ifstream ifs(filename.c_str());
  127. if (!ifs.is_open()) {
  128. std::cerr
  129. << "testwave: " << filename
  130. << ": command line warning: config file not found"
  131. << std::endl;
  132. return false;
  133. }
  134. // parse the file and extract all given arguments and options
  135. std::vector<std::string> options;
  136. std::string line;
  137. while (std::getline(ifs, line)) {
  138. // skip empty lines
  139. std::string::size_type pos = line.find_first_not_of(" \t");
  140. if (pos == std::string::npos)
  141. continue;
  142. // skip comment lines
  143. if ('#' != line[pos])
  144. options.push_back(line);
  145. }
  146. if (options.size() > 0) {
  147. if (9 == debuglevel) {
  148. std::cerr << "read_config_file: options size is: "
  149. << (int)options.size() << std::endl;
  150. }
  151. // treat positional arguments as --input parameters
  152. po::positional_options_description p;
  153. p.add("input", -1);
  154. // parse the vector of lines and store the results into the given
  155. // variables map
  156. // (the (int) cast is to make the True64 compiler happy)
  157. using namespace boost::program_options::command_line_style;
  158. po::store(po::command_line_parser(options)
  159. .options(desc).positional(p).style((int)unix_style).run(), vm);
  160. po::notify(vm);
  161. }
  162. if (9 == debuglevel) {
  163. std::cerr << "read_config_file: succeeded to read config options"
  164. << std::endl;
  165. }
  166. return true;
  167. }
  168. ///////////////////////////////////////////////////////////////////////////
  169. // predicate to extract all positional arguments from the command line
  170. struct is_argument
  171. {
  172. bool operator()(po::option const &opt)
  173. {
  174. return (opt.position_key == -1) ? true : false;
  175. }
  176. };
  177. ///////////////////////////////////////////////////////////////////////////////
  178. } // namespace cmd_line_utils
  179. ///////////////////////////////////////////////////////////////////////////////
  180. //
  181. // Special validator overload, which allows to handle the -I- syntax for
  182. // switching the semantics of an -I option.
  183. //
  184. // This must be injected into the boost::program_options namespace
  185. //
  186. ///////////////////////////////////////////////////////////////////////////////
  187. namespace boost { namespace program_options {
  188. inline void
  189. validate(boost::any &v, std::vector<std::string> const &s,
  190. cmd_line_utils::include_paths *, int)
  191. {
  192. cmd_line_utils::include_paths::validate(v, s);
  193. }
  194. }} // namespace boost::program_options
  195. #endif // !defined(BOOST_WAVE_LIBS_WAVE_TEST_CMD_LINE_UTILS_HPP)