let parse () =
let pos =
ref 0
in
let scmd =
ref
(SubCommand.make
(s_ "none")
""
""
(fun () ->
pp_print_help NoSubCommand Output err_formatter ();
failwith
(s_ "No subcommand defined, call 'oasis help' for help")))
in
let scmd_args =
ref [||]
in
let set_scmd s =
scmd := SubCommand.find s;
scmd_args :=
Array.sub Sys.argv !pos ((Array.length Sys.argv) - !pos);
pos := !pos + Array.length !scmd_args
in
let handle_error exc hext =
let get_bad str =
match split_newline ~trim:false str with
| fst :: _ ->
fst
| [] ->
s_ "Unknown error on the command line"
in
match exc with
| Arg.Bad txt ->
pp_print_help hext Output err_formatter ();
prerr_newline ();
prerr_endline (get_bad txt);
exit 2
| Arg.Help txt ->
pp_print_help hext Output std_formatter ();
exit 0
| e ->
raise e
in
begin
try
Arg.parse_argv
~current:pos
Sys.argv
(Arg.align specs)
set_scmd
usage_msg
with e ->
handle_error e NoSubCommand
end;
begin
try
Arg.parse_argv
~current:(ref 0)
!scmd_args
(Arg.align !scmd.scmd_specs)
!scmd.scmd_anon
(Printf.sprintf
(f_ "Subcommand %s options:\n")
!scmd.scmd_name)
with e ->
handle_error e (SubCommand !scmd.scmd_name)
end;
!scmd.scmd_main