]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cmndline.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cmndline.cc,v 1.4 1998/10/20 02:39:25 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)
93 for (; Opt
!= OptEnd
&& *Opt
!= '-'; Opt
++);
96 return _error
->Error("Command line option %s is not understood",argv
[I
]);
99 for (A
= ArgList
; A
->end() == false &&
100 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
103 if (A
->end() == true && OptEnd
- Opt
!= 1)
104 return _error
->Error("Command line option %s is not understood",argv
[I
]);
106 // The option could be a single letter option prefixed by a no-..
107 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
109 if (A
->end() == true)
110 return _error
->Error("Command line option %s is not understood",argv
[I
]);
112 // The option is not boolean
113 if (A
->IsBoolean() == false)
114 return _error
->Error("Command line option %s is not boolean",argv
[I
]);
119 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
) == false)
123 // Copy any remaining file names over
124 for (; I
!= argc
; I
++)
131 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
132 // ---------------------------------------------------------------------
133 /* This is a helper function for parser, it looks at a given argument
134 and looks for specific patterns in the string, it gets tokanized
135 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
136 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
137 const char *&Opt
,Args
*A
)
139 const char *Argument
= 0;
140 bool CertainArg
= false;
143 /* Determine the possible location of an option or 0 if their is
145 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
147 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
148 Argument
= argv
[I
+1];
150 // Equals was specified but we fell off the end!
151 if (Opt
[1] == '=' && Argument
== 0)
152 return _error
->Error("Option %s requires an argument.",argv
[I
]);
169 // Option is an argument set
170 if ((A
->Flags
& HasArg
) == HasArg
)
173 return _error
->Error("Option %s requires an argument.",argv
[I
]);
177 // Parse a configuration file
178 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
179 return ReadConfigFile(*Conf
,Argument
);
181 // Arbitary item specification
182 if ((A
->Flags
& ArbItem
) == ArbItem
)
185 for (J
= Argument
; *J
!= 0 && *J
!= '='; J
++);
187 return _error
->Error("Option %s: Configuration item sepecification must have an =<val>.",argv
[I
]);
193 return _error
->Error("Option %s: Configuration item sepecification must have an =<val>.",argv
[I
]);
194 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
197 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
202 Conf
->Set(A
->ConfName
,Argument
);
206 // Option is an integer level
207 if ((A
->Flags
& IntLevel
) == IntLevel
)
209 // There might be an argument
213 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
215 // Conversion failed and the argument was specified with an =s
216 if (EndPtr
== Argument
&& CertainArg
== true)
217 return _error
->Error("Option %s requires an integer argument, not '%s'",argv
[I
],Argument
);
219 // Conversion was ok, set the value and return
220 if (EndPtr
!= Argument
)
222 Conf
->Set(A
->ConfName
,Value
);
229 // Increase the level
230 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
234 // Option is a boolean
235 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
237 // Look for an argument.
240 // Look at preceeding text
244 if (strlen(argv
[I
]) >= sizeof(Buffer
))
245 return _error
->Error("Option '%s' is too long",argv
[I
]);
247 const char *J
= argv
[I
];
248 for (; *J
!= 0 && *J
== '-'; J
++);
249 const char *JEnd
= J
;
250 for (; *JEnd
!= 0 && *JEnd
!= '-'; JEnd
++);
253 strncpy(Buffer
,J
,JEnd
- J
);
254 Buffer
[JEnd
- J
] = 0;
263 Sense
= StringToBool(Argument
);
267 if (Argument
!= Buffer
)
275 if (CertainArg
== true)
276 return _error
->Error("Sense %s is not understood, try true or false.",Argument
);
281 // Indeterminate sense depends on the flag
284 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
290 Conf
->Set(A
->ConfName
,Sense
);
294 // CommandLine::FileSize - Count the number of filenames /*{{{*/
295 // ---------------------------------------------------------------------
297 unsigned int CommandLine::FileSize() const
299 unsigned int Count
= 0;
300 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)