apt @VERSION@
cmndline.h
00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00003 // $Id: cmndline.h,v 1.7 1999/10/31 06:32:28 jgg Exp $
00004 /* ######################################################################
00005 
00006    Command Line Class - Sophisticated command line parser
00007    
00008    This class provides a unified command line parser/option handliner/
00009    configuration mechanism. It allows the caller to specify the option
00010    set and map the option set into the configuration class or other
00011    special functioning.
00012    
00013    Filenames are stripped from the option stream and put into their
00014    own array.
00015    
00016    The argument descriptor array can be initialized as:
00017    
00018  CommandLine::Args Args[] = 
00019  {{'q',"quiet","apt::get::quiet",CommandLine::IntLevel},
00020   {0,0,0,0}};
00021    
00022    The flags mean,
00023      HasArg - Means the argument has a value
00024      IntLevel - Means the argument is an integer level indication, the
00025                 following -qqqq (+3) -q5 (=5) -q=5 (=5) are valid
00026      Boolean  - Means it is true/false or yes/no. 
00027                 -d (true) --no-d (false) --yes-d (true)
00028                 --long (true) --no-long (false) --yes-long (true)
00029                 -d=yes (true) -d=no (false) Words like enable, disable,
00030                 true false, yes no and on off are recognized in logical 
00031                 places.
00032      InvBoolean - Same as boolean but the case with no specified sense
00033                   (first case) is set to false.
00034      ConfigFile - Means this flag should be interprited as the name of 
00035                   a config file to read in at this point in option processing.
00036                   Implies HasArg.
00037      ArbItem    - Means the item is an arbitrary configuration string of
00038                   the form item=value, where item is passed directly
00039                   to the configuration class.
00040    The default, if the flags are 0 is to use Boolean
00041    
00042    ##################################################################### */
00043                                                                         /*}}}*/
00044 #ifndef PKGLIB_CMNDLINE_H
00045 #define PKGLIB_CMNDLINE_H
00046 
00047 class Configuration;
00048 
00049 class CommandLine
00050 {
00051    public:
00052    struct Args;
00053    struct Dispatch;
00054    
00055    protected:
00056    
00057    Args *ArgList;
00058    Configuration *Conf;
00059    bool HandleOpt(int &I,int argc,const char *argv[],
00060                   const char *&Opt,Args *A,bool PreceedeMatch = false);
00061    void static SaveInConfig(unsigned int const &argc, char const * const * const argv);
00062 
00063    public:
00064    
00065    enum AFlags 
00066    {
00067       HasArg = (1 << 0),
00068       IntLevel = (1 << 1),
00069       Boolean = (1 << 2),
00070       InvBoolean = (1 << 3),
00071       ConfigFile = (1 << 4) | HasArg,
00072       ArbItem = (1 << 5) | HasArg
00073    };
00074 
00075    const char **FileList;
00076    
00077    bool Parse(int argc,const char **argv);
00078    void ShowHelp();
00079    unsigned int FileSize() const;
00080    bool DispatchArg(Dispatch *List,bool NoMatch = true);
00081       
00082    CommandLine(Args *AList,Configuration *Conf);
00083    ~CommandLine();
00084 };
00085 
00086 struct CommandLine::Args
00087 {
00088    char ShortOpt;
00089    const char *LongOpt;
00090    const char *ConfName;
00091    unsigned long Flags;
00092    
00093    inline bool end() {return ShortOpt == 0 && LongOpt == 0;};
00094    inline bool IsBoolean() {return Flags == 0 || (Flags & (Boolean|InvBoolean)) != 0;};
00095 };
00096 
00097 struct CommandLine::Dispatch
00098 {
00099    const char *Match;
00100    bool (*Handler)(CommandLine &);
00101 };
00102 
00103 #endif