ApplicationUsage 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_APPLICATIONUSAGE
  14. #define OSG_APPLICATIONUSAGE 1
  15. #include <osg/Referenced>
  16. #include <map>
  17. #include <string>
  18. #include <ostream>
  19. namespace osg {
  20. class OSG_EXPORT ApplicationUsage : public osg::Referenced
  21. {
  22. public:
  23. static ApplicationUsage* instance();
  24. ApplicationUsage() {}
  25. ApplicationUsage(const std::string& commandLineUsage);
  26. typedef std::map<std::string,std::string> UsageMap;
  27. /** The ApplicationName is often displayed when logging errors, and frequently incorporated into the Description (below). */
  28. void setApplicationName(const std::string& name) { _applicationName = name; }
  29. const std::string& getApplicationName() const { return _applicationName; }
  30. /** If non-empty, the Description is typically shown by the Help Handler
  31. * as text on the Help display (which also lists keyboard abbreviations. */
  32. void setDescription(const std::string& desc) { _description = desc; }
  33. const std::string& getDescription() const { return _description; }
  34. enum Type
  35. {
  36. NO_HELP = 0x0,
  37. COMMAND_LINE_OPTION = 0x1,
  38. ENVIRONMENTAL_VARIABLE = 0x2,
  39. KEYBOARD_MOUSE_BINDING = 0x4,
  40. HELP_ALL = KEYBOARD_MOUSE_BINDING|ENVIRONMENTAL_VARIABLE|COMMAND_LINE_OPTION
  41. };
  42. void addUsageExplanation(Type type,const std::string& option,const std::string& explanation);
  43. void setCommandLineUsage(const std::string& explanation) { _commandLineUsage=explanation; }
  44. const std::string& getCommandLineUsage() const { return _commandLineUsage; }
  45. void addCommandLineOption(const std::string& option,const std::string& explanation, const std::string &defaultValue = "");
  46. void setCommandLineOptions(const UsageMap& usageMap) { _commandLineOptions=usageMap; }
  47. const UsageMap& getCommandLineOptions() const { return _commandLineOptions; }
  48. void setCommandLineOptionsDefaults(const UsageMap& usageMap) { _commandLineOptionsDefaults=usageMap; }
  49. const UsageMap& getCommandLineOptionsDefaults() const { return _commandLineOptionsDefaults; }
  50. void addEnvironmentalVariable(const std::string& option,const std::string& explanation, const std::string& defaultValue = "");
  51. void setEnvironmentalVariables(const UsageMap& usageMap) { _environmentalVariables=usageMap; }
  52. const UsageMap& getEnvironmentalVariables() const { return _environmentalVariables; }
  53. void setEnvironmentalVariablesDefaults(const UsageMap& usageMap) { _environmentalVariablesDefaults=usageMap; }
  54. const UsageMap& getEnvironmentalVariablesDefaults() const { return _environmentalVariablesDefaults; }
  55. void addKeyboardMouseBinding(const std::string& prefix, int key, const std::string& explanation);
  56. void addKeyboardMouseBinding(int key, const std::string& explanation);
  57. void addKeyboardMouseBinding(const std::string& option,const std::string& explanation);
  58. void setKeyboardMouseBindings(const UsageMap& usageMap) { _keyboardMouse=usageMap; }
  59. const UsageMap& getKeyboardMouseBindings() const { return _keyboardMouse; }
  60. void getFormattedString(std::string& str, const UsageMap& um,unsigned int widthOfOutput=80,bool showDefaults=false,const UsageMap& ud=UsageMap());
  61. void write(std::ostream& output,const UsageMap& um,unsigned int widthOfOutput=80,bool showDefaults=false,const UsageMap& ud=UsageMap());
  62. void write(std::ostream& output,unsigned int type=COMMAND_LINE_OPTION, unsigned int widthOfOutput=80,bool showDefaults=false);
  63. void writeEnvironmentSettings(std::ostream& output);
  64. protected:
  65. virtual ~ApplicationUsage() {}
  66. std::string _applicationName;
  67. std::string _description;
  68. std::string _commandLineUsage;
  69. UsageMap _commandLineOptions;
  70. UsageMap _environmentalVariables;
  71. UsageMap _keyboardMouse;
  72. UsageMap _environmentalVariablesDefaults;
  73. UsageMap _commandLineOptionsDefaults;
  74. };
  75. class ApplicationUsageProxy
  76. {
  77. public:
  78. /** register an explanation of commandline/environmentvariable/keyboard mouse usage.*/
  79. ApplicationUsageProxy(ApplicationUsage::Type type,const std::string& option,const std::string& explanation)
  80. {
  81. ApplicationUsage::instance()->addUsageExplanation(type,option,explanation);
  82. }
  83. };
  84. }
  85. #endif