[opendemo-cvs] CVS: opendemo/tools/odcut odcut.pl,1.46,1.47
Status: Beta
Brought to you by:
girlich
From: Uwe G. <gi...@us...> - 2004-10-31 20:36:16
|
Update of /cvsroot/opendemo/opendemo/tools/odcut In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15773 Modified Files: odcut.pl Log Message: Replace GetOpt::Long by AppConfig, which also replaces odconfig. Index: odcut.pl =================================================================== RCS file: /cvsroot/opendemo/opendemo/tools/odcut/odcut.pl,v retrieving revision 1.46 retrieving revision 1.47 diff -C2 -d -r1.46 -r1.47 *** odcut.pl 31 Oct 2004 15:27:01 -0000 1.46 --- odcut.pl 31 Oct 2004 20:36:06 -0000 1.47 *************** *** 12,25 **** ! # modules init use strict; - use Getopt::Long qw(:config no_ignore_case); use Pod::Usage; use Pod::Text; use Time::HiRes; use POSIX; ! # release information sub release() { (my $release = q$Revision$) =~ s/^[^:]+:\s*(.*?)\s*$/$1/; $release; } sub date() { (my $date = q$Date$) =~ s/^[^:]+:\s*(.*?)\s*$/$1/; $date; } --- 12,25 ---- ! # Module init. use strict; use Pod::Usage; use Pod::Text; use Time::HiRes; use POSIX; + use AppConfig qw(:argcount :expand); ! # Release information. sub release() { (my $release = q$Revision$) =~ s/^[^:]+:\s*(.*?)\s*$/$1/; $release; } sub date() { (my $date = q$Date$) =~ s/^[^:]+:\s*(.*?)\s*$/$1/; $date; } *************** *** 27,31 **** ! # prototypes sub logging; sub warning; --- 27,31 ---- ! # Prototypes. sub logging; sub warning; *************** *** 33,78 **** ! # main ! # default option values ! my $opt_version = 0; # version off ! my $opt_help = 0; # help off ! my $opt_man = 0; # manual off ! my $opt_logging = 0; # logging verbose level ! my $opt_warning = 0; # warning verbose level ! my $opt_verbose = 0; # verbose off ! my $opt_output = "odcut.od"; # default output file name ! my $opt_gui = 0; # GUI off ! my $opt_odfile = "parser"; # default file class: XML::Parser parser ! my $opt_config = ""; # default config file: no ! # parse command line ! GetOptions( ! 'version|V' => \$opt_version, ! 'help|?' => \$opt_help, ! 'man|m' => \$opt_man, ! 'logging|l=i' => \$opt_logging, ! 'warning|w=i' => \$opt_warning, ! 'output=s' => \$opt_output, ! 'gui' => \$opt_gui, ! 'file=s' => \$opt_odfile, ! 'config|c' => \$opt_config, ! ) or pod2usage(-msg=>"Syntax error", -verbose=>0); - pod2usage(-verbose=>0) if $opt_help; ! pod2usage(-verbose=>2) if $opt_man; ! if ($opt_version) { ! printf "ODcut %s, %s (%s)\n", release(), date(), comment(); exit(1); } ! # Read in configuration files. ! my $config = new odconfig; ! # $config->set("hallo","huhu"); ! # $config->readfile("out"); ! # $config->set("hallo2",$config->get("game_password") . "huhu2"); ! # $config->writefile("out2"); ! # exit; # Create the output object. --- 33,147 ---- ! # Main program. ! # Create configuration object. ! my $config = AppConfig->new({ ! CASE => 1, ! GLOBAL => { ! ARGCOUNT => ARGCOUNT_ONE, ! ARGS => "=s", ! EXPAND => EXPAND_UID | EXPAND_VAR | EXPAND_ENV, ! } ! }); ! ! # Create the configuration variables. ! $config->define( ! "game_dir", { ! DEFAULT => "/home/uwe/games/q3a/game/linuxq3apoint-1.32b-2", ! }, ! "game_opendemo", { ! DEFAULT => "od", ! }, ! "game_password", { ! DEFAULT => "odcut", ! }, ! "config_file", { ! ALIAS => "c", ! DEFAULT => "", ! }, ! "action_version", { ! ALIAS => "version|V", ! ARGCOUNT => ARGCOUNT_NONE, ! DEFAULT => 0, ! ARGS => "", ! }, ! "action_help", { ! ALIAS => "help|h", ! ARGCOUNT => ARGCOUNT_NONE, ! DEFAULT => 0, ! ARGS => "", ! }, ! "action_man", { ! ALIAS => "man|m", ! ARGCOUNT => ARGCOUNT_NONE, ! DEFAULT => 0, ! ARGS => "", ! }, ! "logging", { ! ALIAS => "l", ! ARGS => "=i", ! DEFAULT => 0, ! }, ! "warning", { ! ALIAS => "w", ! ARGS => "=i", ! DEFAULT => 0, ! }, ! "output_file", { ! ALIAS => "output|o", ! DEFAULT => "odcut.od", ! }, ! "action_gui", { ! ALIAS => "gui|g", ! ARGCOUNT => ARGCOUNT_NONE, ! DEFAULT => 0, ! ARGS => "", ! }, ! "odfile", { ! ALIAS => "file|f", ! DEFAULT => "parser", ! }, ! ); ! $config->define( ! "game_executable", { ! DEFAULT => '$game_dir/quake3.x86', ! }, ! ); ! ! ! # First parse the command line @ARGV. ! if (!$config->getopt()) { ! pod2usage(-msg=>"Syntax error", -verbose=>0); exit(1); } ! # Perform basic actions. ! if ($config->action_version) { ! printf "ODcut %s, %s (%s)\n", release(), date(), comment(); ! exit(0); ! } ! ! if ($config->action_help) { ! pod2usage(-verbose=>0); ! exit(0); ! } ! ! if ($config->action_man) { ! pod2usage(-verbose=>2); ! exit(0); ! } ! ! # If we have a config file, read it in. ! if (length($config->config_file) > 0) { ! my $result = $config->file($config->config_file); ! if (!defined $result) { ! die "Could not parse configuration file ". ! $config->config_file . ": $!.\n"; ! } ! } ! # Create the output object. *************** *** 91,95 **** # Set the output file. ! $output->do_command(odcommand->new("o $opt_output")); --- 160,164 ---- # Set the output file. ! $output->do_command(odcommand->new("o " . $config->output_file)); *************** *** 103,107 **** ! if ($opt_gui) { # Get the GUI library. use odcutgui; --- 172,176 ---- ! if ($config->action_gui) { # Get the GUI library. use odcutgui; *************** *** 206,210 **** { my $level = shift; ! if ($level<=$opt_logging) { printf STDERR @_; } --- 275,279 ---- { my $level = shift; ! if ($level<=$config->logging) { printf STDERR @_; } *************** *** 214,218 **** { my $level = shift; ! if ($level<=$opt_warning) { printf STDERR @_; } --- 283,287 ---- { my $level = shift; ! if ($level<=$config->warning) { printf STDERR @_; } *************** *** 412,504 **** - # class definition odconfig ################################################### - package odconfig; - - - sub new($) - { - my $class = shift; - my $self = {}; - bless($self,$class); - $self->_init(); - return $self; - } - - - sub _init($) - { - my $self = shift; - - my $config = { - # 'game_dir' => '/home/uwe/games/q3a/game/linuxq3apoint-1.32b-2', - # 'game_executable' => 'quake3.x86', - # 'game_opendemo' => 'od', - 'game_password' => 'odcut', - }; - - $self->{"config"} = $config; - } - - - - sub get($$) - { - my ($self, $key) = @_; - - if (exists $self->{"config"}->{$key}) { - return $self->{"config"}->{$key}; - } - else { - warn "Config value $key does not exist.\n"; - return undef; - } - } - - - sub set($$$) - { - my ($self, $key, $value) = @_; - $self->{"config"}->{$key} = $value; - } - - - sub readfile($$) - { - my ($self, $filename) = @_; - - my $config = undef; - # TODO Portabiliy: Somehow 'do' does not work. - # unless (my $result = do $filename) { - unless (my $result = eval `cat $filename`) { - warn "couldn't parse $filename: $@\n" if $@; - warn "couldn't do $filename: $!\n" unless defined $result; - warn "couldn't run $filename\n" unless $result; - } - - # Merge data in. - # print $config; - foreach (keys %$config) { - # print ">>$_<<\n"; - $self->{"config"}->{$_} = $config->{$_}; - } - } - - - sub writefile($$) - { - my ($self, $filename) = @_; - - use Data::Dumper; - my $text = Data::Dumper->Dump([$self->{"config"}],["config"]); - my $fh = new IO::File ">$filename"; - if (!defined $fh) { - warn "couldn't open $filename for writing: $!\n"; - return -1; - } - print $fh $text; - $fh->close; - } - - # class definition odoutput ################################################### package odoutput; --- 481,484 ---- *************** *** 522,526 **** @$self{keys %extra} = values %extra; } ! $self->set_outputfile($opt_output); $self->interactive(0); $self->range_running(0); --- 502,506 ---- @$self{keys %extra} = values %extra; } ! $self->set_outputfile($config->output_file); $self->interactive(0); $self->range_running(0); *************** *** 528,531 **** --- 508,512 ---- $self->range_start(0); $self->range_end(0); + $self->{"config"} = $config; } *************** *** 997,1001 **** } else { my $odfile = undef; ! my $odfile_module = "odfile_" . $opt_odfile; my @OLDINC = @INC; # This will become more configurable in the future or will be --- 978,982 ---- } else { my $odfile = undef; ! my $odfile_module = "odfile_" . $config->odfile; my @OLDINC = @INC; # This will become more configurable in the future or will be *************** *** 1008,1012 **** }; if ($@) { ! main::warning 0, "no odfile class $opt_odfile known.\n"; } else { --- 989,993 ---- }; if ($@) { ! main::warning 0, "no odfile class %s known.\n", $config->odfile; } else { *************** *** 1217,1229 **** elsif ($command->get_code() eq "l") { if ($command->get_argc()>=1) { ! $opt_logging = $command->get_arg(0); } ! main::logging 0, "LOGGING %d\n", $opt_logging; } # end command l elsif ($command->get_code() eq "w") { if ($command->get_argc()>=1) { ! $opt_warning = $command->get_arg(0); } ! main::logging 0, "WARNING %d\n", $opt_warning; } # end command l elsif ($command->get_code() eq "i") { --- 1198,1210 ---- elsif ($command->get_code() eq "l") { if ($command->get_argc()>=1) { ! $config->set("logging",$command->get_arg(0)); } ! main::logging 0, "LOGGING %d\n", $config->logging; } # end command l elsif ($command->get_code() eq "w") { if ($command->get_argc()>=1) { ! $config->set("warning",$command->get_arg(0)); } ! main::logging 0, "WARNING %d\n", $config->warning; } # end command l elsif ($command->get_code() eq "i") { *************** *** 1267,1270 **** --- 1248,1252 ---- -h|-?|--help brief help message. -m|--man full documentation. + -c|--config file read configuration file. -l|--logging level logging verbose level (default: 0, off). -w|--warning level warning verbose level (default: 0, off). *************** *** 1289,1292 **** --- 1271,1278 ---- Prints the program version and exits. + =item B<--config level> + + Read the given configuration file. + =item B<--logging level> |