Client: do not display usage on all errors

This commit is contained in:
Benjamin Canou 2018-02-28 12:08:55 +01:00
parent 64a41538c8
commit 40ea190364

View File

@ -1190,24 +1190,24 @@ let pp_cli_errors ppf ~executable_name ~global_options ~default errs =
| Bad_argument (i, v) -> | Bad_argument (i, v) ->
Format.fprintf ppf Format.fprintf ppf
"Erroneous command line argument %d (%s)." i v ; "Erroneous command line argument %d (%s)." i v ;
[] Some []
| Option_expected_argument (arg, command) -> | Option_expected_argument (arg, command) ->
Format.fprintf ppf Format.fprintf ppf
"Command line option @{<opt>%s@} expects an argument." arg ; "Command line option @{<opt>%s@} expects an argument." arg ;
Option.unopt_map ~f:(fun command -> [ Ex command ]) ~default:[] command Some (Option.unopt_map ~f:(fun command -> [ Ex command ]) ~default:[] command)
| Bad_option_argument (arg, command) -> | Bad_option_argument (arg, command) ->
Format.fprintf ppf Format.fprintf ppf
"Wrong value for command line option @{<opt>%s@}." arg ; "Wrong value for command line option @{<opt>%s@}." arg ;
Option.unopt_map ~f:(fun command -> [ Ex command ]) ~default:[] command Some (Option.unopt_map ~f:(fun command -> [ Ex command ]) ~default:[] command)
| Multiple_occurences (arg, command) -> | Multiple_occurences (arg, command) ->
Format.fprintf ppf Format.fprintf ppf
"Command line option @{<opt>%s@} appears multiple times." arg ; "Command line option @{<opt>%s@} appears multiple times." arg ;
Option.unopt_map ~f:(fun command -> [ Ex command ]) ~default:[] command Some (Option.unopt_map ~f:(fun command -> [ Ex command ]) ~default:[] command)
| No_manual_entry [ keyword ] -> | No_manual_entry [ keyword ] ->
Format.fprintf ppf Format.fprintf ppf
"No manual entry that match @{<hilight>%s@}." "No manual entry that match @{<hilight>%s@}."
keyword ; keyword ;
[] Some []
| No_manual_entry (keyword :: keywords) -> | No_manual_entry (keyword :: keywords) ->
Format.fprintf ppf Format.fprintf ppf
"No manual entry that match %a and @{<hilight>%s@}." "No manual entry that match %a and @{<hilight>%s@}."
@ -1216,29 +1216,29 @@ let pp_cli_errors ppf ~executable_name ~global_options ~default errs =
(fun ppf keyword -> Format.fprintf ppf "@{<hilight>%s@}" keyword)) (fun ppf keyword -> Format.fprintf ppf "@{<hilight>%s@}" keyword))
keywords keywords
keyword ; keyword ;
[] Some []
| Unknown_option (option, command) -> | Unknown_option (option, command) ->
Format.fprintf ppf Format.fprintf ppf
"Unexpected command line option @{<opt>%s@}." "Unexpected command line option @{<opt>%s@}."
option ; option ;
Option.unopt_map ~f:(fun command -> [ Ex command ]) ~default:[] command Some (Option.unopt_map ~f:(fun command -> [ Ex command ]) ~default:[] command)
| Extra_arguments (extra, command) -> | Extra_arguments (extra, command) ->
Format.fprintf ppf Format.fprintf ppf
"Extra command line arguments:@, @[<h>%a@]." "Extra command line arguments:@, @[<h>%a@]."
(Format.pp_print_list (fun ppf -> Format.fprintf ppf "%s")) extra ; (Format.pp_print_list (fun ppf -> Format.fprintf ppf "%s")) extra ;
[ Ex command ] Some [ Ex command ]
| Unterminated_command (_, commands) -> | Unterminated_command (_, commands) ->
Format.fprintf ppf Format.fprintf ppf
"@[<v 2>Unterminated command, here are possible completions.@,%a@]" "@[<v 2>Unterminated command, here are possible completions.@,%a@]"
(Format.pp_print_list (Format.pp_print_list
(fun ppf (Command { params ; options = Argument { spec ; _ } ; _ }) -> (fun ppf (Command { params ; options = Argument { spec ; _ } ; _ }) ->
print_commandline ppf ([], spec, params))) commands ; print_commandline ppf ([], spec, params))) commands ;
List.map (fun c -> Ex c) commands Some (List.map (fun c -> Ex c) commands)
| Command_not_found ([], _all_commands) -> | Command_not_found ([], _all_commands) ->
Format.fprintf ppf Format.fprintf ppf
"@[<v 0>Unrecognized command.@,\ "@[<v 0>Unrecognized command.@,\
Try using the @{<kwd>man@} command to get more information.@]" ; Try using the @{<kwd>man@} command to get more information.@]" ;
[] Some []
| Command_not_found (_, commands) -> | Command_not_found (_, commands) ->
Format.fprintf ppf Format.fprintf ppf
"@[<v 0>Unrecognized command.@,\ "@[<v 0>Unrecognized command.@,\
@ -1246,17 +1246,26 @@ let pp_cli_errors ppf ~executable_name ~global_options ~default errs =
(Format.pp_print_list (Format.pp_print_list
(fun ppf (Command { params ; options = Argument { spec ; _ } ; _ }) -> (fun ppf (Command { params ; options = Argument { spec ; _ } ; _ }) ->
print_commandline ppf ([], spec, params))) commands ; print_commandline ppf ([], spec, params))) commands ;
List.map (fun c -> Ex c) commands Some (List.map (fun c -> Ex c) commands)
| err -> default ppf err ; [] in | err -> default ppf err ; None in
let rec pp acc = function let rec pp acc errs =
| [] -> [] let return command =
| [ last ] -> pp_one last @ acc match command, acc with
| None, _ -> acc
| Some command, Some commands -> Some (command @ commands)
| Some command, None -> Some command in
match errs with
| [] -> None
| [ last ] -> return (pp_one last)
| err :: errs -> | err :: errs ->
let acc = pp_one err @ acc in let acc = return (pp_one err) in
Format.fprintf ppf "@," ; Format.fprintf ppf "@," ;
pp acc errs in pp acc errs in
Format.fprintf ppf "@[<v 2>@{<error>@{<title>Error@}@}@," ; Format.fprintf ppf "@[<v 2>@{<error>@{<title>Error@}@}@," ;
let commands = pp [] errs in match pp None errs with
| None ->
Format.fprintf ppf "@]@\n"
| Some commands ->
Format.fprintf ppf "@]@\n@\n@[<v 0>%a@]" Format.fprintf ppf "@]@\n@\n@[<v 0>%a@]"
(fun ppf commands -> usage_internal ppf ~executable_name ~global_options commands) (fun ppf commands -> usage_internal ppf ~executable_name ~global_options commands)
commands commands