13
13
#pragma once
14
14
15
15
#include < sharg/detail/format_base.hpp>
16
+ #include < sharg/test/tmp_filename.hpp>
16
17
17
18
namespace sharg ::detail
18
19
{
@@ -38,6 +39,9 @@ class format_man : public format_help_base<format_man>
38
39
// !\brief Befriend the base class to give access to the private member functions.
39
40
friend base_type;
40
41
42
+ // !\brief Whether to call man and open the man page.
43
+ bool open_man_page{false };
44
+
41
45
public:
42
46
/* !\name Constructors, destructor and assignment
43
47
* \{
@@ -52,10 +56,38 @@ class format_man : public format_help_base<format_man>
52
56
// !\copydoc sharg::detail::format_help_base::format_help_base
53
57
format_man (std::vector<std::string> const & names,
54
58
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} {};
57
63
// !\}
58
64
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
+
59
91
private:
60
92
// !\brief Prints a help page header in man page format to std::cout.
61
93
void print_header ()
0 commit comments