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 #ifndef APT_8_CLEANER_HEADERS
00048 #include <apt-pkg/configuration.h>
00049 #endif
00050 
00051 class Configuration;
00052 
00053 class CommandLine
00054 {
00055    public:
00056    struct Args;
00057    struct Dispatch;
00058    
00059    protected:
00060    
00061    Args *ArgList;
00062    Configuration *Conf;
00063    bool HandleOpt(int &I,int argc,const char *argv[],
00064                   const char *&Opt,Args *A,bool PreceedeMatch = false);
00065    void static SaveInConfig(unsigned int const &argc, char const * const * const argv);
00066 
00067    public:
00068    
00069    enum AFlags 
00070    {
00071       HasArg = (1 << 0),
00072       IntLevel = (1 << 1),
00073       Boolean = (1 << 2),
00074       InvBoolean = (1 << 3),
00075       ConfigFile = (1 << 4) | HasArg,
00076       ArbItem = (1 << 5) | HasArg
00077    };
00078 
00079    const char **FileList;
00080    
00081    bool Parse(int argc,const char **argv);
00082    void ShowHelp();
00083    unsigned int FileSize() const;
00084    bool DispatchArg(Dispatch *List,bool NoMatch = true);
00085       
00086    CommandLine(Args *AList,Configuration *Conf);
00087    ~CommandLine();
00088 };
00089 
00090 struct CommandLine::Args
00091 {
00092    char ShortOpt;
00093    const char *LongOpt;
00094    const char *ConfName;
00095    unsigned long Flags;
00096    
00097    inline bool end() {return ShortOpt == 0 && LongOpt == 0;};
00098    inline bool IsBoolean() {return Flags == 0 || (Flags & (Boolean|InvBoolean)) != 0;};
00099 };
00100 
00101 struct CommandLine::Dispatch
00102 {
00103    const char *Match;
00104    bool (*Handler)(CommandLine &);
00105 };
00106 
00107 #endif