console.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: samples/console/console.cpp
  3. // Purpose: A sample console (as opposed to GUI) program using wxWidgets
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 04.10.99
  7. // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers (this file is usually all you
  22. // need because it includes almost all "standard" wxWidgets headers)
  23. #ifndef WX_PRECOMP
  24. #include "wx/wx.h"
  25. #endif
  26. #include <wx/app.h>
  27. #include <wx/cmdline.h>
  28. // ============================================================================
  29. // implementation
  30. // ============================================================================
  31. static const wxCmdLineEntryDesc cmdLineDesc[] =
  32. {
  33. { wxCMD_LINE_SWITCH, "h", "help", "show this help message",
  34. wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
  35. { wxCMD_LINE_SWITCH, "d", "dummy", "a dummy switch" },
  36. // ... your other command line options here...
  37. { wxCMD_LINE_NONE }
  38. };
  39. int main(int argc, char **argv)
  40. {
  41. wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
  42. wxInitializer initializer;
  43. if ( !initializer )
  44. {
  45. fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
  46. return -1;
  47. }
  48. wxCmdLineParser parser(cmdLineDesc, argc, argv);
  49. switch ( parser.Parse() )
  50. {
  51. case -1:
  52. // help was given, terminating
  53. break;
  54. case 0:
  55. // everything is ok; proceed
  56. if (parser.Found("d"))
  57. {
  58. wxPrintf("Dummy switch was given...\n");
  59. while (1)
  60. {
  61. wxChar input[128];
  62. wxPrintf("Try to guess the magic number (type 'quit' to escape): ");
  63. if ( !wxFgets(input, WXSIZEOF(input), stdin) )
  64. break;
  65. // kill the last '\n'
  66. input[wxStrlen(input) - 1] = 0;
  67. if (wxStrcmp(input, "quit") == 0)
  68. break;
  69. long val;
  70. if (!wxString(input).ToLong(&val))
  71. {
  72. wxPrintf("Invalid number...\n");
  73. continue;
  74. }
  75. if (val == 42)
  76. wxPrintf("You guessed!\n");
  77. else
  78. wxPrintf("Bad luck!\n");
  79. }
  80. }
  81. break;
  82. default:
  83. break;
  84. }
  85. if ( argc == 1 )
  86. {
  87. // If there were no command-line options supplied, emit a message
  88. // otherwise it's not obvious that the sample ran successfully
  89. wxPrintf("Welcome to the wxWidgets 'console' sample!\n");
  90. wxPrintf("For more information, run it again with the --help option\n");
  91. }
  92. // do something useful here
  93. return 0;
  94. }