caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Using common arguments with Command module of the Core library
@ 2015-08-03 12:18 leowzukw
  2015-08-03 16:53 ` Ben Millwood
  0 siblings, 1 reply; 3+ messages in thread
From: leowzukw @ 2015-08-03 12:18 UTC (permalink / raw)
  To: caml-list


[-- Attachment #1.1: Type: text/plain, Size: 1194 bytes --]

 

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 [1] 

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 
 

Links:
------
[1] http://j.mp/1JHlZ6S

[-- Attachment #1.2: Type: text/html, Size: 2464 bytes --]

[-- Attachment #2: cmd.ml --]
[-- Type: text/plain, Size: 2525 bytes --]

(* Build with:
  * corebuild cmd.byte *)


open Core.Std;;

(* With this code, we are trying to
 * Define a common set of arguments to be passed to all sub commands
 * Handle these common arguments all the same way
 * Define sub commands in a common, less verbose way *)

(* The program compiled could be used this way
 * cmd.byte sum 32 + 10 # Display 42
 * cmd.byte settings # Display all the settings
 * But we could use common arguments :
 * cmd.byte sum -c 32 + 10 # Display 42 and a message about color
 * cmd.byte settings # Display all the settings
 * *)

(* Problems:
  * Arguments from other commands are not passed to the functions
  * Several XXX below *)

(* Verbosity *)
let verb = ref 0;;
let color = ref false;;

(* A set of common flags *)
let common =
  let open Command.Spec in
  empty
  +> flag "-v" (optional_with_default 0 int)
    ~doc:"n Set verbosity"
  +> flag "-c" (optional_with_default false bool)
    ~doc:"bool Set color"
  +> flag "--rc" (optional_with_default "" string)
    ~doc:"name Set configuration file"
;;

(* Treating common args *)
let parse_common ~f = fun verbosity col rc ->
  verb := verbosity;
  color := col;

  f ~rc
;;

(* Common way to define subcommands *)
let sub ~f ~summary ~args name =
  let open Command in
  let def =
    basic ~summary:"" Spec.(common +> args) (* XXX Couldn't set set summary with
    the argument ~summary of sub function. Error is
      This expression has type t -> bytes
      but an expression was expected of type bytes*)
      (parse_common ~f)
  in
    ( name, def )
;;

(* Two sub commands *)

(* Display the sum of the arguments *)
let sum =
  sub
    ~f:(fun ~rc _ () -> (* XXX Strange arguments passed here *)
      (* We would like to get the numbers passed in arguments ("first number"
       * and "second number" below) *)
      ();
      (* Some code to use common arguments *)
      if !color then printf "Colored\n" else print_endline rc)
    ~args:(Command.Spec.(
      empty
      +> anon ("first number" %: int)
      +> anon ("second number" %: int)
      ))
    ~summary:"Sum"
    "sum"
;;

(* Print some settings and a number passed to the program *)
let settings =
  sub
    ~f:(fun ~rc _ () ->
      printf "Settings\n";
      printf "Rc: %s" rc;
      printf "Color: %b" !color)
    ~args:(let open Command.Spec in
      empty
      )
    ~summary:"Display settings"
    "settings"
;;

let () =
  let open Command in
  group ~summary:"A program to test" [ sum ; settings ]
  |> run ~version:"" ~build_info:"" ~argv:[]
;;

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Using common arguments with Command module of the Core library
  2015-08-03 12:18 [Caml-list] Using common arguments with Command module of the Core library leowzukw
@ 2015-08-03 16:53 ` Ben Millwood
       [not found]   ` <05d254b1bbdff3c0030bcc8b85e1c951@vmail.me>
  0 siblings, 1 reply; 3+ messages in thread
From: Ben Millwood @ 2015-08-03 16:53 UTC (permalink / raw)
  To: leowzukw; +Cc: caml users

[-- 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 --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Using common arguments with Command module of the Core library
       [not found]     ` <CA+MHO52qGd9CEwwq7pWGQDr9b0r=7niLUBk2qT0yOkSaNRCvSw@mail.gmail.com>
