]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cmndline.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cmndline.cc,v 1.15 2003/02/10 01:40:58 doogie Exp $
4 /* ######################################################################
6 Command Line Class - Sophisticated command line parser
8 This source is placed in the Public Domain, do with it what you will
9 It was originally written by Jason Gunthorpe <jgg@debian.org>.
11 ##################################################################### */
13 // Include files /*{{{*/
14 #include <apt-pkg/cmndline.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/strutl.h>
22 // CommandLine::CommandLine - Constructor /*{{{*/
23 // ---------------------------------------------------------------------
25 CommandLine::CommandLine(Args
*AList
,Configuration
*Conf
) : ArgList(AList
),
26 Conf(Conf
), FileList(0)
30 // CommandLine::~CommandLine - Destructor /*{{{*/
31 // ---------------------------------------------------------------------
33 CommandLine::~CommandLine()
38 // CommandLine::Parse - Main action member /*{{{*/
39 // ---------------------------------------------------------------------
41 bool CommandLine::Parse(int argc
,const char **argv
)
44 FileList
= new const char *[argc
];
45 const char **Files
= FileList
;
47 for (I
= 1; I
!= argc
; I
++)
49 const char *Opt
= argv
[I
];
51 // It is not an option
60 // Double dash signifies the end of option processing
61 if (*Opt
== '-' && Opt
[1] == 0)
67 // Single dash is a short option
70 // Iterate over each letter
73 // Search for the option
75 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
77 return _error
->Error(_("Command line option '%c' [from %s] is not known."),*Opt
,argv
[I
]);
79 if (HandleOpt(I
,argc
,argv
,Opt
,A
) == false)
89 // Match up to a = against the list
90 const char *OptEnd
= Opt
;
92 for (; *OptEnd
!= 0 && *OptEnd
!= '='; OptEnd
++);
93 for (A
= ArgList
; A
->end() == false &&
94 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
96 // Failed, look for a word after the first - (no-foo)
97 bool PreceedMatch
= false;
100 for (; Opt
!= OptEnd
&& *Opt
!= '-'; Opt
++);
103 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
106 for (A
= ArgList
; A
->end() == false &&
107 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
110 if (A
->end() == true && OptEnd
- Opt
!= 1)
111 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
113 // The option could be a single letter option prefixed by a no-..
114 if (A
->end() == true)
116 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
118 if (A
->end() == true)
119 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
122 // The option is not boolean
123 if (A
->IsBoolean() == false)
124 return _error
->Error(_("Command line option %s is not boolean"),argv
[I
]);
130 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
134 // Copy any remaining file names over
135 for (; I
!= argc
; I
++)
142 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
143 // ---------------------------------------------------------------------
144 /* This is a helper function for parser, it looks at a given argument
145 and looks for specific patterns in the string, it gets tokanized
146 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
147 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
148 const char *&Opt
,Args
*A
,bool PreceedMatch
)
150 const char *Argument
= 0;
151 bool CertainArg
= false;
154 /* Determine the possible location of an option or 0 if their is
156 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
158 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
159 Argument
= argv
[I
+1];
161 // Equals was specified but we fell off the end!
162 if (Opt
[1] == '=' && Argument
== 0)
163 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
180 // Option is an argument set
181 if ((A
->Flags
& HasArg
) == HasArg
)
184 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
188 // Parse a configuration file
189 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
190 return ReadConfigFile(*Conf
,Argument
);
192 // Arbitrary item specification
193 if ((A
->Flags
& ArbItem
) == ArbItem
)
196 for (J
= Argument
; *J
!= 0 && *J
!= '='; J
++);
198 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
204 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
205 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
208 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
213 const char *I
= A
->ConfName
;
214 for (; *I
!= 0 && *I
!= ' '; I
++);
216 Conf
->Set(string(A
->ConfName
,0,I
-A
->ConfName
),string(I
+1) + Argument
);
218 Conf
->Set(A
->ConfName
,string(I
) + Argument
);
223 // Option is an integer level
224 if ((A
->Flags
& IntLevel
) == IntLevel
)
226 // There might be an argument
230 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
232 // Conversion failed and the argument was specified with an =s
233 if (EndPtr
== Argument
&& CertainArg
== true)
234 return _error
->Error(_("Option %s requires an integer argument, not '%s'"),argv
[I
],Argument
);
236 // Conversion was ok, set the value and return
237 if (EndPtr
!= 0 && EndPtr
!= Argument
&& *EndPtr
== 0)
239 Conf
->Set(A
->ConfName
,Value
);
246 // Increase the level
247 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
251 // Option is a boolean
252 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
254 // Look for an argument.
257 // Look at preceeding text
261 if (PreceedMatch
== false)
264 if (strlen(argv
[I
]) >= sizeof(Buffer
))
265 return _error
->Error(_("Option '%s' is too long"),argv
[I
]);
267 // Skip the leading dash
268 const char *J
= argv
[I
];
269 for (; *J
!= 0 && *J
== '-'; J
++);
271 const char *JEnd
= J
;
272 for (; *JEnd
!= 0 && *JEnd
!= '-'; JEnd
++);
275 strncpy(Buffer
,J
,JEnd
- J
);
276 Buffer
[JEnd
- J
] = 0;
285 Sense
= StringToBool(Argument
);
289 if (Argument
!= Buffer
)
297 if (CertainArg
== true)
298 return _error
->Error(_("Sense %s is not understood, try true or false."),Argument
);
303 // Indeterminate sense depends on the flag
306 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
312 Conf
->Set(A
->ConfName
,Sense
);
316 // CommandLine::FileSize - Count the number of filenames /*{{{*/
317 // ---------------------------------------------------------------------
319 unsigned int CommandLine::FileSize() const
321 unsigned int Count
= 0;
322 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
327 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
328 // ---------------------------------------------------------------------
330 bool CommandLine::DispatchArg(Dispatch
*Map
,bool NoMatch
)
333 for (I
= 0; Map
[I
].Match
!= 0; I
++)
335 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
337 bool Res
= Map
[I
].Handler(*this);
338 if (Res
== false && _error
->PendingError() == false)
339 _error
->Error("Handler silently failed");
345 if (Map
[I
].Match
== 0)
348 _error
->Error(_("Invalid operation %s"),FileList
[0]);