Switch from native command-line processing to wxCmdLineParser
[ctsim.git] / src / ctsim.cpp
index 48dd5360a9646e3807fef7ff3cd47b0c1f569bbb..9b7957966c5fbc06b5668510edf909e944be53c1 100644 (file)
@@ -7,9 +7,7 @@
 **   Date Started:  July 2000
 **
 **  This is part of the CTSim program
-**  Copyright (c) 1983-2001 Kevin Rosenberg
-**
-**  $Id$
+**  Copyright (c) 1983-2009 Kevin Rosenberg
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
@@ -38,6 +36,7 @@
 #include "wx/image.h"
 #include "wx/filesys.h"
 #include "wx/fs_zip.h"
+#include "wx/cmdline.h"
 #ifdef __WXMSW__
 #include "wx/msw/helpchm.h"
 #endif
 #endif
 #endif
 
-static const char* rcsindent = "$Id$";
-
-struct option CTSimApp::ctsimOptions[] =
-{
-  {"help", 0, 0, O_HELP},
-  {"version", 0, 0, O_VERSION},
-  {"print", 0, 0, O_PRINT},
-  {0, 0, 0, 0}
-};
 
 IMPLEMENT_APP(CTSimApp)
 
 CTSimApp::CTSimApp()
 : m_bAdvancedOptions(false), m_bSetModifyNewDocs(true), 
   m_bVerboseLogging(false), m_bShowStartupTips(true),
-m_iCurrentTip(0), m_bUseBackgroundTasks(false),
-m_docManager(NULL), m_pFrame(NULL), m_pConfig(0), m_pLog(0), m_pLogDoc(0)
+  m_iCurrentTip(0), m_bUseBackgroundTasks(false),
+  m_docManager(NULL), m_pFrame(NULL), m_pConfig(0), m_pLog(0), m_pLogDoc(0),
+  m_bPrintCmdLineImages(false), m_bCmdLineVerboseFlag(false)
 {
   theApp = this;
 }
@@ -98,10 +89,57 @@ m_docManager(NULL), m_pFrame(NULL), m_pConfig(0), m_pLog(0), m_pLogDoc(0)
 #include <sys/resource.h>
 #endif
 