@ 2015-08-04 17:25       ` leowzukw
  0 siblings, 0 replies; 3+ messages in thread
From: leowzukw @ 2015-08-04 17:25 UTC (permalink / raw)
  To: Ben Millwood; +Cc: caml-list


[-- Attachment #1.1: Type: text/plain, Size: 3297 bytes --]

 

I installed 112.35.01 and now it works perfectly, both the installation
and your solution. 

I join the working code for mailing list. I also updated the gist. 

Thanks a lot for your help 

Leo 

Le 2015-08-04 10:34, Ben Millwood a écrit : 

> Make sure you are installing core 112.35.01, not 112.35.00. I believe it contains a fix for the issue you're seeing. If it doesn't, let me know and I'll look into it a little more. 
> 
> On 3 August 2015 at 19:54, <leowzukw@vmail.me> wrote:
> 
> Thanks for your quick answer. It seems clear enough. 
> 
> Unfortunately, I can't install latest version of core, I get this error 
> 
> # File "src/iobuf.ml [1]", line 624, characters 17-28:
> # Integer literal exceeds the range of representable integers of type int
> 
> I think it's because of my 32bit computer. I will keep your solution in mind until I could get the latest version of core. 
> 
> Leo 
> 
> PS: Should I report an issue on Github for that ? In opam or core repository ? 
> 
> Le 2015-08-03 18:53, Ben Millwood a écrit : 
> 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 [2] 
> 
> 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 [3]
> File "cmd.ml [3]", line 98, characters 5-44:
> Warning 48: implicit elimination of optional argument ?extend
> File "cmd.ml [3]", line 49, characters 11-341:
> Warning 27: unused variable summary.
> File "cmd.ml [3]", line 49, characters 20-341:
> Warning 27: unused variable args.
> Finished, 3 targets (0 cached) in 00:00:03. 
> 
> Thanks for your help
> Leo

 

Links:
------
[1] http://iobuf.ml
[2] http://j.mp/1JHlZ6S
[3] http://cmd.ml

[-- Attachment #1.2: Type: text/html, Size: 6843 bytes --]

[-- Attachment #2: cmd.ml --]
[-- Type: text/plain, Size: 2205 bytes --]

(* Build with:
  * corebuild cmd.byte *)


open Core.Std;;

(* With this code, we are trying to
 * Define a common set of arguments to be passed to all sub commands
 * Handle these common arguments all the same way
 * Define sub commands in a common, less verbose way *)

(* The program compiled could be used this way
 * cmd.byte sum 32 + 10 # Display 42
 * cmd.byte settings # Display all the settings
 * But we could use common arguments :
 * cmd.byte sum -c true 32 + 10 # Display 42 and a message about color
 * cmd.byte settings # Display all the settings
 * *)

(* Verbosity *)
let verb = ref 0;;
let color = ref false;;

(* A set of common flags *)
let shared_params =
  let open Command.Param in
  return (fun v c rc ->
    verb := v;
    color := c;
    rc)
  <*> flag "-v" (optional_with_default 0 int)
    ~doc:"n Set verbosity"
  <*> flag "-c" (optional_with_default false bool)
    ~doc:"bool Set color"
  <*> flag "--rc" (optional_with_default "" string)
    ~doc:"name Set configuration file"
;;

(* Two sub commands *)

(* Display the sum of the arguments *)
let sum =
  (
      "sum"
      ,
    Command.basic ~summary:""
      Command.Spec.(
        empty
        +> shared_params
        +> anon ("first_number" %: int)
        +> anon ("second_number" %: int)
      )
      (fun rc a b () -> (* XXX Strange arguments passed here *)
        (* We would like to get the numbers passed in arguments ("first
         * number"
         * and "second number" below) *)
        a + b |> printf "%i\n";
        (* Some code to use common arguments *)
        if !color then printf "Colored\n" else
          print_endline rc)
  )
;;

(* Print some settings and a number passed to the program *)
let settings =
  (
     "settings"
     ,
     Command.basic ~summary:"Display settings"
      Command.Spec.(
         empty
         +> shared_params
         +> anon (maybe ("a number" %: int))
      )
      (fun rc n () ->
        printf "n: %i\n" (Option.value ~default:0 n);
        printf "\nSettings\n";
        printf "Rc: %s\n" rc;
        printf "Color: %b\n" !color)
  )
;;

let () =
  let open Command in
  run begin
    group ~summary:"A program to test" [ sum ; settings ]
  end
;;

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-08-04 17:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-03 12:18 [Caml-list] Using common arguments with Command module of the Core library leowzukw
2015-08-03 16:53 ` Ben Millwood
     [not found]   ` <05d254b1bbdff3c0030bcc8b85e1c951@vmail.me>
     [not found]     ` <CA+MHO52qGd9CEwwq7pWGQDr9b0r=7niLUBk2qT0yOkSaNRCvSw@mail.gmail.com>
2015-08-04 17:25       ` leowzukw

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).