cmndline.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cmndline.h,v 1.2 1998/12/29 04:38:09 jgg Exp $
  4. /* ######################################################################
  5. Command Line Class - Sophisticated command line parser
  6. This class provides a unified command line parser/option handliner/
  7. configuration mechanism. It allows the caller to specify the option
  8. set and map the option set into the configuration class or other
  9. special functioning.
  10. Filenames are stripped from the option stream and put into their
  11. own array.
  12. The argument descriptor array can be initialized as:
  13. CommandLine::Args Args[] =
  14. {{'q',"quiet","apt::get::quiet",CommandLine::IntLevel},
  15. {0,0,0,0,0}};
  16. The flags mean,
  17. HasArg - Means the argument has a value
  18. IntLevel - Means the argument is an integer level indication, the
  19. following -qqqq (+3) -q5 (=5) -q=5 (=5) are valid
  20. Boolean - Means it is true/false or yes/no.
  21. -d (true) --no-d (false) --yes-d (true)
  22. --long (true) --no-long (false) --yes-long (true)
  23. -d=yes (true) -d=no (false) Words like enable, disable,
  24. true false, yes no and on off are recognized in logical
  25. places.
  26. InvBoolean - Same as boolean but the case with no specified sense
  27. (first case) is set to false.
  28. ConfigFile - Means this flag should be interprited as the name of
  29. a config file to read in at this point in option processing.
  30. Implies HasArg.
  31. The default, if the flags are 0 is to use Boolean
  32. ##################################################################### */
  33. /*}}}*/
  34. #ifndef PKGLIB_CMNDLINE_H
  35. #define PKGLIB_CMNDLINE_H
  36. #ifdef __GNUG__
  37. #pragma interface "dsync/cmndline.h"
  38. #endif
  39. #include <dsync/configuration.h>
  40. class CommandLine
  41. {
  42. public:
  43. struct Args;
  44. struct Dispatch;
  45. protected:
  46. Args *ArgList;
  47. Configuration *Conf;
  48. bool HandleOpt(int &I,int argc,const char *argv[],
  49. const char *&Opt,Args *A,bool PreceedeMatch = false);
  50. public:
  51. enum AFlags
  52. {
  53. HasArg = (1 << 0),
  54. IntLevel = (1 << 1),
  55. Boolean = (1 << 2),
  56. InvBoolean = (1 << 3),
  57. ConfigFile = (1 << 4) | HasArg,
  58. ArbItem = (1 << 5) | HasArg
  59. };
  60. const char **FileList;
  61. bool Parse(int argc,const char **argv);
  62. void ShowHelp();
  63. unsigned int FileSize() const;
  64. bool DispatchArg(Dispatch *List,bool NoMatch = true);
  65. CommandLine(Args *AList,Configuration *Conf);
  66. ~CommandLine();
  67. };
  68. struct CommandLine::Args
  69. {
  70. char ShortOpt;
  71. const char *LongOpt;
  72. const char *ConfName;
  73. unsigned long Flags;
  74. inline bool end() {return ShortOpt == 0 && LongOpt == 0;};
  75. inline bool IsBoolean() {return Flags == 0 || (Flags & (Boolean|InvBoolean)) != 0;};
  76. };
  77. struct CommandLine::Dispatch
  78. {
  79. const char *Match;
  80. bool (*Handler)(CommandLine &);
  81. };
  82. #endif