digitalmars.D.announce - ProgramOptions ported from Boost
- Marcin Kuszczak (67/67) Dec 19 2006 Hello!
Hello! I have managed to port boost::program_options to D. INTRO: ------------------------------ Program Options allow you to manage program options in convenient and easy way. Advantages of using library over hand-written code: - it's easier. You can define your options in few lines, automatically have help for your options (nicely presented, and always up to date), easy handle short/long form of options, convert values to proper types etc. - better error reporting. Hand-written code usually don't pay too much attention to detect errors. - different sources of options. It is possible to parse command line, config file and environment variables, and use them in your program. DIFFERENCES FROM ORIGINAL ------------------------------ 1. By default whole array of argument strings should be send to command line parsers. Parsers know that first string is self binary name. You can tune this behaviour by using proper flags for command line parser. 2. You can use special syntax to extract information about self binary directory and self binary name Below is code sample with syntax and additional feature - extracting "selfdir" and "selfname": auto desc = new OptionsDescription("Allowed options"); desc.add_options() ("help", "produce help message") ("selfdir", value!(string)(), "selfname", value!(string)()) ; auto vm = new VariablesMap; store((new CommandLineParser(args)).options(desc).run(), vm); notify(vm); if ("help" in vm) { writefln("Usage: options_description [options]"); writefln(desc); writefln("Program directory: ", vm["selfdir"].as!(string)()); writefln("Program name: ", vm["selfname"].as!(string)()); return 0; } More examples in source code. PROBLEMS DURING PORTING TO D ------------------------------ I didn't manage to port only one example: regex.d The problem is that it's not easy to overload templates kept in library by user. Templates are not instantiated where they are used but in it's own module. This way user can not deliver his own template definition by himself... How to overcome this? Is there possible another solution in D? HOW TO GET D PROGRAM OPTIONS? ------------------------------ You can get it from: www.dsource.org/projects/doost/ You need at least DMD 0.177 to compile Any (due to opAssign and linking errors in earlier compilers). At the moment project is not visible at "Projects" page. Also forum doesn't work. You have to use above link directly. In SVN directory "downloads" is zip file with source files for ProgramOptions and Any. PLEASE REVIEW! ------------------------------ I would be happy to get some feedback from you about library. Reviews, comments and constructive critique is welcome! -- Regards Marcin Kuszczak (Aarti_pl) ------------------------------------- Ask me why I believe in Jesus http://zapytaj.dlajezusa.pl (en/pl) -------------------------------------
Dec 19 2006