File: getopt.ml

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (261 lines) | stat: -rw-r--r-- 7,498 bytes parent folder | download | duplicates (6)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
(* Command line handling for OCaml tools in libguestfs.
 * Copyright (C) 2016 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *)

open Common_gettext.Gettext

open Std_utils

open Printf

type spec =
  | Unit of (unit -> unit)
  | Set of bool ref
  | Clear of bool ref
  | String of string * (string -> unit)
  | Set_string of string * string ref
  | Int of string * (int -> unit)
  | Set_int of string * int ref
  | Symbol of string * string list * (string -> unit)
  | OptString of string * (string option -> unit)

module OptionName = struct
  type option_name = S of char | L of string | M of string
end
open OptionName

type keys = option_name list
type doc = string
type usage_msg = string
type anon_fun = (string -> unit)
type c_keys = string array

type speclist = (keys * spec * doc) list

type t = {
  mutable specs : speclist;
  anon_fun : anon_fun option;
  usage_msg : usage_msg;
}

let hidden_option_description = ""

external getopt_parse : string array -> (c_keys * spec * doc) array -> ?anon_fun:anon_fun -> usage_msg -> unit = "guestfs_int_mllib_getopt_parse"

let column_wrap = 38

(* This should only be used for --help and man pages. *)
let string_of_option_name = function
  | S c -> sprintf "-%c" c
  | L s -> "--" ^ s
  | M s -> "-" ^ s

let show_help h () =
  let b = Buffer.create 1024 in

  let spaces n =
    String.make n ' ' in

  let prologue = sprintf (f_"%s\nOptions:\n") h.usage_msg in
  Buffer.add_string b prologue;

  let specs =
    List.filter (
      fun (_, _, doc) ->
      doc <> hidden_option_description
    ) h.specs in

  List.iter (
    fun (keys, spec, doc) ->
      let columns = ref 0 in
      let add s =
        Buffer.add_string b s;
        columns := !columns + (String.length s)
      in

      add "  ";
      add (String.concat ", " (List.map string_of_option_name keys));
      let arg =
        match spec with
        | Unit _
        | Set _
        | Clear _
        | OptString _ -> None
        | String (arg, _)
        | Set_string (arg, _)
        | Int (arg, _)
        | Set_int (arg, _)
        | Symbol (arg, _, _) -> Some arg in
      let optarg =
        match spec with
        | Unit _
        | Set _
        | Clear _
        | String _
        | Set_string _
        | Int _
        | Set_int _
        | Symbol _ -> None
        | OptString (arg, _) -> Some arg in
      (match arg, optarg with
      | None, None -> ()    (* --foo *)
      | Some arg, None ->   (* --foo=val *)
        add (sprintf " <%s>" arg)
      | None, Some arg ->   (* --foo[=val] *)
        add (sprintf "[=%s]" arg)
      | Some _, Some _ ->   (* should not happen *)
        failwith "internal error: getopt: option marked both with arg and optarg"
      );
      if !columns >= column_wrap then (
        Buffer.add_char b '\n';
        Buffer.add_string b (spaces column_wrap);
      ) else (
        Buffer.add_string b (spaces (column_wrap - !columns));
      );
      Buffer.add_string b doc;
      Buffer.add_char b '\n';
  ) specs;

  Buffer.output_buffer stdout b;
  exit 0

let is_prefix str prefix =
  let n = String.length prefix in
  String.length str >= n && String.sub str 0 n = prefix

(* Implement `--short-options' and `--long-options'. *)
let display_short_options h () =
  List.iter (
    fun (args, _, _) ->
      List.iter (
        function
        | S c -> printf "-%c\n" c
        | M s -> printf "-%s\n" s
        | L _ -> ()
      ) args
  ) h.specs;
  exit 0
let display_long_options h () =
  List.iter (
    fun (args, _, _) ->
      List.iter (
        function
        | L "short-options" | L "long-options"
        | S _ -> ()
        | L s | M s -> printf "--%s\n" s
      ) args
  ) h.specs;
  exit 0

let compare_command_line_args a b =
  let string_of_option_name_no_dashes = function
    | S c -> String.make 1 c
    | L s | M s -> s
  in
  let a = String.lowercase_ascii (string_of_option_name_no_dashes a) in
  let b = String.lowercase_ascii (string_of_option_name_no_dashes b) in
  compare a b

let create specs ?anon_fun usage_msg =
  (* Sanity check the input *)
  let validate_key = function
    | M s when String.length s <> 2 || (s.[0] <> 'i' && s.[0] <> 'o') ->
       invalid_arg "Getopt spec: invalid use of M\"...\" option - only use this for virt-v2v -iX and -oX options"
    | L"" -> invalid_arg "Getopt spec: invalid empty long option"
    | L"help" -> invalid_arg "Getopt spec: should not have L\"help\""
    | L"short-options" ->
       invalid_arg "Getopt spec: should not have L\"short-options\""
    | L"long-options" ->
       invalid_arg "Getopt spec: should not have L\"long-options\""
    | L s when s.[0] = '-' ->
       invalid_arg (sprintf "Getopt spec: L%S should not begin with a dash"
                            s)
    | L s when String.contains s '_' ->
       invalid_arg (sprintf "Getopt spec: L%S should not contain '_'"
                            s)
    | _ -> ()
  in

  let validate_spec = function
    | Unit _ -> ()
    | Set _ -> ()
    | Clear _ -> ()
    | String _ -> ()
    | Set_string _ -> ()
    | Int _ -> ()
    | Set_int _ -> ()
    | OptString _ -> ()
    | Symbol (_, elements, _) ->
      List.iter (
        fun e ->
          if String.length e == 0 || is_prefix e "-" then
            invalid_arg (sprintf "invalid element in Symbol: '%s'" e);
      ) elements;
  in

  List.iter (
    fun (keys, spec, doc) ->
      if keys == [] then
        invalid_arg "empty keys for Getopt spec";
      List.iter validate_key keys;
      validate_spec spec;
  ) specs;

  let t = {
    specs = [];      (* Set it later, with own options, and sorted. *)
    anon_fun = anon_fun;
    usage_msg = usage_msg;
  } in

  let added_options = [
    [ L"short-options" ], Unit (display_short_options t),
                                         hidden_option_description;
    [ L"long-options" ], Unit (display_long_options t),
                                         hidden_option_description;
    [ L"help" ], Unit (show_help t),     s_"Display brief help";
  ] in
  let specs = added_options @ specs in

  (* Sort the specs.  *)
  let specs = List.map (
    fun (keys, action, doc) ->
      List.hd (List.sort compare_command_line_args keys), (keys, action, doc)
  ) specs in
  let specs =
    let cmp (arg1, _) (arg2, _) = compare_command_line_args arg1 arg2 in
    List.sort cmp specs in
  let specs = List.map snd specs in

  t.specs <- specs;
  t

let parse_argv t argv =
  let specs = List.map (
    fun (keys, spec, doc) ->
      let keys = List.map (
        function
        | S c -> sprintf "-%c" c
        | L s | M s -> sprintf "--%s" s
      ) keys in
      let keys = Array.of_list keys in
      keys, spec, doc
  ) t.specs in
  let specs = Array.of_list specs in
  getopt_parse argv specs ?anon_fun:t.anon_fun t.usage_msg

let parse t =
  parse_argv t Sys.argv