ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/CommandLineInterface.pm
Revision: 1.2
Committed: Tue Sep 14 16:12:56 1999 UTC (25 years, 8 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.1: +105 -7 lines
Log Message:
Fully tested an working - up to parse options - passing options into only

File Contents

# User Rev Content
1 williamc 1.1 #
2     # CommandLineInterface.pm
3     #
4     # Originally Written by Christopher Williams
5     #
6     # Description
7     # Do all the setting up required for the InputQuery based on an input hash
8     # --- defaults taken from activedoc configuration setup
9     #
10     # Interface
11     # ---------
12     # new(UserInterface) : A new CommandLineInterface object
13     # 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     # getargs(level) : return a list of arguments corresponding
23     # to a given level of command
24     # commandlevel() : return the number of found commands
25     #
26     # - private interface -------------------------------
27     # getoptions(command, level) : return the optionhash for a given command
28     # parsecmdoptions(optionhashref, \@args) : parse the options, setting the
29     # UserQuery
30 williamc 1.1 # appropriately as indicated by optionhash
31     # The option hash ref should have the
32     # format as given in example below
33     # $ohr={
34     # '-I'=> [ qw(dir >>) ],
35     # '-g'=> [ qw(debug true) ] }
36     # ( >> means take next argument as value)
37 williamc 1.2 #
38 williamc 1.1
39     package ActiveDoc::CommandLineInterface;
40     use ActiveDoc::UserQuery;
41     require 5.001;
42    
43     sub new {
44     my $class=shift;
45     $self={};
46     bless $self, $class;
47     $self->init(@_);
48     return $self;
49     }
50    
51     # --------------- Support Routines --------------------
52     sub init {
53     my $self=shift;
54     my $UI=shift;
55    
56     $self->{UI}=$UI;
57     $self->{InputQuery}=ActiveDoc::UserQuery->new($self->{UI});
58    
59     $self->{errormessg}="Error : ";
60     }
61    
62    
63     sub error {
64     my $self=shift;
65     my $string=shift;
66    
67     die $self->{errormessg}.$string."\n";
68     }
69    
70    
71     # --------------- Interface routines --------------------
72    
73     sub getDOChandler {
74     my $self=shift;
75     $self->{dochandler}=ActiveDoc::DOChandler->new($self->{InputQuery});
76     return $self->{dochandler};
77     }
78    
79 williamc 1.2 sub parsecmdoptions {
80 williamc 1.1 my $self=shift;
81     my $optionhashref=shift;
82 williamc 1.2 my $argref=shift;
83 williamc 1.1
84     my $option;
85    
86 williamc 1.2 $option=shift @$argref;
87 williamc 1.1
88     while ( defined $option ) {
89     # deal with options
90     if ( $option=~/^\-/ ) {
91     if ( ! exists $$optionhashref{$option} ) {
92     $self->error("Unknown Option $option");
93     }
94     else {
95     # take next word as argument -- indicate by >
96     if ( $$optionhashref{$option}[1]=~/^\>\>/ ) {
97     $self->{InputQuery}->setparam($$optionhashref{$option}[0],
98 williamc 1.2 (shift @$argref));
99 williamc 1.1 }
100     else { # simply set value as in hashref
101     $self->{InputQuery}->setparam($$optionhashref{$option}[0],
102     $$optionhashref{$option}[1]);
103     }
104     }
105     }
106     else { # deal with commands
107 williamc 1.2 unshift @$argref, $option;
108 williamc 1.1 last;
109     }
110 williamc 1.2 $option=shift @$argref;
111 williamc 1.1 } # end while
112     }
113    
114 williamc 1.2 sub parseoptions {
115     my $self=shift;
116     my @args=@_;
117    
118     my $command;
119     my $commandhashref;
120     my $level;
121    
122     $command='_base';
123    
124     # base level
125     $level=0;
126     $commandhashref=$self->getoptions($command, $level);
127     $self->parsecmdoptions($commandhashref, \@args);
128     @{$self->{"_arg_0"}}=();
129    
130    
131     $self->{arglev}=$level;
132     $level++;
133    
134     while ( ( $#args >= 0) && ( $level <= $#{$self->{commands}}) ) {
135     $arg=shift @args;
136     if ( exists ${$self->{commands}[$level]}{$arg} ) {
137     $self->{InputQuery}->setparam("_cmd_$level", $arg);
138     $commandhashref=$self->getoptions($arg, $level);
139     $self->parsecmdoptions($commandhashref, \@args);
140     $self->{arglev}=$level;
141     @{$self->{"_arg_$self->{arglev}"}}=(); #initialise new arg array
142     $level++;
143     }
144     else {
145     # must be an argument to the last command
146     push @{$self->{"_arg_$self->{arglev}"}} , $arg;
147     }
148     }
149     # add any futher arguments to last argumentlist
150     push @{$self->{"_arg_$self->{arglev}"}} , @args;
151     return $self->{arglev};
152     }
153    
154     sub commandlevel {
155     my $self=shift;
156     return $self->{arglev};
157     }
158    
159     sub getargs {
160     my $self=shift;
161     my $level=shift;
162    
163     return @{$self->{"_arg_$level"}};
164     }
165    
166     sub getoptions {
167     my $self=shift;
168     my $name=shift;
169     my $level=shift;
170    
171     return ${$self->{commands}[$level]}{$name};
172     }
173    
174     sub setcommand {
175     my $self=shift;
176     my $name=shift;
177     my $level=shift;
178     my $optionref=shift;
179    
180     ${$self->{commands}[$level]}{$name}=$optionref;
181     }
182    
183     sub setbase {
184     my $self=shift;
185     my $optionref=shift;
186    
187     $self->setcommand("_base", 0 , $optionref);
188     }
189    
190     sub command {
191     my $self=shift;
192     my $level=shift;
193    
194     return $self->{InputQuery}->getparam("_cmd_$level");
195     }