1313#pragma once
1414
1515#include < sharg/detail/format_base.hpp>
16+ #include < sharg/test/tmp_filename.hpp>
1617
1718namespace sharg ::detail
1819{
@@ -38,6 +39,9 @@ class format_man : public format_help_base<format_man>
3839 // !\brief Befriend the base class to give access to the private member functions.
3940 friend base_type;
4041
42+ // !\brief Whether to call man and open the man page.
43+ bool open_man_page{false };
44+
4145public:
4246 /* !\name Constructors, destructor and assignment
4347 * \{
@@ -52,10 +56,38 @@ class format_man : public format_help_base<format_man>
5256 // !\copydoc sharg::detail::format_help_base::format_help_base
5357 format_man (std::vector<std::string> const & names,
5458 update_notifications const version_updates,
55- bool const advanced = false ) :
56- base_type{names, version_updates, advanced} {};
59+ bool const advanced = false ,
60+ bool const open_man_page = false ) :
61+ base_type{names, version_updates, advanced},
62+ open_man_page{open_man_page} {};
5763 // !\}
5864
65+ /* !\brief Initiates the printing of the man page to std::cout or opens it in man.
66+ * \param[in] parser_meta The meta information that are needed for a detailed man page.
67+ */
68+ void parse (parser_meta_data & parser_meta)
69+ {
70+ if (!open_man_page)
71+ return base_type::parse (parser_meta);
72+
73+ sharg::test::tmp_filename tmp_file{parser_meta.app_name .c_str ()};
74+
75+ {
76+ std::ofstream out{tmp_file.get_path ()};
77+ std::streambuf * coutbuf = std::cout.rdbuf ();
78+ std::cout.rdbuf (out.rdbuf ());
79+
80+ base_type::parse (parser_meta);
81+
82+ std::cout.rdbuf (coutbuf);
83+ }
84+
85+ std::string command{" /usr/bin/man -l " };
86+ command += tmp_file.get_path ().c_str ();
87+ if (std::system (command.c_str ()) != 0 )
88+ throw sharg::parser_error{" Unexpected failure." }; // LCOV_EXCL_LINE
89+ }
90+
5991private:
6092 // !\brief Prints a help page header in man page format to std::cout.
6193 void print_header ()
0 commit comments