4 |
|
# Originally Written by Christopher Williams |
5 |
|
# |
6 |
|
# Description |
7 |
< |
# Do all the setting up required for the InputQuery based on an input hash |
7 |
> |
# Do all the setting up required for the UserQuery based on an input hash |
8 |
|
# --- defaults taken from activedoc configuration setup |
9 |
|
# |
10 |
|
# Interface |
11 |
|
# --------- |
12 |
< |
# new(UserInterface) : A new CommandLineInterface object |
12 |
> |
# new(CommandObjName, UserInterface) : A new CommandLineInterface object |
13 |
|
# define userinterface for any CL querys |
14 |
|
# setcommand(name,level,optionref) : Add a command to recognise at the given |
15 |
|
# level(>0) of the parse. Associate an |
19 |
|
# retunrs number of commands levels found |
20 |
|
# command(level) : return the command given on the command line |
21 |
|
# at a given level |
22 |
+ |
# allowedcommands(level) : return list of allowed commands for level |
23 |
|
# getargs(level) : return a list of arguments corresponding |
24 |
|
# to a given level of command |
25 |
|
# commandlevel() : return the number of found commands |
26 |
+ |
# parse(@args) : Hand it the args and it will setup dochandler |
27 |
+ |
# after calling the parse() method on the |
28 |
+ |
# CommandObj |
29 |
|
# |
30 |
|
# - private interface ------------------------------- |
31 |
|
# getoptions(command, level) : return the optionhash for a given command |
42 |
|
|
43 |
|
package ActiveDoc::CommandLineInterface; |
44 |
|
use ActiveDoc::UserQuery; |
45 |
+ |
use ActiveDoc::DOChandler; |
46 |
|
require 5.001; |
47 |
|
|
48 |
|
sub new { |
56 |
|
# --------------- Support Routines -------------------- |
57 |
|
sub init { |
58 |
|
my $self=shift; |
59 |
+ |
my $file=shift; |
60 |
|
my $UI=shift; |
61 |
|
|
62 |
+ |
# Set up a User Query Object |
63 |
|
$self->{UI}=$UI; |
64 |
< |
$self->{InputQuery}=ActiveDoc::UserQuery->new($self->{UI}); |
64 |
> |
$self->{UserQuery}=ActiveDoc::UserQuery->new($self->{UI}); |
65 |
> |
$self->_defaults(); # set up some minimal defaults |
66 |
|
|
67 |
+ |
# Set up our command handling object |
68 |
+ |
eval "require $file" ; |
69 |
+ |
die $@ if $@; |
70 |
+ |
$self->{Commands}=$file->new($self); |
71 |
+ |
|
72 |
|
$self->{errormessg}="Error : "; |
73 |
|
} |
74 |
|
|
75 |
+ |
sub parse { |
76 |
+ |
my $self=shift; |
77 |
+ |
my @args=@_; |
78 |
+ |
|
79 |
+ |
# Get all parameters into UserQuery |
80 |
+ |
$self->parseoptions(@_); |
81 |
+ |
|
82 |
+ |
# Initialise the DocHandler with what weve got so far |
83 |
+ |
$self->{dochandler}=ActiveDoc::DOChandler->new($self->{UserQuery}); |
84 |
|
|
85 |
+ |
$self->{Commands}->parse(@_); |
86 |
+ |
|
87 |
+ |
} |
88 |
+ |
|
89 |
|
sub error { |
90 |
|
my $self=shift; |
91 |
|
my $string=shift; |
96 |
|
|
97 |
|
# --------------- Interface routines -------------------- |
98 |
|
|
73 |
– |
sub getDOChandler { |
74 |
– |
my $self=shift; |
75 |
– |
$self->{dochandler}=ActiveDoc::DOChandler->new($self->{InputQuery}); |
76 |
– |
return $self->{dochandler}; |
77 |
– |
} |
99 |
|
|
100 |
|
sub parsecmdoptions { |
101 |
|
my $self=shift; |
115 |
|
else { |
116 |
|
# take next word as argument -- indicate by > |
117 |
|
if ( $$optionhashref{$option}[1]=~/^\>\>/ ) { |
118 |
< |
$self->{InputQuery}->setparam($$optionhashref{$option}[0], |
118 |
> |
$self->{UserQuery}->setparam($$optionhashref{$option}[0], |
119 |
|
(shift @$argref)); |
120 |
|
} |
121 |
|
else { # simply set value as in hashref |
122 |
< |
$self->{InputQuery}->setparam($$optionhashref{$option}[0], |
122 |
> |
$self->{UserQuery}->setparam($$optionhashref{$option}[0], |
123 |
|
$$optionhashref{$option}[1]); |
124 |
|
} |
125 |
|
} |
155 |
|
while ( ( $#args >= 0) && ( $level <= $#{$self->{commands}}) ) { |
156 |
|
$arg=shift @args; |
157 |
|
if ( exists ${$self->{commands}[$level]}{$arg} ) { |
158 |
< |
$self->{InputQuery}->setparam("_cmd_$level", $arg); |
158 |
> |
$self->{UserQuery}->setparam("_cmd_$level", $arg); |
159 |
|
$commandhashref=$self->getoptions($arg, $level); |
160 |
|
$self->parsecmdoptions($commandhashref, \@args); |
161 |
|
$self->{arglev}=$level; |
177 |
|
return $self->{arglev}; |
178 |
|
} |
179 |
|
|
180 |
+ |
sub allowedcommands { |
181 |
+ |
my $self=shift; |
182 |
+ |
my $level=shift; |
183 |
+ |
|
184 |
+ |
return keys %{$self->{commands}[$level]}; |
185 |
+ |
} |
186 |
+ |
|
187 |
|
sub getargs { |
188 |
|
my $self=shift; |
189 |
|
my $level=shift; |
204 |
|
my $name=shift; |
205 |
|
my $level=shift; |
206 |
|
my $optionref=shift; |
207 |
+ |
my $commandref=shift; |
208 |
|
|
209 |
|
${$self->{commands}[$level]}{$name}=$optionref; |
210 |
|
} |
211 |
|
|
212 |
+ |
|
213 |
|
sub setbase { |
214 |
|
my $self=shift; |
215 |
|
my $optionref=shift; |
221 |
|
my $self=shift; |
222 |
|
my $level=shift; |
223 |
|
|
224 |
< |
return $self->{InputQuery}->getparam("_cmd_$level"); |
224 |
> |
return $self->{UserQuery}->getparam("_cmd_$level"); |
225 |
> |
} |
226 |
> |
|
227 |
> |
# |
228 |
> |
# Minimum defaults for a simple Dochandler Bootstrap |
229 |
> |
# |
230 |
> |
sub _defaults { |
231 |
> |
my $self=shift; |
232 |
> |
$self->{UserQuery}->setparam('cache', "/tmp"); |
233 |
|
} |