use PDL::Options;
$hashref = parse( \%defaults, \%user_options);
use PDL::Options ();
$opt = new PDL::Options; $opt = new PDL::Options ( \%defaults );
$opt->defaults ( \%defaults ); $opt->synonyms ( { 'COLOR' => 'COLOUR' } );
$hashref = $opt->defaults;
$opt->options ( \%user_options );
$hashref = $opt->options;
$opt->incremental(1); $opt->full_options(0);
A hash (not hash reference) containing the processed options is returned.
%options = parse( { LINE => 1, COLOUR => 'red'}, { COLOR => 'blue'});
The current values are reset whenever the defaults are changed.
This allows you to provide alternate keywords (such as allowing 'COLOR' as an option when your defaults uses 'COLOUR').
full_options()
is true the full options hash is
returned, if full_options()
is false only the modified options
are returned (as set by the last call to options()).
This method can be used to setup the dictionary and is hash reference with the following structure:
OPTIONA => { 'string1' => decode1, 'string2' => decode2 }, OPTIONB => { 's4' => decodeb1, } etc....
Where OPTION? corresponds to the top level option name as stored in the defaults array (eg LINECOLOR) and the anonymous hashes provide the translation from string1 ('red') to decode1 ('#ff0000').
An options string will be translated automatically during the main
options()
processing if autotrans()
is set to
true. Else translation can be initiated by the user using the
translate()
method.
Can be used to set or return this value. Default is false.
This can be useful when you are only interested in the changes to the options rather than knowing the full state. (For example, if defaults contains keys for COLOUR and LINESTYLE and the user supplied a key of COL, you may simply be interested in the modification to COLOUR rather than the state of LINESTYLE and COLOUR.)
Default is true.
Can be used to set or return this value.
If a particular key matches exactly (within the constraints imposed bby case sensitivity) this key will always be taken as correct even if others are similar. For example COL would match COL and COLOUR but this implementation will always return COL in this case (note that for CO it will return both COL and COLOUR and pick one at random.
Can be used to set or return this value.
translate()
method immediately following the main options
parsing. Default is to autotranslate (1).
Can be used to set or return this value.
translation()
or not (1). Default is to be
case insensitive.
Can be used to set or return this value.
translation().
Default is false (0).
If a particular key matches exactly (within the constraints imposed bby case sensitivity) this key will always be taken as correct even if others are similar. For example COL would match COL and COLOUR but this implementation will always return COL in this case (note that for CO it will return both COL and COLOUR and pick one at random.
Can be used to set or return this value.
incremental()).
The user-supplied keys will be compared with the defaults. Case sensitivity
and minimum matching can be configured using the mimatch()
and
casesens()
methods.
A warning is raised if keys present in the user options are not present in the defaults.
A reference to a hash containing the merged options is returned.
$merged = $opt->options( { COL => 'red', Width => 1});
The state of the object can be retrieved after this by using the
current()
method or by using the options()
method
with no arguments. If full_options()
is true, all options are
returned (options plus overrides), if full_options()
is false
then only the modified options are returned.
Synonyms are supported if they have been configured via the
synonyms()
method.
options()
method) using the provided
translation().
This method updates the current state of the object and returns the updated options hash as a reference.
$ref = $opt->translate;
use PDL::Options (':Func');
%options = parse( { LINE => 1, COLOUR => 'red', }, { COLOR => 'blue' } );
This will return a hash containg
%options = ( LINE => 1, COLOUR => 'blue' )
use PDL::Options ();
# Create new object and supply defaults $opt = new PDL::Options( { Colour => 'red', LineStyle => 'dashed', LineWidth => 1 } );
# Create synonyms $opt->synonyms( { Color => 'Colour' } );
# Create translation dictionary $opt->translation( { Colour => { 'blue' => '#0000ff', 'red' => '#ff0000', 'green'=> '#00ff00' }, LineStyle => { 'solid' => 1, 'dashed' => 2, 'dotted' => 3 } } );
# Generate and parse test hash $options = $opt->options( { Color => 'green', lines => 'solid', } );
When this code is run, $options will be the reference to a hash containing the following:
Colour => '#00ff00', LineStyle => 1, LineWidth => 1
If full_options()
was set to false (0), $options would be a reference to a hash
containing:
Colour => '#00ff00', LineStyle => 1
Minimum matching and case insensitivity can be configured for both the initial parsing and for the subsequent translating. The translation can be turned off if not desired.
Currently synonyms are not available for the translation although this could be added quite simply.