| Config-Tree documentation | view source | Contained in the Config-Tree distribution. |
Config::Tree::CmdLine - Read configuration tree from command line options
# READING CONFIG FROM COMMAND LINE
# in shell:
% perl script.pl --foo/bar=3
% perl script.pl --foo='{bar: 3}'; # same thing
% perl script.pl '{bar: 3}'; # same thing, since ui.order of foo is 0
# in script.pl:
use Config::Tree::CmdLine;
my $conf = Config::Tree::CmdLine->new(
schema => [hash=>{keys=>{
foo=>[hash=>{ keys=>{bar=>"int"}, "ui.order"=>0, "ui.description"=>"Foo is blah" }],
baz=>[str=>{ "ui.order"=>1, "ui.description"=>"Baz is blah..." }],
}}],
# when_invalid => ...,
# include_path_re => qr/.../,
# exclude_path_re => qr/.../,
# must_exist => 0|1,
# special_options => {...},
ro => 0,
);
my $val = $conf->get('/foo/bar'); # 3
$conf->cd('/foo');
$conf->set('bar', 10); # same as set('/foo/bar', 10);
# DISPLAYING HELP
# in shell:
% perl script.pl --help; # will display help using information from schema
Construct a new Config::Tree::CmdLine object. Arguments.
ro. Optional, default is 0. Whether we should disallow set() and save(). when_invalid. Optional, default is 'die'. What to do when a command line
option is unknown in schema or does not validate schema. Choices: 'die', 'warn',
'quiet'. Will do nothing if no schema is supplied. exclude_path_re. Optional. When set, config path matching the regex will not
be retrieved. See also: include_path_re. include_path_re. Optional. When set, only config path matching the regex will
be retrieved. Takes precedence over exclude_path_re. schema. Optional. When specified, after the tree is retrieved, it will be
validated against this schema using Data::Schema. special_options. Optional. Normally each command line option will be added to
the config tree. However, if a hashref is supplied for this property, and an
option name matches the key in the hashref, then the supplied special
instructions will be used. This is used for example to display help/usage, add
synonyms, etc.
special_options is:
{help => { schema=>'bool', sub=>{$self->usage(); exit 0} }
sub will be called with option value as the first argument and
$self as the second argument, and should return nothing or a hashref containing
option names and values which will be added to config tree. Another example for
special option:
{
help => ...,
debug => { schema=>'bool', sub=>sub {{log_level=>"debug"}} }
verbose => { schema=>'bool', sub=>sub {{log_level=>"info"}} }
quiet => { schema=>'bool', sub=>sub {{log_level=>"error"}} }
}
short_options. Optional. A hashref which map letter to long name equivalent.
Example:
{h => "help", d => "debug", v => "verbose"}
stop_after_first_arg. Optional. Default is 1. If enabled, then command line
options processing will stop as soon as first non-option (i.e., argument) is
encountered. Under stop_after_first_arg=1:
% script.pl --foo 1 3 --bar 2
argv. Optional, an arrayref. Instead of the default @ARGV, process command
line on this array instead.Prints usage information. Requires schema be specified.
Does nothing.
Does nothing.
Steven Haryanto, <stevenharyanto at gmail.com>
Copyright 2009 Steven Haryanto, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Config-Tree documentation | view source | Contained in the Config-Tree distribution. |