PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/util/options/GetLongOpt.h

https://bitbucket.org/asarje/mpqc-devel
C Header | 82 lines | 42 code | 12 blank | 28 comment | 0 complexity | 0ec05fc40a875d0fdeb3799614e38a3b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. /* $Id$ */
  2. /* S Manoharan. Advanced Computer Research Institute. Lyon. France */
  3. #ifndef _GetLongOpt_h_
  4. #define _GetLongOpt_h_
  5. #include <iostream>
  6. #include <string.h>
  7. namespace sc {
  8. /// Parse command line options.
  9. class GetLongOpt {
  10. public:
  11. /// Used by the enroll member to specify whether or not a value is
  12. /// expected or optional.
  13. enum OptType {
  14. NoValue, OptionalValue, MandatoryValue
  15. };
  16. private:
  17. struct Cell {
  18. const char *option; // option name
  19. OptType type; // option type
  20. const char *description; // a description of option
  21. const char *value; // value of option (string)
  22. Cell *next; // pointer to the next cell
  23. Cell() { option = description = value = 0; next = 0; }
  24. };
  25. private:
  26. Cell *table; // option table
  27. const char *ustring; // usage message
  28. char *pname; // program basename
  29. char optmarker; // option marker
  30. int enroll_done; // finished enrolling
  31. Cell *last; // last entry in option table
  32. private:
  33. char *basename(char * const p) const;
  34. int setcell(Cell *c, const char *valtoken, const char *nexttoken, const char *p);
  35. public:
  36. /// Initialize the object.
  37. /// @param optmark the option flag marker (default is <tt>-</tt>).
  38. GetLongOpt(const char optmark = '-');
  39. ~GetLongOpt();
  40. /// Parse command line options.
  41. /// @param argc the number of arguments, as passed to <tt>main</tt>
  42. /// @param argv the arguments, as passed to <tt>main</tt>
  43. /// @return the index to the start of arguments that were not
  44. /// processed (an error occurred if the return value is < 1)
  45. int parse(int argc, char * const *argv);
  46. /// Parse options in a string.
  47. /// @param str the string to be parsed
  48. /// @param p a prefix that will be prefixed to error messages
  49. /// @return the index to the start of arguments that were not
  50. /// processed (an error occurred if the return value is < 1)
  51. int parse(char * const str, char * const p);
  52. /// Enroll an option.
  53. /// @param opt the option name
  54. /// @param t whether or not a value is expected
  55. /// @param desc a description of the option
  56. /// @param val a default value for the option with an optional value
  57. int enroll(const char * const opt, const OptType t,
  58. const char * const desc, const char * const val);
  59. /// Retrieve an option.
  60. /// @param opt the name of the option
  61. const char *retrieve(const char * const opt) const;
  62. /// Print usage information.
  63. /// @param outfile stream to use for printing (default: <tt>std::cout</tt>)
  64. void usage(std::ostream &outfile = std::cout) const;
  65. /// Initialize usage synopsis.
  66. /// @param str the usage synopsis
  67. void usage(const char *str) { ustring = str; }
  68. };
  69. }
  70. #endif /* _GetLongOpt_h_ */