args.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. // Copyright (c) 2016 Klemens D. Morgenstern
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_PROCESS_ARGS_HPP
  11. #define BOOST_PROCESS_ARGS_HPP
  12. /** \file boost/process/args.hpp
  13. *
  14. * This header provides the \xmlonly <globalname alt="boost::process::args">args</globalname>\endxmlonly property. It also provides the
  15. * alternative name \xmlonly <globalname alt="boost::process::argv">argv</globalname>\endxmlonly .
  16. *
  17. *
  18. \xmlonly
  19. <programlisting>
  20. namespace boost {
  21. namespace process {
  22. <emphasis>unspecified</emphasis> <globalname alt="boost::process::args">args</globalname>;
  23. <emphasis>unspecified</emphasis> <globalname alt="boost::process::argv">argv</globalname>;
  24. }
  25. }
  26. </programlisting>
  27. \endxmlonly
  28. */
  29. #include <boost/process/detail/basic_cmd.hpp>
  30. #include <iterator>
  31. namespace boost { namespace process { namespace detail {
  32. struct args_
  33. {
  34. template<typename T>
  35. using remove_reference_t = typename std::remove_reference<T>::type;
  36. template<typename T>
  37. using value_type = typename remove_reference_t<T>::value_type;
  38. template<typename T>
  39. using vvalue_type = value_type<value_type<T>>;
  40. template <class Range>
  41. arg_setter_<vvalue_type<Range>, true> operator()(Range &&range) const
  42. {
  43. return arg_setter_<vvalue_type<Range>, true>(std::forward<Range>(range));
  44. }
  45. template <class Range>
  46. arg_setter_<vvalue_type<Range>, true> operator+=(Range &&range) const
  47. {
  48. return arg_setter_<vvalue_type<Range>, true>(std::forward<Range>(range));
  49. }
  50. template <class Range>
  51. arg_setter_<vvalue_type<Range>, false> operator= (Range &&range) const
  52. {
  53. return arg_setter_<vvalue_type<Range>, false>(std::forward<Range>(range));
  54. }
  55. template<typename Char>
  56. arg_setter_<Char, true> operator()(std::basic_string<Char> && str) const
  57. {
  58. return arg_setter_<Char, true> (str);
  59. }
  60. template<typename Char>
  61. arg_setter_<Char, true> operator+=(std::basic_string<Char> && str) const
  62. {
  63. return arg_setter_<Char, true> (str);
  64. }
  65. template<typename Char>
  66. arg_setter_<Char, false> operator= (std::basic_string<Char> && str) const
  67. {
  68. return arg_setter_<Char, false>(str);
  69. }
  70. template<typename Char>
  71. arg_setter_<Char, true> operator()(const std::basic_string<Char> & str) const
  72. {
  73. return arg_setter_<Char, true> (str);
  74. }
  75. template<typename Char>
  76. arg_setter_<Char, true> operator+=(const std::basic_string<Char> & str) const
  77. {
  78. return arg_setter_<Char, true> (str);
  79. }
  80. template<typename Char>
  81. arg_setter_<Char, false> operator= (const std::basic_string<Char> & str) const
  82. {
  83. return arg_setter_<Char, false>(str);
  84. }
  85. template<typename Char>
  86. arg_setter_<Char, true> operator()(std::basic_string<Char> & str) const
  87. {
  88. return arg_setter_<Char, true> (str);
  89. }
  90. template<typename Char>
  91. arg_setter_<Char, true> operator+=(std::basic_string<Char> & str) const
  92. {
  93. return arg_setter_<Char, true> (str);
  94. }
  95. template<typename Char>
  96. arg_setter_<Char, false> operator= (std::basic_string<Char> & str) const
  97. {
  98. return arg_setter_<Char, false>(str);
  99. }
  100. template<typename Char>
  101. arg_setter_<Char, true> operator()(const Char* str) const
  102. {
  103. return arg_setter_<Char, true> (str);
  104. }
  105. template<typename Char>
  106. arg_setter_<Char, true> operator+=(const Char* str) const
  107. {
  108. return arg_setter_<Char, true> (str);
  109. }
  110. template<typename Char>
  111. arg_setter_<Char, false> operator= (const Char* str) const
  112. {
  113. return arg_setter_<Char, false>(str);
  114. }
  115. // template<typename Char, std::size_t Size>
  116. // arg_setter_<Char, true> operator()(const Char (&str) [Size]) const
  117. // {
  118. // return arg_setter_<Char, true> (str);
  119. // }
  120. // template<typename Char, std::size_t Size>
  121. // arg_setter_<Char, true> operator+=(const Char (&str) [Size]) const
  122. // {
  123. // return arg_setter_<Char, true> (str);
  124. // }
  125. // template<typename Char, std::size_t Size>
  126. // arg_setter_<Char, false> operator= (const Char (&str) [Size]) const
  127. // {
  128. // return arg_setter_<Char, false>(str);
  129. // }
  130. arg_setter_<char, true> operator()(std::initializer_list<const char*> &&range) const
  131. {
  132. return arg_setter_<char>(range.begin(), range.end());
  133. }
  134. arg_setter_<char, true> operator+=(std::initializer_list<const char*> &&range) const
  135. {
  136. return arg_setter_<char, true>(range.begin(), range.end());
  137. }
  138. arg_setter_<char, false> operator= (std::initializer_list<const char*> &&range) const
  139. {
  140. return arg_setter_<char, true>(range.begin(), range.end());
  141. }
  142. arg_setter_<char, true> operator()(std::initializer_list<std::string> &&range) const
  143. {
  144. return arg_setter_<char>(range.begin(), range.end());
  145. }
  146. arg_setter_<char, true> operator+=(std::initializer_list<std::string> &&range) const
  147. {
  148. return arg_setter_<char, true>(range.begin(), range.end());
  149. }
  150. arg_setter_<char, false> operator= (std::initializer_list<std::string> &&range) const
  151. {
  152. return arg_setter_<char, true>(range.begin(), range.end());
  153. }
  154. arg_setter_<wchar_t, true> operator()(std::initializer_list<const wchar_t*> &&range) const
  155. {
  156. return arg_setter_<wchar_t>(range.begin(), range.end());
  157. }
  158. arg_setter_<wchar_t, true> operator+=(std::initializer_list<const wchar_t*> &&range) const
  159. {
  160. return arg_setter_<wchar_t, true>(range.begin(), range.end());
  161. }
  162. arg_setter_<wchar_t, false> operator= (std::initializer_list<const wchar_t*> &&range) const
  163. {
  164. return arg_setter_<wchar_t, true>(range.begin(), range.end());
  165. }
  166. arg_setter_<wchar_t, true> operator()(std::initializer_list<std::wstring> &&range) const
  167. {
  168. return arg_setter_<wchar_t>(range.begin(), range.end());
  169. }
  170. arg_setter_<wchar_t, true> operator+=(std::initializer_list<std::wstring> &&range) const
  171. {
  172. return arg_setter_<wchar_t, true>(range.begin(), range.end());
  173. }
  174. arg_setter_<wchar_t, false> operator= (std::initializer_list<std::wstring> &&range) const
  175. {
  176. return arg_setter_<wchar_t, true>(range.begin(), range.end());
  177. }
  178. };
  179. }
  180. /**
  181. The `args` property allows to explicitly set arguments for the execution. The
  182. name of the executable will always be the first element in the arg-vector.
  183. \section args_details Details
  184. \subsection args_operations Operations
  185. \subsubsection args_set_var Setting values
  186. To set a the argument vector the following syntax can be used.
  187. \code{.cpp}
  188. args = value;
  189. args(value);
  190. \endcode
  191. `std::initializer_list` is among the allowed types, so the following syntax is also possible.
  192. \code{.cpp}
  193. args = {value1, value2};
  194. args({value1, value2});
  195. \endcode
  196. Below the possible types for `value` are listed, with `char_type` being either `char` or `wchar_t`.
  197. \paragraph args_set_var_value value
  198. - `std::basic_string<char_type>`
  199. - `const char_type * `
  200. - `std::initializer_list<const char_type *>`
  201. - `std::vector<std::basic_string<char_type>>`
  202. Additionally any range of `std::basic_string<char_type>` can be passed.
  203. \subsubsection args_append_var Appending values
  204. To append a the argument vector the following syntax can be used.
  205. \code{.cpp}
  206. args += value;
  207. \endcode
  208. `std::initializer_list` is among the allowed types, so the following syntax is also possible.
  209. \code{.cpp}
  210. args += {value1, value2};
  211. \endcode
  212. Below the possible types for `value` are listed, with `char_type` being either `char` or `wchar_t`.
  213. \paragraph args_append_var_value value
  214. - `std::basic_string<char_type>`
  215. - `const char_type * `
  216. - `std::initializer_list<const char_type *>`
  217. - `std::vector<std::basic_string<char_type>>`
  218. Additionally any range of `std::basic_string<char_type>` can be passed.
  219. \subsection args_example Example
  220. The overload form is used when more than one string is passed, from the second one forward.
  221. I.e. the following expressions have the same results:
  222. \code{.cpp}
  223. spawn("gcc", "--version");
  224. spawn("gcc", args ="--version");
  225. spawn("gcc", args+="--version");
  226. spawn("gcc", args ={"--version"});
  227. spawn("gcc", args+={"--version"});
  228. \endcode
  229. \note A string will be parsed and set in quotes if it has none and contains spaces.
  230. */
  231. constexpr boost::process::detail::args_ args{};
  232. ///Alias for \xmlonly <globalname alt="boost::process::args">args</globalname> \endxmlonly .
  233. constexpr boost::process::detail::args_ argv{};
  234. }}
  235. #endif