@@ -11,6 +11,7 @@ use warnings;
11
11
12
12
use Getopt::Long 2.5 qw( GetOptionsFromArray) ;
13
13
use Pod::Usage;
14
+ use Data::Dumper;
14
15
15
16
# === Constants ===========================================================
16
17
@@ -58,6 +59,7 @@ use constant OB_STARTLINE => 2;
58
59
59
60
# Internals
60
61
my $Package = ' ' ; # package name for the generated script
62
+ my $PackageNum = 0; # make sure each run has a unique package name
61
63
my $RootSTDOUT ;
62
64
my $WorkingDir = ' .' ;
63
65
my %Opts ; # Parsed command-line options
@@ -616,6 +618,10 @@ my %CMDLINE_OPTS = (
616
618
SETS => [' s' ,' |set:s%' ], # Extra data in %S, without text substitution
617
619
# --usage reserved
618
620
PRINT_VERSION => [' v' ,' |version+' ],
621
+
622
+ # Special-case for testing --- don't exit on --help &c.
623
+ NOEXIT_ON_HELP => [' z_noexit_on_help' ],
624
+
619
625
# -? reserved
620
626
);
621
627
@@ -645,22 +651,42 @@ sub parse_command_line {
645
651
# small POD below, which links to `perldoc perlpp`.
646
652
647
653
# Get options
654
+ my $ok =
648
655
GetOptionsFromArray($lrArgs , $hrOptsOut , # destination hash
649
656
' usage|?' , ' h|help' , ' man' , # options we handle here
650
- map { $_ -> [0] . $_ -> [1] } values %CMDLINE_OPTS , # options strs
651
- )
652
- or pod2usage(-verbose => 0, -exitval => EXIT_PARAM_ERR, %docs );
653
- # unknown opt
654
-
655
- # Help, if requested
656
- pod2usage(-verbose => 0, -exitval => EXIT_PROC_ERR, %docs ) if have(' usage' );
657
- pod2usage(-verbose => 1, -exitval => EXIT_PROC_ERR, %docs ) if have(' h' );
658
- pod2usage(-verbose => 2, -exitval => EXIT_PROC_ERR, %docs ) if have(' man' );
657
+ map { $_ -> [0] . ($_ -> [1]//' ' ) } values %CMDLINE_OPTS , # options strs
658
+ );
659
+ my $noexit_on_help =
660
+ $hrOptsOut -> { $CMDLINE_OPTS {NOEXIT_ON_HELP }-> [0] } // false;
661
+
662
+ if ($noexit_on_help ) { # Report help during testing
663
+ # unknown opt --- error out. false => processing should terminate.
664
+ pod2usage(-verbose => 0, -exitval => ' NOEXIT' , %docs ), return false unless $ok ;
665
+
666
+ # Help, if requested
667
+ pod2usage(-verbose => 0, -exitval => ' NOEXIT' , %docs ), return false if have(' usage' );
668
+ pod2usage(-verbose => 1, -exitval => ' NOEXIT' , %docs ), return false if have(' h' );
669
+ pod2usage(-verbose => 2, -exitval => ' NOEXIT' , %docs ), return false if have(' man' );
670
+
671
+ } else { # Normal usage
672
+ # unknown opt --- error out
673
+ pod2usage(-verbose => 0, -exitval => EXIT_PARAM_ERR, %docs ) unless $ok ;
674
+
675
+ # Help, if requested
676
+ pod2usage(-verbose => 0, -exitval => EXIT_PROC_ERR, %docs ) if have(' usage' );
677
+ pod2usage(-verbose => 1, -exitval => EXIT_PROC_ERR, %docs ) if have(' h' );
678
+ pod2usage(-verbose => 2, -exitval => EXIT_PROC_ERR, %docs ) if have(' man' );
679
+ }
659
680
660
681
# Map the option names from GetOptions back to the internal names we use,
661
682
# e.g., $hrOptsOut->{EVAL} from $hrOptsOut->{e}.
662
683
my %revmap = map { $CMDLINE_OPTS {$_ }-> [0] => $_ } keys %CMDLINE_OPTS ;
684
+ # say "revmap ", Dumper(\%revmap);
685
+ # say "hrOptsOut ", Dumper($hrOptsOut);
663
686
for my $optname (keys %$hrOptsOut ) {
687
+ # say "\nOptname $optname";
688
+ # say "Value $hrOptsOut->{$optname}";
689
+ # say "Revmap $revmap{$optname}";
664
690
$hrOptsOut -> { $revmap {$optname } } = $hrOptsOut -> { $optname };
665
691
}
666
692
@@ -672,13 +698,16 @@ sub parse_command_line {
672
698
# Process other arguments. TODO? support multiple input filenames?
673
699
$hrOptsOut -> {INPUT_FILENAME } = $ARGV [0] // " " ;
674
700
701
+ return true; # Go ahead and run
675
702
} # parse_command_line()
676
703
677
704
# === Main ================================================================
678
705
679
706
sub Main {
680
707
my $lrArgv = shift // [];
681
- parse_command_line $lrArgv , \%Opts ;
708
+ unless (parse_command_line $lrArgv , \%Opts ) {
709
+ return EXIT_OK; # TODO report param err vs. proc err?
710
+ }
682
711
683
712
if ($Opts {PRINT_VERSION }) {
684
713
print " PerlPP version $Text::PerlPP::VERSION \n " ;
@@ -694,6 +723,7 @@ sub Main {
694
723
$Package =~ s / ^.*?([a-z_][a-z_0-9.]*).pl?$/ $1 / i ;
695
724
$Package =~ s / [^a-z0-9_]/ _/ gi ;
696
725
# $Package is not the whole name, so can start with a number.
726
+ $Package .= $PackageNum ++;
697
727
698
728
StartOB(); # Output from here on will be included in the generated script
699
729
0 commit comments