]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cmndline.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cmndline.cc,v 1.6 1998/11/25 23:54:22 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>
19 // CommandLine::CommandLine - Constructor /*{{{*/
20 // ---------------------------------------------------------------------
22 CommandLine::CommandLine(Args
*AList
,Configuration
*Conf
) : ArgList(AList
),
23 Conf(Conf
), FileList(0)
27 // CommandLine::~CommandLine - Destructor /*{{{*/
28 // ---------------------------------------------------------------------
30 CommandLine::~CommandLine()
35 // CommandLine::Parse - Main action member /*{{{*/
36 // ---------------------------------------------------------------------
38 bool CommandLine::Parse(int argc
,const char **argv
)
41 FileList
= new const char *[argc
];
42 const char **Files
= FileList
;
44 for (I
= 1; I
!= argc
; I
++)
46 const char *Opt
= argv
[I
];
48 // It is not an option
57 // Double dash signifies the end of option processing
58 if (*Opt
== '-' && Opt
[1] == 0)
61 // Single dash is a short option
64 // Iterate over each letter
67 // Search for the option
69 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
71 return _error
->Error("Command line option '%c' [from %s] is not known.",*Opt
,argv
[I
]);
73 if (HandleOpt(I
,argc
,argv
,Opt
,A
) == false)
83 // Match up to a = against the list
84 const char *OptEnd
= Opt
;
86 for (; *OptEnd
!= 0 && *OptEnd
!= '='; OptEnd
++);
87 for (A
= ArgList
; A
->end() == false &&
88 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
90 // Failed, look for a word after the first - (no-foo)
91 bool PreceedMatch
= false;
94 for (; Opt
!= OptEnd
&& *Opt
!= '-'; Opt
++);
97 return _error
->Error("Command line option %s is not understood",argv
[I
]);
101 for (A
= ArgList
; A
->end() == false &&
102 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
105 if (A
->end() == true && OptEnd
- Opt
!= 1)
106 return _error
->Error("Command line option %s is not understood",argv
[I
]);
108 // The option could be a single letter option prefixed by a no-..
109 if (A
->end() == true)
111 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
113 if (A
->end() == true)
114 return _error
->Error("Command line option %s is not understood",argv
[I
]);
117 // The option is not boolean
118 if (A
->IsBoolean() == false)
119 return _error
->Error("Command line option %s is not boolean",argv
[I
]);
125 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
129 // Copy any remaining file names over
130 for (; I
!= argc
; I
++)
137 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
138 // ---------------------------------------------------------------------
139 /* This is a helper function for parser, it looks at a given argument
140 and looks for specific patterns in the string, it gets tokanized
141 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
142 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
143 const char *&Opt
,Args
*A
,bool PreceedMatch
)
145 const char *Argument
= 0;
146 bool CertainArg
= false;
149 /* Determine the possible location of an option or 0 if their is
151 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
153 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
154 Argument
= argv
[I
+1];
156 // Equals was specified but we fell off the end!
157 if (Opt
[1] == '=' && Argument
== 0)
158 return _error
->Error("Option %s requires an argument.",argv
[I
]);
175 // Option is an argument set
176 if ((A
->Flags
& HasArg
) == HasArg
)
179 return _error
->Error("Option %s requires an argument.",argv
[I
]);
183 // Parse a configuration file
184 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
185 return ReadConfigFile(*Conf
,Argument
);
187 // Arbitary item specification
188 if ((A
->Flags
& ArbItem
) == ArbItem
)
191 for (J
= Argument
; *J
!= 0 && *J
!= '='; J
++);
193 return _error
->Error("Option %s: Configuration item sepecification must have an =<val>.",argv
[I
]);
199 return _error
->Error("Option %s: Configuration item sepecification must have an =<val>.",argv
[I
]);
200 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
203 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
208 Conf
->Set(A
->ConfName
,Argument
);
212 // Option is an integer level
213 if ((A
->Flags
& IntLevel
) == IntLevel
)
215 // There might be an argument
219 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
221 // Conversion failed and the argument was specified with an =s
222 if (EndPtr
== Argument
&& CertainArg
== true)
223 return _error
->Error("Option %s requires an integer argument, not '%s'",argv
[I
],Argument
);
225 // Conversion was ok, set the value and return
226 if (EndPtr
!= Argument
)
228 Conf
->Set(A
->ConfName
,Value
);
235 // Increase the level
236 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
240 // Option is a boolean
241 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
243 // Look for an argument.
246 // Look at preceeding text
250 if (PreceedMatch
== false)
253 if (strlen(argv
[I
]) >= sizeof(Buffer
))
254 return _error
->Error("Option '%s' is too long",argv
[I
]);
256 // Skip the leading dash
257 const char *J
= argv
[I
];
258 for (; *J
!= 0 && *J
== '-'; J
++);
260 const char *JEnd
= J
;
261 for (; *JEnd
!= 0 && *JEnd
!= '-'; JEnd
++);
264 strncpy(Buffer
,J
,JEnd
- J
);
265 Buffer
[JEnd
- J
] = 0;
274 Sense
= StringToBool(Argument
);
278 if (Argument
!= Buffer
)
286 if (CertainArg
== true)
287 return _error
->Error("Sense %s is not understood, try true or false.",Argument
);
292 // Indeterminate sense depends on the flag
295 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
301 Conf
->Set(A
->ConfName
,Sense
);
305 // CommandLine::FileSize - Count the number of filenames /*{{{*/
306 // ---------------------------------------------------------------------
308 unsigned int CommandLine::FileSize() const
310 unsigned int Count
= 0;
311 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
316 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
317 // ---------------------------------------------------------------------
319 bool CommandLine::DispatchArg(Dispatch
*Map
)
322 for (I
= 0; Map
[I
].Match
!= 0; I
++)
324 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
326 bool Res
= Map
[I
].Handler(*this);
327 if (Res
== false && _error
->PendingError() == false)
328 _error
->Error("Handler silently failed");
334 if (Map
[I
].Match
== 0)
335 _error
->Error("Invalid operation %s",FileList
[0]);