Merged
Conversation
emillon
commented
Aug 4, 2022
- keep alias support
- remove group support now that Cmd exists
|
What is the best way to review this? Should we compare with upstream cmdliner? |
Author
|
@Alizter the easiest way would be to review the diff between 1.1.1 upstream and 1.1.1-dune: |
|
Here is the diff for reference diff --git a/src/cmdliner.mli b/src/cmdliner.mli
index 43622e2..c6d179a 100644
--- a/src/cmdliner.mli
+++ b/src/cmdliner.mli
@@ -573,8 +573,10 @@ module Cmd : sig
(** {1:info Environment variable information} *)
[@@@alert "-deprecated"]
+
type info = Term.env_info (* because of Arg. *)
(** The type for environment variable information. *)
+
[@@@alert "+deprecated"]
val info : ?deprecated:string -> ?docs:string -> ?doc:string -> var -> info
@@ -799,11 +801,13 @@ module Arg : sig
(** The type for converted argument printers. *)
[@@@alert "-deprecated"] (* Need to be able to mention them ! *)
+
type 'a conv = 'a parser * 'a printer
(** The type for argument converters.
{b Warning.} Do not use directly, use {!val-conv} or {!val-conv'}.
This type will become abstract in the next major version of cmdliner. *)
+
[@@@alert "+deprecated"] (* Need to be able to mention them ! *)
val conv :
@@ -947,6 +951,15 @@ module Arg : sig
{b Note.} Environment variable lookup is unsupported for
for these arguments. *)
+ val alias : string list -> info -> bool t
+ (** [alias l i] is a [flag i] except the arguments [l] are also parsed as
+ if they appeared in place of the option. *)
+
+ val alias_opt : (string -> string list) -> info -> bool t
+ (** [alias l i] is a [flag i] except the arguments [l arg] are also parsed as
+ if they appeared in place of the option. [arg] is the possible argument
+ given on the command line *)
+
val opt : ?vopt:'a -> 'a conv -> 'a -> info -> 'a t
(** [opt vopt c v i] is an ['a] argument defined by the value of
an optional argument that may appear {e at most} once on the command
diff --git a/src/cmdliner_arg.ml b/src/cmdliner_arg.ml
index 56d1fb6..f32a36d 100644
--- a/src/cmdliner_arg.ml
+++ b/src/cmdliner_arg.ml
@@ -96,6 +96,35 @@ let list_to_args f l =
let add acc v = Cmdliner_info.Arg.Set.add (f v) acc in
List.fold_left add Cmdliner_info.Arg.Set.empty l
+let alias_opt aliases a =
+ let a = Cmdliner_info.Arg.make_opt ~absent:Err ~kind:Opt a in
+ let aliases = (fun f -> function
+ | None -> Error (Cmdliner_msg.err_opt_value_missing f)
+ | Some o -> Ok (aliases o)) in
+ let a = Cmdliner_info.Arg.aliases ~aliases a in
+ if Cmdliner_info.Arg.is_pos a then invalid_arg err_not_opt else
+ let convert ei cl = match Cmdliner_cline.opt_arg cl a with
+ | [] -> try_env ei a Cmdliner_base.env_bool_parse ~absent:false
+ | [_, _, None] -> Ok true
+ | [_, f, Some v] -> Ok true
+ | (_, f, _) :: (_ ,g, _) :: _ -> err (Cmdliner_msg.err_opt_repeated f g)
+ in
+ arg_to_args a, convert
+
+let alias aliases a =
+ let aliases = (fun f -> function
+ | Some v -> Error (Cmdliner_msg.err_flag_value f v)
+ | None -> Ok aliases) in
+ let a = Cmdliner_info.Arg.aliases ~aliases a in
+ if Cmdliner_info.Arg.is_pos a then invalid_arg err_not_opt else
+ let convert ei cl = match Cmdliner_cline.opt_arg cl a with
+ | [] -> try_env ei a Cmdliner_base.env_bool_parse ~absent:false
+ | [_, _, None] -> Ok true
+ | [_, f, Some v] -> err (Cmdliner_msg.err_flag_value f v)
+ | (_, f, _) :: (_ ,g, _) :: _ -> err (Cmdliner_msg.err_opt_repeated f g)
+ in
+ arg_to_args a, convert
+
let flag a =
if Cmdliner_info.Arg.is_pos a then invalid_arg err_not_opt else
let convert ei cl = match Cmdliner_cline.opt_arg cl a with
diff --git a/src/cmdliner_arg.mli b/src/cmdliner_arg.mli
index e3faa2f..4375b41 100644
--- a/src/cmdliner_arg.mli
+++ b/src/cmdliner_arg.mli
@@ -45,6 +45,8 @@ val flag : info -> bool t
val flag_all : info -> bool list t
val vflag : 'a -> ('a * info) list -> 'a t
val vflag_all : 'a list -> ('a * info) list -> 'a list t
+val alias : string list -> info -> bool t
+val alias_opt : (string -> string list) -> info -> bool t
val opt : ?vopt:'a -> 'a converter -> 'a -> info -> 'a t
val opt_all : ?vopt:'a -> 'a converter -> 'a list -> info -> 'a list t
diff --git a/src/cmdliner_cline.ml b/src/cmdliner_cline.ml
index 5bff9d8..2b8108c 100644
--- a/src/cmdliner_cline.ml
+++ b/src/cmdliner_cline.ml
@@ -110,6 +110,11 @@ let parse_opt_args ~peek_opts optidx cl args =
| v :: rest -> if is_opt v then None, args else Some v, rest
in
let arg = O ((k, name, value) :: opt_arg cl a) in
+ let errs,args =
+ match Cmdliner_info.Arg.alias a name value with
+ | Ok l -> errs,l@args
+ | Error err -> err::errs,args
+ in
loop errs (k + 1) (Amap.add a arg cl) pargs args
| `Not_found when peek_opts -> loop errs (k + 1) cl pargs args
| `Not_found ->
diff --git a/src/cmdliner_info.ml b/src/cmdliner_info.ml
index d516fc1..87dec76 100644
--- a/src/cmdliner_info.ml
+++ b/src/cmdliner_info.ml
@@ -92,7 +92,10 @@ module Arg = struct
pos : pos_kind; (* positional arg kind. *)
opt_kind : opt_kind; (* optional arg kind. *)
opt_names : string list; (* names (for opt args). *)
- opt_all : bool; } (* repeatable (for opt args). *)
+ opt_all : bool; (* repeatable (for opt args). *)
+ opt_alias: string -> string option -> (string list, string) Result.t; (* [opt_alias arg value], [arg] is the name of the argument,
+ and [value] is the possible value *)
+ }
let dumb_pos = pos ~rev:false ~start:(-1) ~len:None
@@ -108,7 +111,8 @@ module Arg = struct
in
{ id = Cmdliner_base.uid (); deprecated; absent = Doc absent;
env; doc; docv; docs; pos = dumb_pos; opt_kind = Flag; opt_names;
- opt_all = false; }
+ opt_all = false;
+ opt_alias = fun _ _ -> Ok [] }
let id a = a.id
let deprecated a = a.deprecated
@@ -129,6 +133,7 @@ module Arg = struct
| n :: ns -> if (String.length n) > 2 then n else find ns
in
find a.opt_names
+ let alias a = a.opt_alias
let make_req a = { a with absent = Err }
let make_all_opts a = { a with opt_all = true }
@@ -138,6 +143,7 @@ module Arg = struct
let make_pos ~pos a = { a with pos }
let make_pos_abs ~absent ~pos a = { a with absent; pos }
+ let aliases ~aliases a = { a with opt_alias = aliases }
let is_opt a = a.opt_names <> []
let is_pos a = a.opt_names = []
diff --git a/src/cmdliner_info.mli b/src/cmdliner_info.mli
index d82b03c..2b995a2 100644
--- a/src/cmdliner_info.mli
+++ b/src/cmdliner_info.mli
@@ -77,6 +77,7 @@ module Arg : sig
val opt_name_sample : t -> string (* warning must be an opt arg *)
val opt_kind : t -> opt_kind
val pos_kind : t -> pos_kind
+ val alias : t -> string -> string option -> (string list, string) Result.t
val make_req : t -> t
val make_all_opts : t -> t
@@ -84,6 +85,7 @@ module Arg : sig
val make_opt_all : absent:absence -> kind:opt_kind -> t -> t
val make_pos : pos:pos_kind -> t -> t
val make_pos_abs : absent:absence -> pos:pos_kind -> t -> t
+ val aliases : aliases:(string -> string option -> (string list, string) Result.t) -> t -> t
val is_opt : t -> bool
val is_pos : t -> bool
diff --git a/src/cmdliner_manpage.ml b/src/cmdliner_manpage.ml
index 46353d5..699564c 100644
--- a/src/cmdliner_manpage.ml
+++ b/src/cmdliner_manpage.ml
@@ -22,6 +22,7 @@ let s_name = "NAME"
let s_synopsis = "SYNOPSIS"
let s_description = "DESCRIPTION"
let s_commands = "COMMANDS"
+let s_command_aliases = "COMMAND ALIASES"
let s_arguments = "ARGUMENTS"
let s_options = "OPTIONS"
let s_common_options = "COMMON OPTIONS"
|
|
@emillon Did you have to patch upstream to get the full command to display? |
Author
|
@Alizter I submitted dbuenzli#162 with this. |
- keep alias support - remove group support now that Cmd exists Signed-off-by: Etienne Millon <me@emillon.org>
Signed-off-by: Etienne Millon <me@emillon.org>
Signed-off-by: Etienne Millon <me@emillon.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.