| 1 |
#include "cl_standard_paths.h" |
| 2 |
#include "csConfig.h" |
| 3 |
#include "file_logger.h" |
| 4 |
#include "main_app.h" |
| 5 |
#include <SocketAPI/clSocketServer.h> |
| 6 |
#include <iostream> |
| 7 |
#include <wx/filename.h> |
| 8 |
#include <wx/crt.h> |
| 9 |
|
| 10 |
IMPLEMENT_APP_CONSOLE(MainApp) |
| 11 |
|
| 12 |
static const wxCmdLineEntryDesc cmdLineDesc[] = { |
| 13 |
{ wxCMD_LINE_SWITCH, "v", "version", "Print current version", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, |
| 14 |
{ wxCMD_LINE_SWITCH, "h", "help", "Print usage", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, |
| 15 |
{ wxCMD_LINE_PARAM, "c", "command", "command", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, |
| 16 |
{ wxCMD_LINE_PARAM, "o", "options", "options", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, |
| 17 |
{ wxCMD_LINE_NONE } |
| 18 |
}; |
| 19 |
|
| 20 |
MainApp::MainApp() |
| 21 |
: m_manager(nullptr) |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
MainApp::~MainApp() {} |
| 26 |
|
| 27 |
int MainApp::OnExit() |
| 28 |
{ |
| 29 |
clDEBUG() << "Going down"; |
| 30 |
wxDELETE(m_manager); |
| 31 |
return TRUE; |
| 32 |
} |
| 33 |
|
| 34 |
bool MainApp::OnInit() |
| 35 |
{ |
| 36 |
SetAppName("codelite-cli"); |
| 37 |
|
| 38 |
// Make sure that the 'local' folder exists |
| 39 |
wxFileName::Mkdir(clStandardPaths::Get().GetUserDataDir(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL); |
| 40 |
|
| 41 |
// Mainly needed for Windows |
| 42 |
clSocketBase::Initialize(); |
| 43 |
|
| 44 |
// Allocate the manager class |
| 45 |
m_manager = new csManager(); |
| 46 |
|
| 47 |
wxCmdLineParser parser(wxAppConsole::argc, wxAppConsole::argv); |
| 48 |
DoParseCommandLine(parser); |
| 49 |
|
| 50 |
// Open the log file |
| 51 |
FileLogger::OpenLog("codelite-cli.log", FileLogger::Developer); |
| 52 |
|
| 53 |
// Startup |
| 54 |
return m_manager->Startup(); |
| 55 |
} |
| 56 |
|
| 57 |
bool MainApp::DoParseCommandLine(wxCmdLineParser& parser) |
| 58 |
{ |
| 59 |
parser.SetDesc(cmdLineDesc); |
| 60 |
if(parser.Parse(false) != 0) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
m_manager->GetCommand() = parser.GetParam(0); |
| 65 |
m_manager->GetOptions() = parser.GetParam(1); |
| 66 |
|
| 67 |
if(parser.Found("v")) { |
| 68 |
// Print version and exit |
| 69 |
std::cout << "codelite-cli v1.0" << std::endl; |
| 70 |
m_manager->SetExitNow(true); |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
if(parser.Found("h")) { |
| 75 |
m_manager->SetExitNow(true); |
| 76 |
PrintUsage(parser); |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
if(m_manager->GetCommand().IsEmpty()) { |
| 81 |
// Try to fetch the options from the INI file |
| 82 |
m_manager->LoadCommandFromINI(); |
| 83 |
if(m_manager->GetCommand().IsEmpty()) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
} |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
void MainApp::PrintUsage(const wxCmdLineParser& parser) |
| 91 |
{ |
| 92 |
wxString usageString = parser.GetUsageString(); |
| 93 |
std::cerr << usageString.mb_str(wxConvUTF8).data() << std::endl; |
| 94 |
} |