Skip to content

Commit

Permalink
fix displaying help
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg committed Feb 3, 2021
1 parent f59021c commit b0f156d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 12 deletions.
16 changes: 13 additions & 3 deletions src/cmdliner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,19 @@ module Term = struct
let sibling_terms = List.map snd choices in
let ei = Cmdliner_info.eval ~env
(Sub_command { term = main ; path ; main ; sibling_terms}) in
let _, _, ei = add_stdopts ei in
Cmdliner_msg.pp_err_usage err_ppf ei ~err_lines:false ~err;
`Error `Parse
let help, version, ei = add_stdopts ei in
let term_args = Cmdliner_info.(term_args @@ eval_term ei) in
let args = remove_exec argv in
begin match Cmdliner_cline.create ~peek_opts:true term_args args with
| Ok cl
| Error (_, cl) ->
begin match try_eval_stdopts ~catch:true ei cl help version with
| Some e -> do_result help_ppf err_ppf ei e
| None ->
Cmdliner_msg.pp_err_usage err_ppf ei ~err_lines:false ~err;
`Error `Parse
end
end
| Error (`Invalid_command (maybe, path, choices, hints)) ->
let err = Cmdliner_base.err_unknown ~kind:"command" maybe ~hints in
let sibling_terms = List.map snd choices in
Expand Down
3 changes: 3 additions & 0 deletions src/cmdliner_docgen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ let term_info_subst ei = function

let invocation ?(sep = ' ') ei = match Cmdliner_info.eval_kind ei with
| `Simple | `Multiple_main -> term_name (Cmdliner_info.eval_main ei)
| `Multiple_group
| `Multiple_sub ->
let sep = String.make 1 sep in
Cmdliner_info.eval_terms_rev ei
Expand All @@ -83,6 +84,7 @@ let synopsis_pos_arg a =

let synopsis ei = match Cmdliner_info.eval_kind ei with
| `Multiple_main -> strf "$(b,%s) $(i,COMMAND) ..." @@ invocation ei
| `Multiple_group
| `Simple | `Multiple_sub ->
let rev_cli_order (a0, _) (a1, _) =
Cmdliner_info.rev_arg_pos_cli_order a0 a1
Expand All @@ -99,6 +101,7 @@ let synopsis ei = match Cmdliner_info.eval_kind ei with

let cmd_docs ei = match Cmdliner_info.eval_kind ei with
| `Simple | `Multiple_sub -> []
| `Multiple_group
| `Multiple_main ->
let add_cmd acc t =
let cmd = strf "$(b,%s)" @@ term_name t in
Expand Down
7 changes: 6 additions & 1 deletion src/cmdliner_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,12 @@ let eval_kind ei =
(* subgroup *)
if ei.choices = [] then `Simple else
if (ei.term.term_info.term_name == ei.main.term_info.term_name)
then `Multiple_main else `Multiple_sub
then
match ei.path with
| [] -> assert false
| [_] -> `Multiple_main
| _ :: _ :: _ -> `Multiple_group
else `Multiple_sub

let eval_terms_rev ei = ei.path

Expand Down
2 changes: 1 addition & 1 deletion src/cmdliner_info.mli
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ val eval_term : eval -> term
val eval_main : eval -> term
val eval_choices : eval -> term list
val eval_env_var : eval -> string -> string option
val eval_kind : eval -> [> `Multiple_main | `Multiple_sub | `Simple ]
val eval_kind : eval -> [> `Multiple_main | `Multiple_group | `Multiple_sub | `Simple ]
val eval_with_term : eval -> term -> eval
val eval_has_choice : eval -> string -> bool
val eval_terms_rev : eval -> term list
Expand Down
1 change: 1 addition & 0 deletions src/cmdliner_msg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ let pp_version ppf ei = match Cmdliner_info.(term_version @@ eval_main ei) with
let pp_try_help ppf ei = match Cmdliner_info.eval_kind ei with
| `Simple | `Multiple_main ->
pp ppf "@[<2>Try `%s --help' for more information.@]" (exec_name ei)
| `Multiple_group
| `Multiple_sub ->
let exec_cmd = Cmdliner_docgen.plain_invocation ei in
let parent =
Expand Down
2 changes: 1 addition & 1 deletion test/chorus.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(* Implementation of the command *)

let chorus count msg = for i = 1 to count do print_endline msg done
let chorus count msg = for _ = 1 to count do print_endline msg done

(* Command line interface *)

Expand Down
42 changes: 36 additions & 6 deletions test/groups.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

$ ./groups.exe things
groups things: is a command group and requires a command argument.
Usage: groups COMMAND ...
Try `groups things --help' for more information.
Usage: groups things [OPTION]...
Try `groups things --help' or `groups --help' for more information.
[124]
$ ./groups.exe things show foo
Expand All @@ -28,9 +28,32 @@


$ ./groups.exe things --help
NAME
groups-things

SYNOPSIS
groups things [OPTION]...

COMMANDS
list

show


OPTIONS
--help[=FMT] (default=auto)
Show this help in format FMT. The value FMT must be one of `auto',
`pager', `groff' or `plain'. With `auto', the format is `pager` or
`plain' whenever the TERM env var is `dumb' or undefined.
--version
Show version information.
$ ./groups.exe things --invalid-arg
groups things: is a command group and requires a command argument.
Usage: groups COMMAND ...
Try `groups things --help' for more information.
Usage: groups things [OPTION]...
Try `groups things --help' or `groups --help' for more information.
[124]

$ ./groups.exe --help
Expand Down Expand Up @@ -66,8 +89,8 @@

$ ./groups.exe widgets baz
groups widgets: unknown command `baz'.
Usage: groups COMMAND ...
Try `groups widgets --help' for more information.
Usage: groups widgets [OPTION]...
Try `groups widgets --help' or `groups --help' for more information.
[124]

Prefixes
Expand All @@ -85,3 +108,10 @@ Default cmd
$ ./groups.exe
default cmd
Version
$ ./groups.exe things --version
%%VERSION%%
$ ./groups.exe things list --version
%%VERSION%%

0 comments on commit b0f156d

Please sign in to comment.