hydrogen 1.2.6
Parser.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2008-2025 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
4 *
5 * http://www.hydrogen-music.org
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY, without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see https://www.gnu.org/licenses
19 *
20 */
21#include "Parser.h"
22
23#include <QtGui>
24#include <QtWidgets>
25#include <QCommandLineParser>
26#include <QCoreApplication>
27#include <QStringList>
28#include <iostream>
29
32#include <core/Version.h>
33
36
39
40bool Parser::parse( int argc, char* argv[] ) {
41 QCoreApplication* pApp = nullptr;
42 if ( QCoreApplication::instance() == nullptr) {
43 pApp = new QCoreApplication( argc, argv );
44 pApp->setApplicationVersion(
45 QString::fromStdString( H2Core::get_version() ) );
46 assert( QCoreApplication::instance() == pApp );
47 }
48
49 QCommandLineParser parser;
50
51 parser.setApplicationDescription( H2Core::getAboutText() );
52
53 QStringList availableAudioDrivers;
54 for ( const auto& ddriver : H2Core::Preferences::getSupportedAudioDrivers() ) {
55 availableAudioDrivers <<
57 }
58 availableAudioDrivers << H2Core::Preferences::audioDriverToQString(
60
61 QCommandLineOption audioDriverOption(
62 QStringList() << "d" << "driver",
63 QString( "Use the selected audio driver (%1)" )
64 .arg( availableAudioDrivers.join( ", " ) ), "Audiodriver");
65 QCommandLineOption playlistFileNameOption(
66 QStringList() << "p" <<
67 "playlist", "Load a playlist (*.h2playlist) at startup", "File" );
68 QCommandLineOption songFileOption(
69 QStringList() << "s" <<
70 "song", "Load a song (*.h2song) at startup", "File" );
71 QCommandLineOption kitOption(
72 QStringList() << "k" << "kit", "Load a drumkit at startup", "DrumkitName" );
73
74 QCommandLineOption installDrumkitOption(
75 QStringList() << "i" <<
76 "install", "Install a drumkit (*.h2drumkit)", "File");
77
78 QCommandLineOption verboseOption(
79 QStringList() << "V" <<
80 "verbose", "Level, if present, may be None, Error, Warning, Info, Debug, Constructors, Locks, or 0xHHHH", "Level" );
81 QCommandLineOption logFileOption(
82 QStringList() << "L" << "log-file", "Alternative log file path", "Path" );
83 QCommandLineOption logTimestampsOption(
84 QStringList() << "T" <<
85 "log-timestamps", "Add timestamps to all log messages" );
86 QCommandLineOption logColorsOption(
87 QStringList() << "log-colors", "Use ANSI colors in log messages" );
88 QCommandLineOption noLogColorsOption(
89 QStringList() << "no-log-colors",
90 "Suppress ANSI colors in log messages" );
91
92 QCommandLineOption systemDataPathOption(
93 QStringList() << "P" <<
94 "data", "Use an alternate system data path", "Path" );
95 QCommandLineOption userDataPathOption(
96 QStringList() << "user-data", "Use an alternate user data path", "Path" );
97 QCommandLineOption configFileOption(
98 QStringList() << "config", "Use an alternate config file", "Path" );
99 QCommandLineOption uiLayoutOption(
100 QStringList() << "layout", "UI layout ('tabbed' or 'single')", "Layout" );
101#ifdef H2CORE_HAVE_OSC
102 QCommandLineOption oscPortOption(
103 QStringList() << "O" <<
104 "osc-port", "Custom port for OSC connections", "int" );
105#endif
106 QCommandLineOption noSplashScreenOption(
107 QStringList() << "n" << "nosplash", "Hide splash screen" );
108
109 QCommandLineOption shotListOption(
110 QStringList() << "t" <<
111 "shotlist", "Shot list of widgets to grab", "ShotList" );
112
113 QCommandLineOption noReporterOption(
114 QStringList() << "child", "Child process (no crash reporter)");
115
116 parser.addHelpOption();
117 parser.addVersionOption();
118
119 parser.addOption( audioDriverOption );
120 parser.addOption( playlistFileNameOption );
121 parser.addOption( songFileOption );
122 parser.addOption( kitOption );
123
124 parser.addOption( installDrumkitOption );
125
126 parser.addOption( verboseOption );
127 parser.addOption( logFileOption );
128 parser.addOption( logTimestampsOption );
129 parser.addOption( logColorsOption );
130 parser.addOption( noLogColorsOption );
131
132 parser.addOption( systemDataPathOption );
133 parser.addOption( userDataPathOption );
134 parser.addOption( configFileOption );
135 parser.addOption( uiLayoutOption );
136#ifdef H2CORE_HAVE_OSC
137 parser.addOption( oscPortOption );
138#endif
139 parser.addOption( noSplashScreenOption );
140
141 parser.addOption( shotListOption );
142
143 parser.addOption( noReporterOption );
144
145 parser.addPositionalArgument( "file", "Song, playlist or Drumkit file" );
146
147 // Evaluate the options
148 parser.process( *( QCoreApplication::instance() ) );
149
150 m_sAudioDriver = parser.value( audioDriverOption );
151 m_sPlaylistFilename = parser.value( playlistFileNameOption );
152 m_sSongFilename = parser.value ( songFileOption );
153 m_sDrumkitToLoad = parser.value( kitOption );
154
155 m_sInstallDrumkitPath = parser.value( installDrumkitOption );
156
157 QString sVerbosityString = parser.value( verboseOption );
159 if ( parser.isSet( verboseOption ) ) {
160 if ( ! sVerbosityString.isEmpty() ) {
162 sVerbosityString.toLocal8Bit() );
163 } else {
165 }
166 }
167 m_sLogFile = parser.value( logFileOption );
168 m_bLogTimestamps = parser.isSet( logTimestampsOption );
169
170#ifdef WIN32
171 m_bLogColors = false;
172#else
173 m_bLogColors = true;
174#endif
175
176 // If both options are present, having colors wins.
177 if ( parser.isSet( logColorsOption ) ) {
178 m_bLogColors = true;
179 }
180 else if ( parser.isSet( noLogColorsOption ) ) {
181 m_bLogColors = false;
182 }
183
184 m_sSysDataPath = parser.value( systemDataPathOption );
185 m_sUsrDataPath = parser.value( userDataPathOption );
186 m_sConfigFilePath = parser.value( configFileOption );
187 m_sUiLayout = parser.value( uiLayoutOption );
188 m_nOscPort = -1;
189#ifdef H2CORE_HAVE_OSC
190 QString sOscPort = parser.value( oscPortOption );
191 if ( ! sOscPort.isEmpty() ) {
192 bool bOk;
193 m_nOscPort = sOscPort.toInt( &bOk, 10 );
194 if ( ! bOk ) {
195 std::cerr << "Unable to parse 'osc-port' option. Please provide an integer value"
196 << std::endl;
197 return false;
198 }
199 }
200#endif
201
202 m_bNoSplashScreen = parser.isSet( noSplashScreenOption );
203
204 m_sShotList = parser.value( shotListOption );
205
206 m_bNoReporter = parser.isSet( noReporterOption );
207
208 // Operating system GUIs typically pass documents to open as simple
209 // positional arguments to the process command line. Handling this here
210 // enables "Open with" as well as default document bindings to work.
211 QString sArg;
212 foreach ( sArg, parser.positionalArguments() ) {
213 if ( sArg.endsWith( H2Core::Filesystem::songs_ext ) ) {
214 m_sSongFilename = sArg;
215 }
216 if ( sArg.endsWith( H2Core::Filesystem::drumkit_ext ) ) {
218 }
219 if ( sArg.endsWith( H2Core::Filesystem::playlist_ext ) ) {
220 m_sPlaylistFilename = sArg;
221 }
222 }
223
224 if ( pApp != nullptr ) {
225 delete pApp;
226 }
227
228 return true;
229}
static const QString playlist_ext
Definition Filesystem.h:120
static const QString drumkit_ext
Definition Filesystem.h:121
static const QString songs_ext
Definition Filesystem.h:117
static unsigned parse_log_level(const char *lvl)
parse a log level string and return the corresponding bit mask
Definition Logger.cpp:289
static std::vector< AudioDriver > getSupportedAudioDrivers()
static QString audioDriverToQString(const AudioDriver &driver)
QString m_sUiLayout
Definition Parser.h:109
bool m_bLogColors
Definition Parser.h:104
QString m_sDrumkitToLoad
Definition Parser.h:97
QString m_sPlaylistFilename
Definition Parser.h:95
bool m_bNoReporter
Definition Parser.h:115
QString m_sAudioDriver
Definition Parser.h:94
QString m_sConfigFilePath
Definition Parser.h:108
QString m_sSongFilename
Definition Parser.h:96
QString m_sLogFile
Definition Parser.h:102
Parser()
Definition Parser.cpp:34
QString m_sUsrDataPath
Definition Parser.h:107
bool m_bNoSplashScreen
Definition Parser.h:111
unsigned m_logLevel
Definition Parser.h:101
QString m_sInstallDrumkitPath
Definition Parser.h:99
QString m_sShotList
Definition Parser.h:113
bool parse(int argc, char *argv[])
Definition Parser.cpp:40
QString m_sSysDataPath
Definition Parser.h:106
bool m_bLogTimestamps
Definition Parser.h:103
int m_nOscPort
Definition Parser.h:110
~Parser()
Definition Parser.cpp:37
QString getAboutText()
Definition Version.cpp:54
std::string get_version()
Returns the current Hydrogen version string.
Definition Version.cpp:30