1 |
#
|
2 |
# CommandLineInterface.pm
|
3 |
#
|
4 |
# Originally Written by Christopher Williams
|
5 |
#
|
6 |
# Description
|
7 |
# Do all the setting up required for the Query based on an input hash
|
8 |
# --- defaults taken from activedoc configuration setup
|
9 |
#
|
10 |
# Interface
|
11 |
# ---------
|
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
|
16 |
# optionhash with it (see parseoptions)
|
17 |
# setbase(optionref) : set options for the base level
|
18 |
# parseoptions(@args) : Parse a set of arguments for options/commands
|
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
|
32 |
# parsecmdoptions(optionhashref, \@args) : parse the options, setting the
|
33 |
# Query
|
34 |
# appropriately as indicated by optionhash
|
35 |
# The option hash ref should have the
|
36 |
# format as given in example below
|
37 |
# $ohr={
|
38 |
# '-I'=> [ qw(dir >>) ],
|
39 |
# '-g'=> [ qw(debug true) ] }
|
40 |
# ( >> means take next argument as value)
|
41 |
#
|
42 |
|
43 |
package ActiveDoc::CommandLineInterface;
|
44 |
use ActiveDoc::Query;
|
45 |
use ActiveDoc::StarterDoc;
|
46 |
use ActiveDoc::UserInterface_basic;
|
47 |
#use ActiveDoc::DOChandler;
|
48 |
require 5.001;
|
49 |
|
50 |
sub new {
|
51 |
my $class=shift;
|
52 |
$self={};
|
53 |
bless $self, $class;
|
54 |
$self->init(@_);
|
55 |
return $self;
|
56 |
}
|
57 |
|
58 |
# --------------- Support Routines --------------------
|
59 |
sub init {
|
60 |
my $self=shift;
|
61 |
my $file=shift;
|
62 |
my $UI=shift;
|
63 |
|
64 |
# Set up a User Query Object
|
65 |
$self->{UI}=$UI;
|
66 |
$self->{Query}=ActiveDoc::Query->new();
|
67 |
|
68 |
# Set up our command handling object
|
69 |
eval "require $file" ;
|
70 |
die $@ if $@;
|
71 |
$self->{Commands}=$file->new($self);
|
72 |
|
73 |
$self->{errormessg}="Error : ";
|
74 |
}
|
75 |
|
76 |
sub parse {
|
77 |
my $self=shift;
|
78 |
my @args=@_;
|
79 |
|
80 |
# Get all parameters into Query
|
81 |
$self->parseoptions(@_);
|
82 |
|
83 |
# Initialise the DocHandler with what weve got so far
|
84 |
#$self->{dochandler}=ActiveDoc::DOChandler->new($self->{Query});
|
85 |
# $self->{StartDoc}=ActiveDoc::StarterDoc->new($self->{Query},
|
86 |
$self->{UI} );
|
87 |
|
88 |
# call method on CommandObject
|
89 |
$self->{Commands}->parse(@_);
|
90 |
|
91 |
}
|
92 |
|
93 |
sub error {
|
94 |
my $self=shift;
|
95 |
my $string=shift;
|
96 |
|
97 |
die $self->{errormessg}.$string."\n";
|
98 |
}
|
99 |
|
100 |
|
101 |
# --------------- Interface routines --------------------
|
102 |
|
103 |
|
104 |
sub parsecmdoptions {
|
105 |
my $self=shift;
|
106 |
my $optionhashref=shift;
|
107 |
my $argref=shift;
|
108 |
|
109 |
my $option;
|
110 |
|
111 |
$option=shift @$argref;
|
112 |
|
113 |
while ( defined $option ) {
|
114 |
# deal with options
|
115 |
if ( $option=~/^\-/ ) {
|
116 |
if ( ! exists $$optionhashref{$option} ) {
|
117 |
$self->error("Unknown Option $option");
|
118 |
}
|
119 |
else {
|
120 |
# take next word as argument -- indicate by >
|
121 |
if ( $$optionhashref{$option}[1]=~/^\>\>/ ) {
|
122 |
$self->{Query}->setparam($$optionhashref{$option}[0],
|
123 |
(shift @$argref));
|
124 |
}
|
125 |
else { # simply set value as in hashref
|
126 |
$self->{Query}->setparam($$optionhashref{$option}[0],
|
127 |
$$optionhashref{$option}[1]);
|
128 |
}
|
129 |
}
|
130 |
}
|
131 |
else { # deal with commands
|
132 |
unshift @$argref, $option;
|
133 |
last;
|
134 |
}
|
135 |
$option=shift @$argref;
|
136 |
} # end while
|
137 |
}
|
138 |
|
139 |
sub parseoptions {
|
140 |
my $self=shift;
|
141 |
my @args=@_;
|
142 |
|
143 |
my $command;
|
144 |
my $commandhashref;
|
145 |
my $level;
|
146 |
|
147 |
$command='_base';
|
148 |
|
149 |
# base level
|
150 |
$level=0;
|
151 |
$commandhashref=$self->getoptions($command, $level);
|
152 |
$self->parsecmdoptions($commandhashref, \@args);
|
153 |
@{$self->{"_arg_0"}}=();
|
154 |
|
155 |
|
156 |
$self->{arglev}=$level;
|
157 |
$level++;
|
158 |
|
159 |
while ( ( $#args >= 0) && ( $level <= $#{$self->{commands}}) ) {
|
160 |
$arg=shift @args;
|
161 |
if ( exists ${$self->{commands}[$level]}{$arg} ) {
|
162 |
$self->{Query}->setparam("_cmd_$level", $arg);
|
163 |
$commandhashref=$self->getoptions($arg, $level);
|
164 |
$self->parsecmdoptions($commandhashref, \@args);
|
165 |
$self->{arglev}=$level;
|
166 |
@{$self->{"_arg_$self->{arglev}"}}=(); #initialise new arg array
|
167 |
$level++;
|
168 |
}
|
169 |
else {
|
170 |
# must be an argument to the last command
|
171 |
push @{$self->{"_arg_$self->{arglev}"}} , $arg;
|
172 |
}
|
173 |
}
|
174 |
# add any futher arguments to last argumentlist
|
175 |
push @{$self->{"_arg_$self->{arglev}"}} , @args;
|
176 |
return $self->{arglev};
|
177 |
}
|
178 |
|
179 |
sub commandlevel {
|
180 |
my $self=shift;
|
181 |
return $self->{arglev};
|
182 |
}
|
183 |
|
184 |
sub allowedcommands {
|
185 |
my $self=shift;
|
186 |
my $level=shift;
|
187 |
|
188 |
return keys %{$self->{commands}[$level]};
|
189 |
}
|
190 |
|
191 |
sub getargs {
|
192 |
my $self=shift;
|
193 |
my $level=shift;
|
194 |
|
195 |
return @{$self->{"_arg_$level"}};
|
196 |
}
|
197 |
|
198 |
sub getoptions {
|
199 |
my $self=shift;
|
200 |
my $name=shift;
|
201 |
my $level=shift;
|
202 |
|
203 |
return ${$self->{commands}[$level]}{$name};
|
204 |
}
|
205 |
|
206 |
sub setcommand {
|
207 |
my $self=shift;
|
208 |
my $name=shift;
|
209 |
my $level=shift;
|
210 |
my $optionref=shift;
|
211 |
my $commandref=shift;
|
212 |
|
213 |
${$self->{commands}[$level]}{$name}=$optionref;
|
214 |
}
|
215 |
|
216 |
|
217 |
sub setbase {
|
218 |
my $self=shift;
|
219 |
my $optionref=shift;
|
220 |
|
221 |
$self->setcommand("_base", 0 , $optionref);
|
222 |
}
|
223 |
|
224 |
sub command {
|
225 |
my $self=shift;
|
226 |
my $level=shift;
|
227 |
|
228 |
return $self->{Query}->getparam("_cmd_$level");
|
229 |
}
|