XML/Axk/App.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« XML/Axk/DOM.pm ↑ Summary XML/Axk/L/L1.pm »
1 package XML::Axk::App;
2 2 ▶use XML::Axk::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use XML::Axk::Core;
Subroutine BEGIN: called CC=1 SCAR=2.6
4  
5 #BEGIN { require Exporter; $Exporter::Verbose=1; }
6  
7 # Semantic versioning, packed per Perl rules. Must always be at least one
8 # digit left of the decimal, and six digits right of the decimal. For
9 # prerelease versions, put an underscore before the last three digits.
10 our $VERSION = '0.001_003';
11  
12 use Getopt::Long qw(GetOptionsFromArray :config gnu_getopt);
13  
14 use constant DEBUG => false;
15  
16 # Shell exit codes
17 use constant EXIT_OK => 0; # success
18 use constant EXIT_PROC_ERR => 1; # error during processing
19 use constant EXIT_PARAM_ERR => 2; # couldn't understand the command line
20  
21 # === Command line parsing ============================================== {{{1
22  
23 # Files/scripts to load, in order. Each element is either [isfile, text]
24 # or {...} for pragmas.
25 # It is a package var so we can localize it.
26 our @_Sources;
27  
28 my $dr_save_source = sub {
29 my ($which, $text) = @_;
30 push @_Sources, [$which eq 'f', $text];
31 }; # dr_save_source
32  
33 my $dr_save_pragma = sub {
34 my ($kind, $text) = @_;
35 $kind = 'B' if $kind eq 'backend';
36 $kind = 'L' if $kind eq 'language';
37  
38 push @_Sources, { $kind => $text };
39 }; # dr_save_pragma
40  
41 my %CMDLINE_OPTS = (
42 # hash from internal name to array reference of
43 # [getopt-name, getopt-options, optional default-value]
44 # --- However, if default-value is a reference, it will be the
45 # --- destination for that value.
46 # They are listed in alphabetical order by option name,
47 # lowercase before upper, although the code does not require that order.
48  
49 #BACKEND => ['b', '|backend=s', $dr_save_pragma], # TODO
50  
51 #DUMP_VARS => ['d', '|dump-variables', false],
52 #DEBUG => ['D','|debug', false],
53 EVAL => ['e','|source=s@', $dr_save_source],
54 #RESTRICTED_EVAL => ['E','|exec=s@'],
55 SCRIPT => ['f','|file=s@', $dr_save_source],
56 # -F field separator?
57 # -h and --help reserved
58 # INPUT_FILENAME assigned by parse_command_line_into()
59 #INCLUDE => ['i','|include=s@'],
60 #KEEP_GOING => ['k','|keep-going',false], #not in gawk
61 #LIB => ['l','|load=s@'],
62 LANGUAGE => ['L','|language=s', $dr_save_pragma],
63 # --man reserved
64 # OUTPUT_FILENAME => ['o','|output=s', ""], # conflict with gawk
65 # OPTIMIZE => ['O','|optimize'],
66 #SANDBOX => ['S','|sandbox',false],
67 #SOURCES reserved
68 # --usage reserved
69 PRINT_VERSION => ['version','', false],
70 DEFS => ['v','|var=s%'],
71 # -? reserved
72 #
73 # gawk(1) long options: --dump-variables, --exec, --gen-po, --lint,
74 # --profile
75  
76 # Long-only options that are specific to axk.
77 NO_INPUT => ['no-input'], # When set, don't read any files. This is so
78 # testing with empty inputs is easier.
79 SHOW => ['show','=s@'], # which debugging output to print.
80 # TODO make it a hash instead?
81 );
82  
83 sub parse_command_line {
84 # Takes {into=>hash ref, from=>array ref}. Fills in the hash with the
85 # values from the command line, keyed by the keys in %CMDLINE_OPTS.
86  
87 my %params = @_;
88 local @_Sources;
89  
90 my $hrOptsOut = $params{into};
91  
92 # Easier syntax for checking whether optional args were provided.
93 # Syntax thanks to http://www.perlmonks.org/?node_id=696592
94 local *have = sub { return exists($hrOptsOut->{ $_[0] }); };
95  
96 # Set defaults so we don't have to test them with exists().
97 %$hrOptsOut = ( # map getopt option name to default value
98 map { $CMDLINE_OPTS{ $_ }->[0] => $CMDLINE_OPTS{ $_ }[2] }
99 grep { (scalar @{$CMDLINE_OPTS{ $_ }})==3 }
100 keys %CMDLINE_OPTS
101 );
102  
103 # Get options
104 my $opts_ok = GetOptionsFromArray(
105 $params{from}, # source array
106 $hrOptsOut, # destination hash
107 'usage|?', 'h|help', 'man', # options we handle here
108 map { $_->[0] . ($_->[1] // '') } values %CMDLINE_OPTS, # options strs
109 );
110  
111 # Help, if requested
112 if(!$opts_ok || have('usage') || have('h') || have('man')) {
113 # Only pull in the Pod routines if we actually need them.
114 require Pod::Usage;
115  
116 #require Pod::Find; # qw(pod_where);
117 #my $pod_input = Pod::Find::pod_where({-inc => 1, -verbose=>1}, __PACKAGE__);
118 # This takes a long time on my system.
119  
120 Pod::Usage::pod2usage(-verbose => 0, -exitval => EXIT_PARAM_ERR, -input => __FILE__) if !$opts_ok; # unknown opt
121 Pod::Usage::pod2usage(-verbose => 0, -exitval => EXIT_OK, -input => __FILE__) if have('usage');
122 Pod::Usage::pod2usage(-verbose => 1, -exitval => EXIT_OK, -input => __FILE__) if have('h');
123 Pod::Usage::pod2usage(-verbose => 2, -exitval => EXIT_OK, -input => __FILE__) if have('man');
124 }
125  
126 # Map the option names from GetOptions back to the internal names we use,
127 # e.g., $hrOptsOut->{EVAL} from $hrOptsOut->{e}.
128 my %revmap = map { $CMDLINE_OPTS{$_}->[0] => $_ } keys %CMDLINE_OPTS;
129 for my $optname (keys %$hrOptsOut) {
130 $hrOptsOut->{ $revmap{$optname} } = $hrOptsOut->{ $optname };
131 }
132  
133 # Process other arguments. TODO? support multiple input filenames?
134 #$hrOptsOut->{INPUT_FILENAME} = $ARGV[0] // "";
135  
136 $hrOptsOut->{SOURCES} = \@_Sources; # our local copy
137  
138 } #parse_command_line()
139  
140 # }}}1
141 # === Command-line runner =============================================== {{{1
142  
143 # Command-line runner. Call as XML::Axk::App::Main(\@ARGV).
144 sub Main {
145 my $lrArgs = shift or croak "No arguments - need at least a script";
146  
147 my %opts;
148 parse_command_line(from => $lrArgs, into => \%opts);
149  
150 if($opts{PRINT_VERSION}) {
151 $VERSION =~ m<^([^\.]+)\.(\d{3})(_?)(\d{3})>;
152 printf "axk version %d.%d.%d ($VERSION)%s\n", $1, $2, $4,
153 ($3 ? ' (dev)' : '');
154  
155 return 0;
156 }
157  
158 # Treat the first non-option arg as a script file if it exists
159 # and is readable, otherwise as axk source.
160 unless(@{$opts{SOURCES}}) {
161 die "No scripts to run" unless @$lrArgs;
162 my $option = shift @$lrArgs;
163 push @{$opts{SOURCES}}, [
164 -r $option && (-f $option || -l $option),
165 $option
166 ];
167 }
168  
169 my $core = XML::Axk::Core->new(\%opts);
170 # Note: core doesn't copy the provided options, so make sure
171 # they stick around as long as $core does.
172  
173 my $cmd_line_idx = 0; # Number the `-e`s on the command line
174 my $curr_lang = undef; # current -L, if any.
175 #my $curr_backend = undef; # to do
176  
177 foreach my $rItem (@{$opts{SOURCES}}) {
178  
179 if(ref $rItem eq 'ARRAY') { # source file or text
180 my $lrSource = $rItem;
181 my ($is_file, $text) = @$lrSource;
182  
183 if($is_file) {
184 $core->load_script_file(filename => $text,
185 $curr_lang ? (language => $curr_lang) : ()
186 );
187  
188 } else {
189 $core->load_script_text(text => $text,
190 filename => "(cmd line script #@{[++$cmd_line_idx]})",
191 $curr_lang ? (language => $curr_lang) :
192 (auto_language => true)
193 # true => add a Ln if there isn't one in the script
194 );
195 }
196  
197 } else { # pragma
198 my $hrPragma = $rItem;
199  
200 if(exists $hrPragma->{L}) {
201 $curr_lang = $hrPragma->{L};
202 #say "# Language is now $curr_lang"
203 }
204  
205 if(exists $hrPragma->{B}) {
206 die 'Backend selection is not yet supported';
207 }
208 }
209  
210 } #foreach source item
211  
212 # read from stdin if no input files specified.
213 push @$lrArgs, '-' unless @$lrArgs || $opts{NO_INPUT};
214  
215 $core->run(@$lrArgs);
216  
217 return 0;
218 } #Main()
219  
220 # }}}1
221  
222 # no import() --- call Main() directly with its fully-qualified name
223  
224 1; # End of XML::Axk::App
225 __END__
« XML/Axk/DOM.pm ↑ Summary XML/Axk/L/L1.pm »