I understand other people have written those things before, and that it's probably not so challenging to someone else, and I'm not saying I can't or wouldn't write it, but I'm under deadline pressure, and I think it would not be looked up on well if I had something with anywhere near so many features or as much work to deliver since if they even exist. Since I have to demonstrate this, the first question they are going to ask is "you spent more than X minutes working on this when you could have been working on the minimum viable product!! Unhappy!!" So I'm not disregarding the input I've got, but I think that I can achieve a less robust working version with the same set of features in a simpler fashion.

So, instead I think can get something very near to a full grammar, while still allowing the fundamental operations I want. Here's what I've got:


type setop =
  | Intersection
  | Difference
  | Union
[@@deriving sexp]

let list_setops = [
    "Intersection", Intersection;
    "Difference", Difference;
    "Union", Union;
]
let setops_doc = List.(to_string ~f:fst (list_setops))
let setops =
  let doc = "." in
  Cmdliner.Arg.(
    value & opt_all (some (pair ~sep:'=' string & pair (enum (list_setops)) & pair string string)) []
    & info ["setop"] ~docv:setops_doc ~doc
  )


Instead of having an recursive variant instance in the type setop place to allow the grammar to be recursive, I will fold over the setops, and add each one to a map. For example, I might have:

--setop Red=Union (Feature1, Feature2) --setop Green=Intersection (Red, Feature3)

So that, as I fold, I will add colors to the feature set. Then, for whatever nested operations otherwise would have been required, I can just manually unfold them on the command line. 

I guess I've solved my problem, but I was hoping to get a recursive parsing capability on the command line that would have supporting a type declaration more like the following:

type setop =
  | Result of setop
  | Intersection of string * string
  | Difference of string * string
  | Union of string * string

The problem with this is, 1) the constructors are non-uniform so that there isn't a clean way to specify to the Cmdliner.Arg.value function what the converter should be 2) The list type of their resulting pairwise sub-command specifications to the command line (the "enum list_setops" part) becomes much harder to specify since those also need to be constructible in the string - type pairs for the list_setops argument to enum. 

I suppose my thinking about how to deal with this would be to write a custom conv to convert the command line input, but to do so it would have to be recursive, and the Cmdliner.Arg.enum would have to support both non-uniform constructors and an argument conv to be able to do this correctly.

Does anybody have a better way to capture what I'm looking to do? 

On Sat, Jul 28, 2018 at 10:54 AM Андрей Бергман <vkni@yandex.ru> wrote:
Probably a parser combinator with a small language would be a better tool for that. Parser generators look too heavy, and comman-line parsers are too light (otherwise they become optparse-applicative, which is too specific to study it => everyone uses cookbook).