+
+void CTSimApp::OnInitCmdLine(wxCmdLineParser& parser)
+{
+  static const wxCmdLineEntryDesc cmdLineDesc[] = {
+    { wxCMD_LINE_SWITCH, _T("l"), _T("verbose"), _T("verbose logging") },
+    { wxCMD_LINE_SWITCH, _T("v"), _T("version"), _T("print version") },
+    { wxCMD_LINE_SWITCH, _T("p"), _T("print"), _T("print images from command line"),
+      wxCMD_LINE_VAL_NONE,
+      wxCMD_LINE_PARAM_OPTIONAL },
+    { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("print this help message"),
+      wxCMD_LINE_VAL_NONE,
+      wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_OPTION_HELP },
+    { wxCMD_LINE_PARAM, NULL, NULL, _T("input file"),
+      wxCMD_LINE_VAL_STRING,
+      wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
+    { wxCMD_LINE_NONE }
+  };
+
+  parser.SetDesc(cmdLineDesc);
+}
+
+bool CTSimApp::OnCmdLineParsed(wxCmdLineParser& parser)
+{
+  if (wxApp::OnCmdLineParsed(parser) == false)
+    return false;
+
+  if (parser.Found(_T("version"))) {
+#ifdef VERSION
+      std::cout << "Version: " << VERSION << std::endl;
+#elif defined(CTSIMVERSION)
+      std::cout << "Version: " << CTSIMVERSION << std::endl;
+#else
+      std::cout << "Version: " << "Unknown" << std::endl;
+#endif
+      return false;
+  }
+  if (parser.Found(_T("print"))) {
+    m_bPrintCmdLineImages = true;
+  }
+  if (parser.Found(_T("verbose"))) {
+    m_bCmdLineVerboseFlag = true;
+  }
+
+  return true;
+}
+
 bool
 CTSimApp::OnInit()
 {
-  wxApp::OnInit();
+  if (! wxApp::OnInit())
+    return false;
 
 #ifdef HAVE_SETPRIORITY
   setpriority (PRIO_PROCESS, 0, 15);  // set to low scheduling priority
@@ -110,50 +148,6 @@ CTSimApp::OnInit()
   openConfig();
 
   g_bRunningWXWindows = true;
-  bool bPrintFiles = false;
-  // process options
-  while (1) {
-#ifdef __WXMAC__
-    int c = -1;
-#else
-    char** cargv = new char* [argc];
-    for (int i = 0; i < argc; i++) {
-      const char* p = wxConvUTF8.cWX2MB(argv[i]);
-      cargv[i] = new char [strlen(p)+1];
-      strcpy(cargv[i], p);
-    }
-    int c = getopt_long (argc, cargv, "", ctsimOptions, NULL);
-    for (int i = 0; i < argc; i++) {
-      delete cargv[i];
-    }
-    delete cargv;
-    
-#endif
-    if (c == -1)
-      break;
-
-    switch (c) {
-    case O_VERSION:
-      std::cout << rcsindent << std::endl;
-#ifdef VERSION
-      std::cout << "Version: " << VERSION << std::endl;
-#elif defined(CTSIMVERSION)
-      std::cout << "Version: " << CTSIMVERSION << std::endl;
-#endif
-      exit(0);
-    case O_HELP:
-    case '?':
-      usage (wxConvCurrent->cWX2MB(argv[0]));
-      exit (0);
-    case O_PRINT:
-      bPrintFiles = true;
-      break;
-    default:
-      usage (wxConvCurrent->cWX2MB(argv[0]));
-      exit (1);
-    }
-  }
-
   m_docManager = new wxDocManager (wxDEFAULT_DOCMAN_FLAGS, true);
 
   m_pDocTemplImage = new wxDocTemplate (m_docManager, _T("ImageFile"), _T("*.if"), _T(""), _T("if"), _T("ImageFile"), _T("ImageView"), CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
@@ -179,13 +173,13 @@ CTSimApp::OnInit()
   int xDisplay, yDisplay;
   ::wxDisplaySize (&xDisplay, &yDisplay);
 
-  m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, _T("CTSim"), wxPoint(0, 0),
 #ifdef CTSIM_MDI
-    wxSize(nearest<int>(xDisplay * .75), nearest<int>(yDisplay * .75)),
+  wxSize frameSize(nearest<int>(xDisplay * .75), nearest<int>(yDisplay * .75));
 #else
-    wxSize(nearest<int>(xDisplay * .6), nearest<int>(yDisplay * .4)),
+  wxSize frameSize(nearest<int>(xDisplay * .6), nearest<int>(yDisplay * .4));
 #endif
-    wxDEFAULT_FRAME_STYLE);
+
+  m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, _T("CTSim"), wxPoint(0, 0), frameSize, wxDEFAULT_FRAME_STYLE);
 
   setIconForFrame (m_pFrame);
   m_pFrame->Centre(wxBOTH);
@@ -209,7 +203,7 @@ CTSimApp::OnInit()
     m_pLogDoc->getView()->getFrame()->Show (true);
   } else
 #else
-    m_pLog = new wxTextCtrl (m_pFrame, -1, _T("Log Window\n"), wxPoint(0, 0), wxSize(0,0), wxTE_MULTILINE | wxTE_READONLY);
+    m_pLog = new wxTextCtrl (m_pFrame, -1, _T("Log Window\n"), wxPoint(0, 0), frameSize, wxTE_MULTILINE | wxTE_READONLY);
 #endif
   wxLog::SetActiveTarget (new wxLogTextCtrl(m_pLog));
 
@@ -240,7 +234,7 @@ CTSimApp::OnInit()
   for (int i = optind + 1; i <= argc; i++) {
     wxString filename = argv [i - 1];
     wxDocument* pNewDoc = m_docManager->CreateDocument (filename, wxDOC_SILENT);
-    if (bPrintFiles) {
+    if (m_bPrintCmdLineImages) {
       wxView* pNewView = pNewDoc->GetFirstView();
       wxPrintout *printout = pNewView->OnCreatePrintout();
       if (printout) {
@@ -253,7 +247,7 @@ CTSimApp::OnInit()
       m_docManager->OnFileClose (nullEvent);
     }
   }
-  if (bPrintFiles) {
+  if (m_bPrintCmdLineImages) {
     wxCommandEvent closeEvent;
     closeEvent.SetInt (MAINMENU_FILE_EXIT);
     m_pFrame->AddPendingEvent(closeEvent);