Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 122 lines (107 sloc) 3.677 kb
c55ebd1 [enhance] manpages: created simple program for quick manpage generation
Mathieu Baudet authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18
19 (** This program is meant to quickly build manpages either from existing sections in proper files, or by a quick-and-dirty parsing of the result of --help *)
20
21 let cmdname =
22 try
23 Sys.argv.(1)
24 with
babea9c [enhance] manpages: now builds manpages in standard build directory
Mathieu Baudet authored
25 _ -> (Printf.eprintf ("Usage: %s <cmdname> [sectionbasename]\n") Sys.argv.(0); exit 1)
c55ebd1 [enhance] manpages: created simple program for quick manpage generation
Mathieu Baudet authored
26
babea9c [enhance] manpages: now builds manpages in standard build directory
Mathieu Baudet authored
27 let sectionbasename =
28 try
29 Sys.argv.(2)
30 with
31 _ -> cmdname
32
33 let read_section name = File.content_opt (sectionbasename ^ "." ^ name)
c55ebd1 [enhance] manpages: created simple program for quick manpage generation
Mathieu Baudet authored
34
35 let help_summary, help_synopsis, help_description, help_options =
36 match read_section "help" with
37 None -> "","","",""
38 | Some help ->
39 let reg0 = (* one line summary ? *)
40 Str.regexp ("^.*"^ (Str.quote cmdname) ^"[ \t]*:[ \t]*\(.*\)$")
41 in
42 let is_blank x =
43 BaseString.contains " \n\t" x
44 in
45 let summary, pos0 =
46 try
47 if Str.string_partial_match reg0 help 0
48 then
49 BaseString.ltrim ~is_space:is_blank (Str.matched_group 1 help), (Str.match_end () + 1) (* +1 is meant to skip \n *)
50 else
51 "", 0
52 with
53 Not_found -> "", 0
54 in
55 let reg1 = (* one line synopsis ? *)
56 Str.regexp ("^[ \t]*[Uu]sage[ \t]*:.*\("^ (Str.quote cmdname) ^".*\)$")
57 in
58 let synopsis, pos1 =
59 try
60 if Str.string_partial_match reg1 help pos0
61 then
62 BaseString.ltrim ~is_space:is_blank (Str.matched_group 1 help), (Str.match_end () +1)
63 else
64 "", pos0
65 with
66 Not_found -> "", pos0
67 in
68 let reg2 = (* beginning of the list of options after the description ? *)
69 Str.regexp "^\(.*[Oo]ptions.*\):[ \t]*\n\([ \t]*--?[a-zA-Z0-0]+\)"
70 in
71 let description, options =
72 try
73 let pos1a = Str.search_forward reg2 help pos1
74 in
75 (* description *)
76 BaseString.ltrim ~is_space:is_blank (BaseString.sub help pos1 (pos1a-pos1)),
77 (* options *)
78 let first_words =
79 BaseString.trim ~is_space:is_blank (Str.matched_group 1 help)
80 in
81 (* N.B. we try keep the last line before the first option unless it's just "Options:" *)
82 let pos2 =
83 if first_words = "Options" || first_words = "options" then Str.group_beginning 2 else pos1a
84 in
85 BaseString.rtrim ~is_space:is_blank (Str.string_after help pos2)
86 with
87 Not_found ->
88 BaseString.ltrim ~is_space:is_blank (Str.string_after help pos1), ""
89 (* no option? then put everything in description *)
90 in
91 summary, synopsis, description, options
92
babea9c [enhance] manpages: now builds manpages in standard build directory
Mathieu Baudet authored
93 let summary =
94 match read_section "summary", help_summary with
95 None, s when s <> "" -> Some(s)
96 | x, _ -> x
c55ebd1 [enhance] manpages: created simple program for quick manpage generation
Mathieu Baudet authored
97
98 let synopsis =
99 match read_section "synopsis", help_synopsis with
100 | None, s when s <> "" -> Some(s)
101 | x, _ -> x
102
103 let description =
104 match read_section "description", help_description with
105 None, s when s <> "" -> Some(s)
106 | x, _ -> x
107
108 let options =
109 match read_section "options", help_options with
110 None, s when s <> "" -> Some(s)
111 | x, _ -> x
112
113 let _ = BaseArg.write_simple_manpage
114 ~cmdname
115 ?summary
116 ~section:1
117 ~centerheader:"Opa Manual"
118 ?synopsis
119 ?description
120 ?other:(match options with Some(str) -> Some["OPTIONS", str] | None -> None)
121 stdout
Something went wrong with that request. Please try again.