1 /*
2  *  This file is part of nzbget. See <http://nzbget.net>.
3  *
4  *  Copyright (C) 2007-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #ifndef COMMANDLINEPARSER_H
22 #define COMMANDLINEPARSER_H
23 
24 #include "NString.h"
25 
26 class CommandLineParser
27 {
28 public:
29 	enum EClientOperation
30 	{
31 		opClientNoOperation,
32 		opClientRequestDownload,
33 		opClientRequestListFiles,
34 		opClientRequestListGroups,
35 		opClientRequestListStatus,
36 		opClientRequestSetRate,
37 		opClientRequestDumpDebug,
38 		opClientRequestEditQueue,
39 		opClientRequestLog,
40 		opClientRequestShutdown,
41 		opClientRequestReload,
42 		opClientRequestVersion,
43 		opClientRequestPostQueue,
44 		opClientRequestWriteLog,
45 		opClientRequestScanSync,
46 		opClientRequestScanAsync,
47 		opClientRequestDownloadPause,
48 		opClientRequestDownloadUnpause,
49 		opClientRequestPostPause,
50 		opClientRequestPostUnpause,
51 		opClientRequestScanPause,
52 		opClientRequestScanUnpause,
53 		opClientRequestHistory,
54 		opClientRequestHistoryAll
55 	};
56 	enum EMatchMode
57 	{
58 		mmId = 1,
59 		mmName,
60 		mmRegEx
61 	};
62 
63 	typedef std::vector<int> IdList;
64 	typedef std::vector<CString> NameList;
65 
66 	CommandLineParser(int argc, const char* argv[]);
67 	void PrintUsage(const char* com);
GetErrors()68 	bool GetErrors() { return m_errors; }
GetNoConfig()69 	bool GetNoConfig() { return m_noConfig; }
GetConfigFilename()70 	const char* GetConfigFilename() { return m_configFilename; }
GetServerMode()71 	bool GetServerMode() { return m_serverMode; }
GetDaemonMode()72 	bool GetDaemonMode() { return m_daemonMode; }
GetRemoteClientMode()73 	bool GetRemoteClientMode() { return m_remoteClientMode; }
GetClientOperation()74 	EClientOperation GetClientOperation() { return m_clientOperation; }
GetOptionList()75 	NameList* GetOptionList() { return &m_optionList; }
GetEditQueueAction()76 	int GetEditQueueAction() { return m_editQueueAction; }
GetEditQueueOffset()77 	int GetEditQueueOffset() { return m_editQueueOffset; }
GetEditQueueIdList()78 	IdList* GetEditQueueIdList() { return &m_editQueueIdList; }
GetEditQueueNameList()79 	NameList* GetEditQueueNameList() { return &m_editQueueNameList; }
GetMatchMode()80 	EMatchMode GetMatchMode() { return m_matchMode; }
GetEditQueueText()81 	const char* GetEditQueueText() { return m_editQueueText; }
GetArgFilename()82 	const char* GetArgFilename() { return m_argFilename; }
GetAddCategory()83 	const char* GetAddCategory() { return m_addCategory; }
GetAddPaused()84 	bool GetAddPaused() { return m_addPaused; }
GetLastArg()85 	const char* GetLastArg() { return m_lastArg; }
GetAddPriority()86 	int GetAddPriority() { return m_addPriority; }
GetAddNzbFilename()87 	const char* GetAddNzbFilename() { return m_addNzbFilename; }
GetAddTop()88 	bool GetAddTop() { return m_addTop; }
GetAddDupeKey()89 	const char* GetAddDupeKey() { return m_addDupeKey; }
GetAddDupeScore()90 	int GetAddDupeScore() { return m_addDupeScore; }
GetAddDupeMode()91 	int GetAddDupeMode() { return m_addDupeMode; }
GetSetRate()92 	int GetSetRate() { return m_setRate; }
GetLogLines()93 	int GetLogLines() { return m_logLines; }
GetWriteLogKind()94 	int GetWriteLogKind() { return m_writeLogKind; }
GetTestBacktrace()95 	bool GetTestBacktrace() { return m_testBacktrace; }
GetWebGet()96 	bool GetWebGet() { return m_webGet; }
GetWebGetFilename()97 	const char* GetWebGetFilename() { return m_webGetFilename; }
GetSigVerify()98 	bool GetSigVerify() { return m_sigVerify; }
GetPubKeyFilename()99 	const char* GetPubKeyFilename() { return m_pubKeyFilename; }
GetSigFilename()100 	const char* GetSigFilename() { return m_sigFilename; }
GetPrintOptions()101 	bool GetPrintOptions() { return m_printOptions; }
GetPrintVersion()102 	bool GetPrintVersion() { return m_printVersion; }
GetPrintUsage()103 	bool GetPrintUsage() { return m_printUsage; }
GetPauseDownload()104 	bool GetPauseDownload() const { return m_pauseDownload; }
105 
106 private:
107 	bool m_noConfig = false;
108 	CString m_configFilename;
109 
110 	// Parsed command-line parameters
111 	bool m_errors = false;
112 	bool m_printVersion = false;
113 	bool m_printUsage = false;
114 	bool m_serverMode = false;
115 	bool m_daemonMode = false;
116 	bool m_remoteClientMode = false;
117 	EClientOperation m_clientOperation;
118 	NameList m_optionList;
119 	int m_editQueueAction = 0;
120 	int m_editQueueOffset = 0;
121 	IdList m_editQueueIdList;
122 	NameList m_editQueueNameList;
123 	EMatchMode m_matchMode = mmId;
124 	CString m_editQueueText;
125 	CString m_argFilename;
126 	CString m_addCategory;
127 	int m_addPriority = 0;
128 	bool m_addPaused = false;
129 	CString m_addNzbFilename;
130 	CString m_lastArg;
131 	bool m_printOptions = false;
132 	bool m_addTop = false;
133 	CString m_addDupeKey;
134 	int m_addDupeScore = 0;
135 	int m_addDupeMode = 0;
136 	int m_setRate = 0;
137 	int m_logLines = 0;
138 	int m_writeLogKind = 0;
139 	bool m_testBacktrace = false;
140 	bool m_webGet = false;
141 	CString m_webGetFilename;
142 	bool m_sigVerify = false;
143 	CString m_pubKeyFilename;
144 	CString m_sigFilename;
145 	bool m_pauseDownload = false;
146 
147 	void InitCommandLine(int argc, const char* argv[]);
148 	void InitFileArg(int argc, const char* argv[]);
149 	void ParseFileIdList(int argc, const char* argv[], int optind);
150 	void ParseFileNameList(int argc, const char* argv[], int optind);
151 	void ReportError(const char* errMessage);
152 };
153 
154 #endif
155