in reply to Smart way to deal with cmdline Args?
Just an idea, but getOpt::Long normally takes a hash reference as the first argument, and that hash can contain default values before the options, but it should be possible to populate that hash from a config file.
The normal way I use getOpt::Long is with code like this.
# Setup the raw options from the system defaults. my $raw_opts; $raw_opts->{'help'} = sub{ pod2usage( -verbose => 0 ) }; $raw_opts->{'man'} = sub{ pod2usage( -verbose => 2, -exitstatus => + 0 ) }; $raw_opts->{'verbose'} = sub{ $logger->more_logging(1) }; $raw_opts->{'quiet'} = sub{ $logger->level($FATAL) }; # Use the store in hash form op GetOptions, with references to excepti +ons setup above. GetOptions( $raw_opts, 'help|h', 'man', 'verbose+', 'quiet', 'username=s', 'password=s', 'password_file=s', # Other options. ) or pod2usage( -verbose => 0, -message => "incorect options" ); # Check the options. # Rest of the program.
I am thinking that a simple way to add support for config files would be that after the config hash has been declared and the defaults loaded, you could load config settings from something like Config::Trivial. After the command line settings have been parsed and checked for correctness, you could then store them again.
I have done this sort of saving and restoring of configuration preferences using this patten in the past, however, I have always ended up modifying it into something more complex, as the needs of the script have increased.
|
|---|