Command-line switches and other configuration options
There are many CPAN modules for application configuration. I picked
AppConfig because I had experience with it, and because it could mimic the
behavior of cfengine's configuration switches.
Each switch is given an alias. For instance,
equivalent, which necessitated using
-v as the shortcut for
could have used case sensitivity to distinguish between them as well.
-h) and input (
-i) are options without arguments. Debug (
-f, named fileread because
AppConfig has an internal
method), and exec (
-e) are one-argument options. Define (
-D) is the only
list option, so the user can say "
-D Alpha -D Beta" and have both Alpha
and Beta defined.
I use the
AppConfig args() method to read the arguments from @ARGV.
Things get a little tricky when the main loop begins. We have to process
all the command-line switches. First we define all the classes given with
Now, the input file is processed. We define
$config_file to be a IO::File
object (a much more reliable way to process file data than the classic
Perl FILE handles, for many reasons). If the
-f option specified a
readable file, we set
$config_file to be that file. Else, if the
-i option was given,
$config_file is set to be the standard input of
cfperl. Else, if the
-e option was given, we just process that option's
argument, do a quick
cfrun() and exit. Finally, if all else failed, we
GLOBAL_CONFIG_FILE defined in the constants section.
The complex options available to cfperl users mimic the options available with cfengine, but they are not the same options and they shouldn't have to be. cfperl does not aim to replace cfengine, only to add functionality to it. Duplicating all of cfengine's behavior and command-line switches would be both unnecessary and difficult.Listing 9. cfperl command-line switches processing: the file processing
Next time, we will finish the code for cfperl -- see you then.