ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/CommandLineInterface.pm
Revision: 1.3
Committed: Mon Sep 20 16:27:57 1999 UTC (25 years, 8 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.2: +50 -12 lines
Log Message:
Basic Working Level

File Contents

# Content
1 #
2 # CommandLineInterface.pm
3 #
4 # Originally Written by Christopher Williams
5 #
6 # Description
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(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 # UserQuery
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::UserQuery;
45 use ActiveDoc::DOChandler;
46 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 my $file=shift;
60 my $UI=shift;
61
62 # Set up a User Query Object
63 $self->{UI}=$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;
92
93 die $self->{errormessg}.$string."\n";
94 }
95
96
97 # --------------- Interface routines --------------------
98
99
100 sub parsecmdoptions {
101 my $self=shift;
102 my $optionhashref=shift;
103 my $argref=shift;
104
105 my $option;
106
107 $option=shift @$argref;
108
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 $self->{UserQuery}->setparam($$optionhashref{$option}[0],
119 (shift @$argref));
120 }
121 else { # simply set value as in hashref
122 $self->{UserQuery}->setparam($$optionhashref{$option}[0],
123 $$optionhashref{$option}[1]);
124 }
125 }
126 }
127 else { # deal with commands
128 unshift @$argref, $option;
129 last;
130 }
131 $option=shift @$argref;
132 } # end while
133 }
134
135 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 $self->{UserQuery}->setparam("_cmd_$level", $arg);
159 $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 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;
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 my $commandref=shift;
208
209 ${$self->{commands}[$level]}{$name}=$optionref;
210 }
211
212
213 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 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 }