cmndline.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cmndline.h,v 1.7 1999/10/31 06:32:28 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}};
  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. ArbItem - Means the item is an arbitrary configuration string of
  32. the form item=value, where item is passed directly
  33. to the configuration class.
  34. The default, if the flags are 0 is to use Boolean
  35. ##################################################################### */
  36. /*}}}*/
  37. #ifndef PKGLIB_CMNDLINE_H
  38. #define PKGLIB_CMNDLINE_H
  39. #include <apt-pkg/macros.h>
  40. #ifndef APT_8_CLEANER_HEADERS
  41. #include <apt-pkg/configuration.h>
  42. #endif
  43. class Configuration;
  44. class CommandLine
  45. {
  46. public:
  47. struct Args;
  48. struct Dispatch;
  49. struct DispatchWithHelp;
  50. protected:
  51. Args *ArgList;
  52. Configuration *Conf;
  53. bool HandleOpt(int &I,int argc,const char *argv[],
  54. const char *&Opt,Args *A,bool PreceedeMatch = false);
  55. void static SaveInConfig(unsigned int const &argc, char const * const * const argv);
  56. public:
  57. enum AFlags
  58. {
  59. HasArg = (1 << 0),
  60. IntLevel = (1 << 1),
  61. Boolean = (1 << 2),
  62. InvBoolean = (1 << 3),
  63. ConfigFile = (1 << 4) | HasArg,
  64. ArbItem = (1 << 5) | HasArg
  65. };
  66. const char **FileList;
  67. bool Parse(int argc,const char **argv);
  68. void ShowHelp();
  69. unsigned int FileSize() const APT_PURE;
  70. // FIXME: merge on next ABI break
  71. bool DispatchArg(Dispatch *List,bool NoMatch = true);
  72. bool DispatchArg(Dispatch const * const List,bool NoMatch = true);
  73. static char const * GetCommand(Dispatch const * const Map,
  74. unsigned int const argc, char const * const * const argv) APT_PURE;
  75. static CommandLine::Args MakeArgs(char ShortOpt, char const *LongOpt,
  76. char const *ConfName, unsigned long Flags) APT_CONST;
  77. CommandLine();
  78. CommandLine(Args *AList,Configuration *Conf);
  79. ~CommandLine();
  80. };
  81. struct CommandLine::Args
  82. {
  83. char ShortOpt;
  84. const char *LongOpt;
  85. const char *ConfName;
  86. unsigned long Flags;
  87. inline bool end() {return ShortOpt == 0 && LongOpt == 0;};
  88. inline bool IsBoolean() {return Flags == 0 || (Flags & (Boolean|InvBoolean)) != 0;};
  89. };
  90. struct CommandLine::Dispatch
  91. {
  92. const char *Match;
  93. bool (*Handler)(CommandLine &);
  94. };
  95. #endif