options.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "options.hpp"
  2. slrn::Options* options = new slrn::Options();
  3. slrn::Options::Options() {
  4. m_borderSize = 10;
  5. m_padding = 0;
  6. m_xdisplay = ":0";
  7. m_tolerance = 4;
  8. }
  9. void slrn::Options::printHelp() {
  10. printf( "Usage: slrn [options]\n" );
  11. printf( "Print user selected region to stdout.\n" );
  12. printf( "\n" );
  13. printf( "options\n" );
  14. printf( " -h, --help show this message.\n" );
  15. printf( " -b=INT, --bordersize=INT set selection rectangle border size.\n" );
  16. printf( " -p=INT, --m_padding=INT set m_padding size for selection.\n" );
  17. printf( " -t=INT, --tolerance=INT if you have a shaky mouse, increasing this value will make slrn detect single clicks better. Rather than interpreting your shaky clicks as region selections.\n" );
  18. printf( " -x=STRING, --xdisplay=STRING set x display (STRING must be hostname:number.screen_number format)\n" );
  19. printf( "examples\n" );
  20. printf( " slrn -b=10 -x=:0 -p=-30 -t=4\n" );
  21. }
  22. int slrn::Options::parseOptions( int argc, char** argv ) {
  23. // Simple command parsing. Just uses sscanf to read each argument.
  24. // It looks complicated because you have to have spaces for delimiters for sscanf.
  25. for ( int i=0; i<argc; i++ ) {
  26. std::string arg = argv[i];
  27. if ( matches( arg, "--b=", "--bordersize=" ) ) {
  28. int err = parseInt( arg, &m_borderSize );
  29. if ( err ) {
  30. return 1;
  31. }
  32. if ( m_borderSize < 0 ) {
  33. m_borderSize = 0;
  34. }
  35. } else if ( matches( arg, "-p=", "--padding=" ) ) {
  36. int err = parseInt( arg, &m_padding );
  37. if ( err ) {
  38. return 1;
  39. }
  40. } else if ( matches( arg, "-t=", "--tolerance=" ) ) {
  41. int err = parseInt( arg, &m_tolerance );
  42. if ( err ) {
  43. return 1;
  44. }
  45. if ( m_tolerance < 0 ) {
  46. m_tolerance = 0;
  47. }
  48. } else if ( matches( arg, "-x=", "--xdisplay=" ) ) {
  49. int err = parseString( arg, &m_xdisplay );
  50. if ( err ) {
  51. return 1;
  52. }
  53. } else if ( arg == "-h" || arg == "--help" ) {
  54. printHelp();
  55. return 2;
  56. } else {
  57. if ( i == 0 ) {
  58. continue;
  59. }
  60. printf( "Error: Unknown argument %s\n", argv[i] );
  61. printf( "Try -h or --help for help.\n" );
  62. return 1;
  63. }
  64. }
  65. return 0;
  66. }
  67. int slrn::Options::parseInt( std::string arg, int* returnInt ) {
  68. std::string copy = arg;
  69. int find = copy.find( "=" );
  70. if ( find != copy.npos ) {
  71. copy.at( find ) = ' ';
  72. }
  73. // Just in case we error out, grab the actual argument name into x.
  74. char* x = new char[ arg.size() ];
  75. int num = sscanf( copy.c_str(), "%s %i", x, returnInt );
  76. if ( num != 2 ) {
  77. printf( "Error parsing command arguments near %s\n", arg.c_str() );
  78. printf( "Usage: %s=INT\n", x );
  79. printf( "Example: %s=10 or %s=-12\n", x, x );
  80. printf( "Try -h or --help for help.\n" );
  81. delete[] x;
  82. return 1;
  83. }
  84. delete[] x;
  85. return 0;
  86. }
  87. bool slrn::Options::matches( std::string arg, std::string shorthand, std::string longhand ) {
  88. if ( arg.substr( 0, shorthand.size() ) == shorthand ||
  89. arg.substr( 0, longhand.size() ) == longhand ) {
  90. return true;
  91. }
  92. return false;
  93. }
  94. int slrn::Options::parseString( std::string arg, std::string* returnString ) {
  95. std::string copy = arg;
  96. int find = copy.find( "=" );
  97. if ( find != copy.npos ) {
  98. copy.at( find ) = ' ';
  99. }
  100. // Just in case we error out, grab the actual argument name into x.
  101. char* x = new char[ arg.size() ];
  102. char* y = new char[ arg.size() ];
  103. int num = sscanf( copy.c_str(), "%s %s", x, y );
  104. if ( num != 2 ) {
  105. printf( "Error parsing command arguments near %s\n", arg.c_str() );
  106. printf( "Usage: %s=STRING\n", x );
  107. printf( "Example: %s=:0 or %s=hostname:0.1\n", x, x );
  108. printf( "Try -h or --help for help.\n" );
  109. delete[] x;
  110. delete[] y;
  111. return 1;
  112. }
  113. *returnString = y;
  114. delete[] x;
  115. delete[] y;
  116. return 0;
  117. }