]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cmndline.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cmndline.cc,v 1.12 2001/06/09 22:39:48 jgg Exp $
4 /* ######################################################################
6 Command Line Class - Sophisticated command line parser
8 ##################################################################### */
10 // Include files /*{{{*/
12 #pragma implementation "apt-pkg/cmndline.h"
14 #include <apt-pkg/cmndline.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/strutl.h>
21 // CommandLine::CommandLine - Constructor /*{{{*/
22 // ---------------------------------------------------------------------
24 CommandLine::CommandLine(Args
*AList
,Configuration
*Conf
) : ArgList(AList
),
25 Conf(Conf
), FileList(0)
29 // CommandLine::~CommandLine - Destructor /*{{{*/
30 // ---------------------------------------------------------------------
32 CommandLine::~CommandLine()
37 // CommandLine::Parse - Main action member /*{{{*/
38 // ---------------------------------------------------------------------
40 bool CommandLine::Parse(int argc
,const char **argv
)
43 FileList
= new const char *[argc
];
44 const char **Files
= FileList
;
46 for (I
= 1; I
!= argc
; I
++)
48 const char *Opt
= argv
[I
];
50 // It is not an option
59 // Double dash signifies the end of option processing
60 if (*Opt
== '-' && Opt
[1] == 0)
66 // Single dash is a short option
69 // Iterate over each letter
72 // Search for the option
74 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
76 return _error
->Error(_("Command line option '%c' [from %s] is not known."),*Opt
,argv
[I
]);
78 if (HandleOpt(I
,argc
,argv
,Opt
,A
) == false)
88 // Match up to a = against the list
89 const char *OptEnd
= Opt
;
91 for (; *OptEnd
!= 0 && *OptEnd
!= '='; OptEnd
++);
92 for (A
= ArgList
; A
->end() == false &&
93 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
95 // Failed, look for a word after the first - (no-foo)
96 bool PreceedMatch
= false;
99 for (; Opt
!= OptEnd
&& *Opt
!= '-'; Opt
++);
102 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
105 for (A
= ArgList
; A
->end() == false &&
106 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
109 if (A
->end() == true && OptEnd
- Opt
!= 1)
110 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
112 // The option could be a single letter option prefixed by a no-..
113 if (A
->end() == true)
115 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
117 if (A
->end() == true)
118 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
121 // The option is not boolean
122 if (A
->IsBoolean() == false)
123 return _error
->Error(_("Command line option %s is not boolean"),argv
[I
]);
129 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
133 // Copy any remaining file names over
134 for (; I
!= argc
; I
++)
141 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
142 // ---------------------------------------------------------------------
143 /* This is a helper function for parser, it looks at a given argument
144 and looks for specific patterns in the string, it gets tokanized
145 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
146 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
147 const char *&Opt
,Args
*A
,bool PreceedMatch
)
149 const char *Argument
= 0;
150 bool CertainArg
= false;
153 /* Determine the possible location of an option or 0 if their is
155 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
157 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
158 Argument
= argv
[I
+1];
160 // Equals was specified but we fell off the end!
161 if (Opt
[1] == '=' && Argument
== 0)
162 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
179 // Option is an argument set
180 if ((A
->Flags
& HasArg
) == HasArg
)
183 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
187 // Parse a configuration file
188 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
189 return ReadConfigFile(*Conf
,Argument
);
191 // Arbitary item specification
192 if ((A
->Flags
& ArbItem
) == ArbItem
)
195 for (J
= Argument
; *J
!= 0 && *J
!= '='; J
++);
197 return _error
->Error(_("Option %s: Configuration item sepecification must have an =<val>."),argv
[I
]);
203 return _error
->Error(_("Option %s: Configuration item sepecification must have an =<val>."),argv
[I
]);
204 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
207 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
212 const char *I
= A
->ConfName
;
213 for (; *I
!= 0 && *I
!= ' '; I
++);
215 Conf
->Set(string(A
->ConfName
,0,I
-A
->ConfName
),string(I
+1) + Argument
);
217 Conf
->Set(A
->ConfName
,string(I
) + Argument
);
222 // Option is an integer level
223 if ((A
->Flags
& IntLevel
) == IntLevel
)
225 // There might be an argument
229 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
231 // Conversion failed and the argument was specified with an =s
232 if (EndPtr
== Argument
&& CertainArg
== true)
233 return _error
->Error(_("Option %s requires an integer argument, not '%s'"),argv
[I
],Argument
);
235 // Conversion was ok, set the value and return
236 if (EndPtr
!= 0 && EndPtr
!= Argument
&& *EndPtr
== 0)
238 Conf
->Set(A
->ConfName
,Value
);
245 // Increase the level
246 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
250 // Option is a boolean
251 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
253 // Look for an argument.
256 // Look at preceeding text
260 if (PreceedMatch
== false)
263 if (strlen(argv
[I
]) >= sizeof(Buffer
))
264 return _error
->Error(_("Option '%s' is too long"),argv
[I
]);
266 // Skip the leading dash
267 const char *J
= argv
[I
];
268 for (; *J
!= 0 && *J
== '-'; J
++);
270 const char *JEnd
= J
;
271 for (; *JEnd
!= 0 && *JEnd
!= '-'; JEnd
++);
274 strncpy(Buffer
,J
,JEnd
- J
);
275 Buffer
[JEnd
- J
] = 0;
284 Sense
= StringToBool(Argument
);
288 if (Argument
!= Buffer
)
296 if (CertainArg
== true)
297 return _error
->Error(_("Sense %s is not understood, try true or false."),Argument
);
302 // Indeterminate sense depends on the flag
305 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
311 Conf
->Set(A
->ConfName
,Sense
);
315 // CommandLine::FileSize - Count the number of filenames /*{{{*/
316 // ---------------------------------------------------------------------
318 unsigned int CommandLine::FileSize() const
320 unsigned int Count
= 0;
321 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
326 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
327 // ---------------------------------------------------------------------
329 bool CommandLine::DispatchArg(Dispatch
*Map
,bool NoMatch
)
332 for (I
= 0; Map
[I
].Match
!= 0; I
++)
334 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
336 bool Res
= Map
[I
].Handler(*this);
337 if (Res
== false && _error
->PendingError() == false)
338 _error
->Error("Handler silently failed");
344 if (Map
[I
].Match
== 0)
347 _error
->Error(_("Invalid operation %s"),FileList
[0]);