Skip to content

Commit

Permalink
[enhance] baseArg: added write_simple_manpage
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Baudet committed Dec 20, 2011
1 parent 96b18ab commit b25e4e4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
56 changes: 56 additions & 0 deletions libbase/baseArg.ml
Expand Up @@ -111,6 +111,62 @@ type anon_fun = Arg.anon_fun
exception Help = Arg.Help
exception Bad = Arg.Bad

(* -- generate a simple manpage -- *)

let date_manpage () =
let dt = Unix.gmtime (Unix.time ()) in
(Date.fullmonth.(dt.Unix.tm_mon))
^ " " ^ (string_of_int (dt.Unix.tm_mday))
^ ", " ^ (string_of_int (dt.Unix.tm_year+1900))


(* todo: move to baseList *)
let pretty_list_to_string empty left separator right = function
| [] -> empty
| x::q -> (List.fold_left (fun s y -> s ^ separator ^ y) (left ^ x) q) ^ right

let split_option_args str =
let reg = Str.regexp "[>)\"}] " in
try
let pos = (Str.search_forward reg str 0) + 1
in
(String.ltrim (Str.string_before str pos)), (String.ltrim (Str.string_after str pos))
with
Not_found -> "", (String.ltrim str)

let print_spec file (key, spec, doc) =
let key = String.replace key "-" "\\-" in
let options, doc = split_option_args doc in
match spec with
| Symbol (l, _) -> Printf.fprintf file ".TP\n%s %s %s\n%s\n" key (pretty_list_to_string "<none>" "{" "|" "}" l) options doc
| _ -> Printf.fprintf file ".TP\n%s %s\n%s\n" key options doc; ()

let add_help speclist =
let add help =
if List.exists (fun (x, _, _) -> x = help) speclist then []
else [help, Unit (fun x->x), " Display this list of options"]
in
speclist @ (add "-help") @ (add "--help")

let write_simple_manpage
~cmdname ~section
?(centerfooter=(date_manpage ()))
?(leftfooter="") ?(centerheader="")
~summary ?synopsis ?description ?options ?(other=[])
file =
Printf.fprintf file ".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n" cmdname (string_of_int section) centerfooter leftfooter centerheader;
Printf.fprintf file ".SH NAME\n%s \\- %s\n" cmdname summary;
begin match synopsis with None -> ()| Some(text) -> Printf.fprintf file ".SH SYNOPSIS\n%s\n" text end;
begin match description with None -> ()| Some(text) -> Printf.fprintf file ".SH DESCRIPTION\n%s\n" text end;
begin match options with None -> () | Some(speclist) -> begin
Printf.fprintf file ".SH OPTIONS\n";
List.iter (print_spec file) (add_help speclist);
end end;
List.iter (fun (title, content) -> Printf.fprintf file ".SH %s\n%s\n" title content) other;
()

(* --- *)


let sort_by_name l = List.stable_sort (fun (x,_,_) (y,_,_) -> compare (x:string) y) l
let sort l = (* also makes names unique *)
Expand Down
12 changes: 12 additions & 0 deletions libbase/baseArg.mli
Expand Up @@ -60,6 +60,18 @@ val parse_argv : ?current:int ref -> string array ->
(key * spec * doc) list -> anon_fun -> usage_msg -> unit
val usage : (key * spec * doc) list -> usage_msg -> unit

val write_simple_manpage :
cmdname:string ->
section:int ->
?centerfooter:string ->
?leftfooter:string ->
?centerheader:string ->
summary:string ->
?synopsis:string ->
?description:string ->
?options:(string * spec * string) list ->
?other:(string * string) list -> out_channel -> unit

val align : (key * spec * doc) list -> (key * spec * doc) list
(** beware, if you wish to call [add_bash_completion], you should do it before calling [align] *)
val current : int ref
Expand Down

0 comments on commit b25e4e4

Please sign in to comment.