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