D Configuration Framework (alias Program Options)

Marcin Kuszczak aarti_nospam at please_interia.pl
Mon Oct 8 15:05:57 PDT 2007


Hello all D fans!

I would like to announce completely rewritten and reworked program options
library. This is beta release, but should be rather stable for everyday
use. You can find source code and download package on project page:
http://www.dsource.org/projects/doost/wiki

----

Program options library is intended to make managing of configuration in D
programs easy. Instead of reinventing the wheel and implementing same parts
of program to manage configuration over and over again, it is possible now
to use library with simple API for common tasks. Common tasks with managing
program configuration include: reading configuration options from physicall
media, saving them back, presenting description of available options to
user, checking for incorrect usage, working with different sources of
options, checking option values for validity, synchronizing with physicall
media etc.

Main goal for this release was to make library usage very easy for common
use cases. Other goal was to make it easy to implement new physicall
backends for storing options and also to make source code and usage of
library more D-like.

To achieve above goals I changed architecture of previous version, so it is
currently based on concept of root access class to options (ProgramOptions
class) and different classes which define specific backends (e.g.
CommandLineStorage, ConfigFileStorage). Standard way to access options is
through root class (ProgramOptions), which is searching then for concrete
options in stacked physicall storages. From library user point of view
interface to options is similar to interface of associative arrays.

New library has gain several abilities which were not available in original:
        * possibility to store options in backends
        * available storages: CommandLineStorage, ConfigFileStorage, 
          EnvironmentStorage, DbStorage (using ddbi)
        * interfacing to whole stack (ProgramOptions) and every concrete storage
        * easy method to add new backends using [read|save]Physically[One|All] 
          methods
        * stacking of options backends
        * removed possibility to store values into concrete variables (it was 
          insecure)
        * notification about option changes (it is possible to register for 
          specific events)
        * easy synchronization of options with backends using synchronize() method
        * different synchronization methods: direct or cached
        * interface to stack of options similar to associative arrays
        * possibility to have many stacks of options in program (ProgramOptions is 
          not singleton)
        * custom file loaders and savers with simple ones set by default
        * possibility to compose one option which is defined in different storages 
          into one value
        * generalization of short option, which now is just alias for normal option
        * reading/storing lists in  backends (with user defined separators)
        * support for escape sequences
        * easy initialization of options stack, with no temporary variables
        * checking for options integrity (not possible to define same option twice 
          in storage, option type must be same in whole stack)
        * Tango-like structure of directories + naming convention similar to other 
          D programs
        * built-in regular expressions for option values
        * user defined formatters for options
        * special types of options: SelfName, SelfDir, SelfPath, RegExpOptions and 
          possibility to create user defined options
        * reworked positional options in CommandLineStorage (currently it is 
          special type option)
        * built-in response files support for CommandLineStorage
        * functions for checking for obligatory options, conflicting options and 
          dependant options
        * comments for DDoc
        * unit tests
        * many smaller improvements and bugfixes

Below you can find simplest usage example for command line:

----------8<----------

import doost.util.config.ProgramOptions;
import doost.util.config.CommandLineStorage;

void main(char[][] args) {
  //Definition of simplest options stack
  auto po = (new ProgramOptions)
                .next(
            (new CommandLineStorage(args))
                .caption("Command line options")
                .options()
                  ("help,h", "produce help message")
                  ("compression", define!(int), "set compression level")
                  ("sync", "stores cmdline options in persistent backend")
                  ()
            );

  //Conecting to storages
  po.connect();

  //Reading options
  if ("help" in po) writefln(po);
  if ("compression" in po)      
        writefln("Compresson level set to: ", po["compression"].as!(uint));

  //Disconnecting storages
  po.disconnect;
}
 
---------->8----------
 
*A lot of different examples for different storages you can find in file:
examples/util/config/FunctionTest.d.*

Currently there are following backends defined for program options:
CommandLineStorage      -       handles command line options
ConfigFileStorage       -       handles options from ini-like files
EnvironmentStorage      -       handles options from environment variables
DbStorage               -       handles options from database through ddbi interface

-------

Although it is beta release (so architecture of library is considered to be
finished) I am open to discuss and eventually change parts of program in
case there are good arguments supporting changes. 

I will be happy to get feedback from you. You can find few things to
consider in file doc/todo.txt. You can also write other physicall backends
to library. If you want I can host them in doost svn.

Also as I am not native English-speaker, I would appreciate help with
improving documentation and corrections for misspelled words. You can send
patches or just send me an e-mail.

I hope anyway you will like what you get :-) Have a fun!

-- 
Regards
Marcin Kuszczak (Aarti_pl)
-------------------------------------
Ask me why... I believe in Jesus - http://www.zapytajmnie.com (en/pl)
Doost (port of few Boost libraries) - http://www.dsource.org/projects/doost/
-------------------------------------




More information about the Digitalmars-d-announce mailing list