1 |
williamc |
1.1 |
#
|
2 |
|
|
# CommandLineInterface.pm
|
3 |
|
|
#
|
4 |
|
|
# Originally Written by Christopher Williams
|
5 |
|
|
#
|
6 |
|
|
# Description
|
7 |
williamc |
1.3 |
# Do all the setting up required for the UserQuery based on an input hash
|
8 |
williamc |
1.1 |
# --- defaults taken from activedoc configuration setup
|
9 |
|
|
#
|
10 |
|
|
# Interface
|
11 |
|
|
# ---------
|
12 |
williamc |
1.3 |
# new(CommandObjName, UserInterface) : A new CommandLineInterface object
|
13 |
williamc |
1.1 |
# define userinterface for any CL querys
|
14 |
williamc |
1.2 |
# 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 |
williamc |
1.3 |
# allowedcommands(level) : return list of allowed commands for level
|
23 |
williamc |
1.2 |
# getargs(level) : return a list of arguments corresponding
|
24 |
|
|
# to a given level of command
|
25 |
|
|
# commandlevel() : return the number of found commands
|
26 |
williamc |
1.3 |
# parse(@args) : Hand it the args and it will setup dochandler
|
27 |
|
|
# after calling the parse() method on the
|
28 |
|
|
# CommandObj
|
29 |
williamc |
1.2 |
#
|
30 |
|
|
# - private interface -------------------------------
|
31 |
|
|
# getoptions(command, level) : return the optionhash for a given command
|
32 |
|
|
# parsecmdoptions(optionhashref, \@args) : parse the options, setting the
|
33 |
|
|
# UserQuery
|
34 |
williamc |
1.1 |
# 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 |
williamc |
1.2 |
#
|
42 |
williamc |
1.1 |
|
43 |
|
|
package ActiveDoc::CommandLineInterface;
|
44 |
|
|
use ActiveDoc::UserQuery;
|
45 |
williamc |
1.3 |
use ActiveDoc::DOChandler;
|
46 |
williamc |
1.1 |
require 5.001;
|
47 |
|
|
|
48 |
|
|
sub new {
|
49 |
|
|
my $class=shift;
|
50 |
|
|
$self={};
|
51 |
|
|
bless $self, $class;
|
52 |
|
|
$self->init(@_);
|
53 |
|
|
return $self;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
# --------------- Support Routines --------------------
|
57 |
|
|
sub init {
|
58 |
|
|
my $self=shift;
|
59 |
williamc |
1.3 |
my $file=shift;
|
60 |
williamc |
1.1 |
my $UI=shift;
|
61 |
|
|
|
62 |
williamc |
1.3 |
# Set up a User Query Object
|
63 |
williamc |
1.1 |
$self->{UI}=$UI;
|
64 |
williamc |
1.3 |
$self->{UserQuery}=ActiveDoc::UserQuery->new($self->{UI});
|
65 |
|
|
$self->_defaults(); # set up some minimal defaults
|
66 |
williamc |
1.1 |
|
67 |
williamc |
1.3 |
# Set up our command handling object
|
68 |
|
|
eval "require $file" ;
|
69 |
|
|
die $@ if $@;
|
70 |
|
|
$self->{Commands}=$file->new($self);
|
71 |
|
|
|
72 |
williamc |
1.1 |
$self->{errormessg}="Error : ";
|
73 |
|
|
}
|
74 |
|
|
|
75 |
williamc |
1.3 |
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 |
williamc |
1.1 |
|
85 |
williamc |
1.3 |
$self->{Commands}->parse(@_);
|
86 |
|
|
|
87 |
|
|
}
|
88 |
|
|
|
89 |
williamc |
1.1 |
sub error {
|
90 |
|
|
my $self=shift;
|
91 |
|
|
my $string=shift;
|
92 |
|
|
|
93 |
|
|
die $self->{errormessg}.$string."\n";
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
# --------------- Interface routines --------------------
|
98 |
|
|
|
99 |
|
|
|
100 |
williamc |
1.2 |
sub parsecmdoptions {
|
101 |
williamc |
1.1 |
my $self=shift;
|
102 |
|
|
my $optionhashref=shift;
|
103 |
williamc |
1.2 |
my $argref=shift;
|
104 |
williamc |
1.1 |
|
105 |
|
|
my $option;
|
106 |
|
|
|
107 |
williamc |
1.2 |
$option=shift @$argref;
|
108 |
williamc |
1.1 |
|
109 |
|
|
while ( defined $option ) {
|
110 |
|
|
# deal with options
|
111 |
|
|
if ( $option=~/^\-/ ) {
|
112 |
|
|
if ( ! exists $$optionhashref{$option} ) {
|
113 |
|
|
$self->error("Unknown Option $option");
|
114 |
|
|
}
|
115 |
|
|
else {
|
116 |
|
|
# take next word as argument -- indicate by >
|
117 |
|
|
if ( $$optionhashref{$option}[1]=~/^\>\>/ ) {
|
118 |
williamc |
1.3 |
$self->{UserQuery}->setparam($$optionhashref{$option}[0],
|
119 |
williamc |
1.2 |
(shift @$argref));
|
120 |
williamc |
1.1 |
}
|
121 |
|
|
else { # simply set value as in hashref
|
122 |
williamc |
1.3 |
$self->{UserQuery}->setparam($$optionhashref{$option}[0],
|
123 |
williamc |
1.1 |
$$optionhashref{$option}[1]);
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
else { # deal with commands
|
128 |
williamc |
1.2 |
unshift @$argref, $option;
|
129 |
williamc |
1.1 |
last;
|
130 |
|
|
}
|
131 |
williamc |
1.2 |
$option=shift @$argref;
|
132 |
williamc |
1.1 |
} # end while
|
133 |
|
|
}
|
134 |
|
|
|
135 |
williamc |
1.2 |
sub parseoptions {
|
136 |
|
|
my $self=shift;
|
137 |
|
|
my @args=@_;
|
138 |
|
|
|
139 |
|
|
my $command;
|
140 |
|
|
my $commandhashref;
|
141 |
|
|
my $level;
|
142 |
|
|
|
143 |
|
|
$command='_base';
|
144 |
|
|
|
145 |
|
|
# base level
|
146 |
|
|
$level=0;
|
147 |
|
|
$commandhashref=$self->getoptions($command, $level);
|
148 |
|
|
$self->parsecmdoptions($commandhashref, \@args);
|
149 |
|
|
@{$self->{"_arg_0"}}=();
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
$self->{arglev}=$level;
|
153 |
|
|
$level++;
|
154 |
|
|
|
155 |
|
|
while ( ( $#args >= 0) && ( $level <= $#{$self->{commands}}) ) {
|
156 |
|
|
$arg=shift @args;
|
157 |
|
|
if ( exists ${$self->{commands}[$level]}{$arg} ) {
|
158 |
williamc |
1.3 |
$self->{UserQuery}->setparam("_cmd_$level", $arg);
|
159 |
williamc |
1.2 |
$commandhashref=$self->getoptions($arg, $level);
|
160 |
|
|
$self->parsecmdoptions($commandhashref, \@args);
|
161 |
|
|
$self->{arglev}=$level;
|
162 |
|
|
@{$self->{"_arg_$self->{arglev}"}}=(); #initialise new arg array
|
163 |
|
|
$level++;
|
164 |
|
|
}
|
165 |
|
|
else {
|
166 |
|
|
# must be an argument to the last command
|
167 |
|
|
push @{$self->{"_arg_$self->{arglev}"}} , $arg;
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
# add any futher arguments to last argumentlist
|
171 |
|
|
push @{$self->{"_arg_$self->{arglev}"}} , @args;
|
172 |
|
|
return $self->{arglev};
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
sub commandlevel {
|
176 |
|
|
my $self=shift;
|
177 |
|
|
return $self->{arglev};
|
178 |
|
|
}
|
179 |
|
|
|
180 |
williamc |
1.3 |
sub allowedcommands {
|
181 |
|
|
my $self=shift;
|
182 |
|
|
my $level=shift;
|
183 |
|
|
|
184 |
|
|
return keys %{$self->{commands}[$level]};
|
185 |
|
|
}
|
186 |
|
|
|
187 |
williamc |
1.2 |
sub getargs {
|
188 |
|
|
my $self=shift;
|
189 |
|
|
my $level=shift;
|
190 |
|
|
|
191 |
|
|
return @{$self->{"_arg_$level"}};
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
sub getoptions {
|
195 |
|
|
my $self=shift;
|
196 |
|
|
my $name=shift;
|
197 |
|
|
my $level=shift;
|
198 |
|
|
|
199 |
|
|
return ${$self->{commands}[$level]}{$name};
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
sub setcommand {
|
203 |
|
|
my $self=shift;
|
204 |
|
|
my $name=shift;
|
205 |
|
|
my $level=shift;
|
206 |
|
|
my $optionref=shift;
|
207 |
williamc |
1.3 |
my $commandref=shift;
|
208 |
williamc |
1.2 |
|
209 |
|
|
${$self->{commands}[$level]}{$name}=$optionref;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
williamc |
1.3 |
|
213 |
williamc |
1.2 |
sub setbase {
|
214 |
|
|
my $self=shift;
|
215 |
|
|
my $optionref=shift;
|
216 |
|
|
|
217 |
|
|
$self->setcommand("_base", 0 , $optionref);
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
sub command {
|
221 |
|
|
my $self=shift;
|
222 |
|
|
my $level=shift;
|
223 |
|
|
|
224 |
williamc |
1.3 |
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 |
williamc |
1.2 |
}
|