From: Ben Millwood <bmillwood@janestreet.com>
To: leowzukw@vmail.me
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Using common arguments with Command module of the Core library
Date: Mon, 3 Aug 2015 17:53:53 +0100 [thread overview]
Message-ID: <CA+MHO52ibTc=5KD=UJ=UZJwMcWm5_wiPekmJyp041K8kvSwNvA@mail.gmail.com> (raw)
In-Reply-To: <f87d8e3d80aba649d4a7320fc1e54850@vmail.me>
[-- Attachment #1: Type: text/plain, Size: 2184 bytes --]
There are a few ways of doing this. The newly-added Applicative interface
is worth showing off:
let shared_params =
let open Command.Param in
return (fun v c rcfile ->
verb := v;
color := c;
rcfile)
<*> flag "-v" (optional_with_default 0 int) ~doc:"..."
<*> flag "-c" (...) ~doc:"..."
<*> ...
shared_params is now a package of arguments that sets the verb and color
refs and then gives you the rcfile (it's only that way to mirror your code;
I'd usually prefer to just put all three in a record or something). You can
use it in Command.basic as follows:
let sum =
Command.basic
~summary:"whatever"
Command.Spec.(
empty
+> shared_params
+> anon ...)
(fun rc ... () -> ...)
let main () =
Command.run begin
Command.group ~summary:"some stuff"
[ "sum", sum
; "settings", ...
]
end
Let me know if I've omitted too much :)
On 3 August 2015 at 13:18, <leowzukw@vmail.me> wrote:
> Hello everybody,
>
> I'm trying to use subcommands with the Core library. I would like to
> factorize several things :
>
> - A set of common arguments (to set color, verbosity and so on)
> - A common way to deal with these arguments
> - A pretty way to define subcommands using these common arguments and
> way to handle it.
>
>
>
> Main problem is that I can't define more arguments to my subcommand. More
> details in my code, which is joined. You may also find it here (Github
> gist, could be updated): http://j.mp/1JHlZ6S
>
>
>
> Result of the compilation (corebuild cmd.byte)
>
>
> + ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g
> -bin-annot -short-paths -thread -syntax camlp4o -package bin_prot.syntax
> -package
> sexplib.syntax,comparelib.syntax,fieldslib.syntax,variantslib.syntax
> -package core -o cmd.cmo cmd.ml
> File "cmd.ml", line 98, characters 5-44:
> Warning 48: implicit elimination of optional argument ?extend
> File "cmd.ml", line 49, characters 11-341:
> Warning 27: unused variable summary.
> File "cmd.ml", line 49, characters 20-341:
> Warning 27: unused variable args.
> Finished, 3 targets (0 cached) in 00:00:03.
>
>
> Thanks for your help
> Leo
>
>
[-- Attachment #2: Type: text/html, Size: 4322 bytes --]
next prev parent reply other threads:[~2015-08-03 16:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-03 12:18 leowzukw
2015-08-03 16:53 ` Ben Millwood [this message]
[not found] ` <05d254b1bbdff3c0030bcc8b85e1c951@vmail.me>
[not found] ` <CA+MHO52qGd9CEwwq7pWGQDr9b0r=7niLUBk2qT0yOkSaNRCvSw@mail.gmail.com>
2015-08-04 17:25 ` leowzukw
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CA+MHO52ibTc=5KD=UJ=UZJwMcWm5_wiPekmJyp041K8kvSwNvA@mail.gmail.com' \
--to=bmillwood@janestreet.com \
--cc=caml-list@inria.fr \
--cc=leowzukw@vmail.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox