[ANN] Clap 0.1.0 (Command-Line Argument Parsing)

I am happy to announce the first release of Clap.

Clap is a library for command-line argument parsing. Clap works by directly consuming arguments in an imperative way. Traditionally, argument parsing in OCaml is done by first defining a specification (an OCaml value defining the types of arguments), and then parsing from this specification. The “impure” approach of Clap skips the need to define a specification and results in code which is quite simple in practice, with limited boilerplate.

Clap is available as an opam package (opam install clap).

Source code, API documentation and a full commented example are available at: https://github.com/rbardou/clap/


The description looks like my minicli library (https://github.com/UnixJunkie/minicli).
Did you know about it?


I did not know about minicli, thanks for the link!

Clap and minicli indeed share the same approach. Here are some differences I observed.

  • In minicli, the list of arguments is immutable; arguments are not consumed. It is “more pure”.

  • As a consequence, minicli does not seem to support unnamed arguments (also known as positional arguments).

  • As another consequence, in minicli each function takes the list of arguments, which makes it a bit more flexible at the cost of being slightly more verbose.

  • In minicli there is a hash table storing the list of seen arguments. It looks like this means that the immutable list of arguments must thus always be the same if you want to call finalize, which relies on this invariant.

  • minicli does not generate a --help (but it would be possible rather easily).

  • minicli considers that negative numbers are not option names, and is thus capable of handling them more naturally (in Clap you would need to escape the dash).

  • I think that minicli does not consider “-abc” to be equivalent to “-a -b -c”, whereas Clap differentiates short names (starting with a single dash and which can be factorize like that) and long names (starting with two dashes).

  • minicli’s source code is significantly shorter and thus simpler and easier to maintain.


Ok, now I see that you really now about it! :slight_smile:

Thanks, you got my style of programming. :slight_smile:

1 Like

Another killer feature of minicli: the various options that we can throw at the user:

exception Minicli.CLI.Not_an_int string                                      
exception Minicli.CLI.Not_a_string string                                    
exception Minicli.CLI.Not_a_float string                                     
exception Minicli.CLI.Not_a_bool string                                      
exception Minicli.CLI.No_param_for_option string                             
exception Minicli.CLI.More_than_once string                                  
exception Minicli.CLI.Option_is_mandatory string                             
exception Minicli.CLI.Duplicate_in_specification string                      
exception Minicli.CLI.Unused_options string             


1 Like

On another topic, maybe you will be interested by parany, if you don’t already know about it. :wink:

1 Like