
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 France License.
I've already wrote something about OptParse last month. Today I discovered how to create a new option (that is not a string, int or bool) and validate it within the arg parser.
So suppose we want to write an application that can output both txt and html and we want the user to specify the format with command line option. One way would be to use a StdOpt.str_option - eventually with a default option - and to retrive it in application code with OptParse.Opt.get.
However this is not satisfactory as we are mixing the application code with command line parsing. A better way is to create a new type of option with Opt.value_option .
This is the concept :
Note that the function Opt.value_option get a default value, a metavar - that is the sting associated with the option in the help ( -o<txt|html>, --out=<txt|html> Output type ), a corce function, that is, a function that transforms a string in the desired type, and an error function that is used by the parser to give a meaningful error is the option is not correctly validated.
For example :
Now when we use this new option in the application code with OptParse.Opt.get and we can be certain that it was correctly validated.
Recent comments
18 weeks 1 day ago
1 year 18 weeks ago
1 year 22 weeks ago
1 year 35 weeks ago
1 year 37 weeks ago
1 year 39 weeks ago
1 year 42 weeks ago
1 year 43 weeks ago
2 years 20 weeks ago
2 years 24 weeks ago