]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cmndline.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cmndline.cc,v 1.13 2002/09/14 05:29:22 jgg 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 /*{{{*/
15 #pragma implementation "apt-pkg/cmndline.h"
17 #include <apt-pkg/cmndline.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/strutl.h>
24 // CommandLine::CommandLine - Constructor /*{{{*/
25 // ---------------------------------------------------------------------
27 CommandLine::CommandLine(Args
*AList
,Configuration
*Conf
) : ArgList(AList
),
28 Conf(Conf
), FileList(0)
32 // CommandLine::~CommandLine - Destructor /*{{{*/
33 // ---------------------------------------------------------------------
35 CommandLine::~CommandLine()
40 // CommandLine::Parse - Main action member /*{{{*/
41 // ---------------------------------------------------------------------
43 bool CommandLine::Parse(int argc
,const char **argv
)
46 FileList
= new const char *[argc
];
47 const char **Files
= FileList
;
49 for (I
= 1; I
!= argc
; I
++)
51 const char *Opt
= argv
[I
];
53 // It is not an option
62 // Double dash signifies the end of option processing
63 if (*Opt
== '-' && Opt
[1] == 0)
69 // Single dash is a short option
72 // Iterate over each letter
75 // Search for the option
77 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
79 return _error
->Error(_("Command line option '%c' [from %s] is not known."),*Opt
,argv
[I
]);
81 if (HandleOpt(I
,argc
,argv
,Opt
,A
) == false)
91 // Match up to a = against the list
92 const char *OptEnd
= Opt
;
94 for (; *OptEnd
!= 0 && *OptEnd
!= '='; OptEnd
++);
95 for (A
= ArgList
; A
->end() == false &&
96 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
98 // Failed, look for a word after the first - (no-foo)
99 bool PreceedMatch
= false;
100 if (A
->end() == true)
102 for (; Opt
!= OptEnd
&& *Opt
!= '-'; Opt
++);
105 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
108 for (A
= ArgList
; A
->end() == false &&
109 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
112 if (A
->end() == true && OptEnd
- Opt
!= 1)
113 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
115 // The option could be a single letter option prefixed by a no-..
116 if (A
->end() == true)
118 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
120 if (A
->end() == true)
121 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
124 // The option is not boolean
125 if (A
->IsBoolean() == false)
126 return _error
->Error(_("Command line option %s is not boolean"),argv
[I
]);
132 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
136 // Copy any remaining file names over
137 for (; I
!= argc
; I
++)
144 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
145 // ---------------------------------------------------------------------
146 /* This is a helper function for parser, it looks at a given argument
147 and looks for specific patterns in the string, it gets tokanized
148 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
149 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
150 const char *&Opt
,Args
*A
,bool PreceedMatch
)
152 const char *Argument
= 0;
153 bool CertainArg
= false;
156 /* Determine the possible location of an option or 0 if their is
158 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
160 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
161 Argument
= argv
[I
+1];
163 // Equals was specified but we fell off the end!
164 if (Opt
[1] == '=' && Argument
== 0)
165 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
182 // Option is an argument set
183 if ((A
->Flags
& HasArg
) == HasArg
)
186 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
190 // Parse a configuration file
191 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
192 return ReadConfigFile(*Conf
,Argument
);
194 // Arbitary item specification
195 if ((A
->Flags
& ArbItem
) == ArbItem
)
198 for (J
= Argument
; *J
!= 0 && *J
!= '='; J
++);
200 return _error
->Error(_("Option %s: Configuration item sepecification must have an =<val>."),argv
[I
]);
206 return _error
->Error(_("Option %s: Configuration item sepecification must have an =<val>."),argv
[I
]);
207 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
210 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
215 const char *I
= A
->ConfName
;
216 for (; *I
!= 0 && *I
!= ' '; I
++);
218 Conf
->Set(string(A
->ConfName
,0,I
-A
->ConfName
),string(I
+1) + Argument
);
220 Conf
->Set(A
->ConfName
,string(I
) + Argument
);
225 // Option is an integer level
226 if ((A
->Flags
& IntLevel
) == IntLevel
)
228 // There might be an argument
232 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
234 // Conversion failed and the argument was specified with an =s
235 if (EndPtr
== Argument
&& CertainArg
== true)
236 return _error
->Error(_("Option %s requires an integer argument, not '%s'"),argv
[I
],Argument
);
238 // Conversion was ok, set the value and return
239 if (EndPtr
!= 0 && EndPtr
!= Argument
&& *EndPtr
== 0)
241 Conf
->Set(A
->ConfName
,Value
);
248 // Increase the level
249 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
253 // Option is a boolean
254 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
256 // Look for an argument.
259 // Look at preceeding text
263 if (PreceedMatch
== false)
266 if (strlen(argv
[I
]) >= sizeof(Buffer
))
267 return _error
->Error(_("Option '%s' is too long"),argv
[I
]);
269 // Skip the leading dash
270 const char *J
= argv
[I
];
271 for (; *J
!= 0 && *J
== '-'; J
++);
273 const char *JEnd
= J
;
274 for (; *JEnd
!= 0 && *JEnd
!= '-'; JEnd
++);
277 strncpy(Buffer
,J
,JEnd
- J
);
278 Buffer
[JEnd
- J
] = 0;
287 Sense
= StringToBool(Argument
);
291 if (Argument
!= Buffer
)
299 if (CertainArg
== true)
300 return _error
->Error(_("Sense %s is not understood, try true or false."),Argument
);
305 // Indeterminate sense depends on the flag
308 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
314 Conf
->Set(A
->ConfName
,Sense
);
318 // CommandLine::FileSize - Count the number of filenames /*{{{*/
319 // ---------------------------------------------------------------------
321 unsigned int CommandLine::FileSize() const
323 unsigned int Count
= 0;
324 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
329 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
330 // ---------------------------------------------------------------------
332 bool CommandLine::DispatchArg(Dispatch
*Map
,bool NoMatch
)
335 for (I
= 0; Map
[I
].Match
!= 0; I
++)
337 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
339 bool Res
= Map
[I
].Handler(*this);
340 if (Res
== false && _error
->PendingError() == false)
341 _error
->Error("Handler silently failed");
347 if (Map
[I
].Match
== 0)
350 _error
->Error(_("Invalid operation %s"),FileList
[0]);