Skip to content
This repository
tag: v1545
Fetching contributors…

Cannot retrieve contributors at this time

file 121 lines (107 sloc) 3.691 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
(*
Copyright © 2011, 2012 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

(** 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 *)

let cmdname =
  try
    Sys.argv.(1)
  with
    _ -> (Printf.eprintf ("Usage: %s <cmdname> [sectionbasename]\n") Sys.argv.(0); exit 1)

let sectionbasename =
  try
    Sys.argv.(2)
  with
    _ -> cmdname

let read_section name = File.content_opt (sectionbasename ^ "." ^ name)

let help_summary, help_synopsis, help_description, help_options =
  match read_section "help" with
    None -> "","","",""
  | Some help ->
    let reg0 = (* one line summary ? *)
      Str.regexp ("^.*"^ (Str.quote cmdname) ^"[ \t]*:[ \t]*\\(.*\\)$")
    in
    let is_blank x =
      BaseString.contains " \n\t" x
    in
    let summary, pos0 =
      try
if Str.string_partial_match reg0 help 0
then
BaseString.ltrim ~is_space:is_blank (Str.matched_group 1 help), (Str.match_end () + 1) (* +1 is meant to skip \n *)
else
"", 0
      with
Not_found -> "", 0
    in
    let reg1 = (* one line synopsis ? *)
      Str.regexp ("^[ \t]*[Uu]sage[ \t]*:.*\\("^ (Str.quote cmdname) ^".*\\)$")
    in
    let synopsis, pos1 =
      try
if Str.string_partial_match reg1 help pos0
then
BaseString.ltrim ~is_space:is_blank (Str.matched_group 1 help), (Str.match_end () +1)
else
"", pos0
      with
Not_found -> "", pos0
    in
    let reg2 = (* beginning of the list of options after the description ? *)
      Str.regexp "^\\(.*[Oo]ptions.*\\):[ \t]*\n\\([ \t]*--?[a-zA-Z0-0]+\\)"
    in
    let description, options =
      try
let pos1a = Str.search_forward reg2 help pos1
in
(* description *)
BaseString.ltrim ~is_space:is_blank (BaseString.sub help pos1 (pos1a-pos1)),
        (* options *)
        let first_words =
BaseString.trim ~is_space:is_blank (Str.matched_group 1 help)
in
(* N.B. we try keep the last line before the first option unless it's just "Options:" *)
let pos2 =
if first_words = "Options" || first_words = "options" then Str.group_beginning 2 else pos1a
in
BaseString.rtrim ~is_space:is_blank (Str.string_after help pos2)
      with
Not_found ->
  BaseString.ltrim ~is_space:is_blank (Str.string_after help pos1), ""
        (* no option? then put everything in description *)
    in
    summary, synopsis, description, options

let summary =
  match read_section "summary", help_summary with
    None, s when s <> "" -> Some(s)
  | x, _ -> x

let synopsis =
  match read_section "synopsis", help_synopsis with
  | None, s when s <> "" -> Some(s)
  | x, _ -> x

let description =
  match read_section "description", help_description with
    None, s when s <> "" -> Some(s)
  | x, _ -> x

let options =
  match read_section "options", help_options with
    None, s when s <> "" -> Some(s)
  | x, _ -> x

let _ = BaseArg.write_simple_manpage
    ~cmdname
    ?summary
    ~section:1
    ~centerheader:"Opa Manual"
    ?synopsis
    ?description
    ?other:(match options with Some(str) -> Some["OPTIONS", str] | None -> None)
    stdout
Something went wrong with that request. Please try again.