Skip to content
Permalink
Browse files
[CLI] Add --version and -v to know QGIS version
  • Loading branch information
Gustry committed Jun 25, 2021
1 parent f14e098 commit bb9c45db6f03c666a36345b0d8a490914a630052
@@ -0,0 +1,40 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgscommandlineutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsCommandLineUtils
{
%Docstring(signature="appended")
Utils class for QGIS command line tools.

.. versionadded:: 3.22
%End

%TypeHeaderCode
#include "qgscommandlineutils.h"
%End
public:

static QString allVersions( );
%Docstring
Display all versions in the standard output stream

.. versionadded:: 3.22
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgscommandlineutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -20,6 +20,7 @@
%Include auto_generated/qgscacheindexfeatureid.sip
%Include auto_generated/qgscadutils.sip
%Include auto_generated/qgsclipper.sip
%Include auto_generated/qgscommandlineutils.sip
%Include auto_generated/qgscolorramp.sip
%Include auto_generated/qgscolorscheme.sip
%Include auto_generated/qgscolorschemeregistry.sip
@@ -294,6 +294,7 @@ set(QGIS_CORE_SRCS
qgscolorramp.cpp
qgscolorscheme.cpp
qgscolorschemeregistry.cpp
qgscommandlineutils.cpp
qgsconditionalstyle.cpp
qgsconnectionregistry.cpp
qgscoordinateformatter.cpp
@@ -896,6 +897,7 @@ set(QGIS_CORE_HDRS
qgscacheindexfeatureid.h
qgscadutils.h
qgsclipper.h
qgscommandlineutils.h
qgscolorramp.h
qgscolorscheme.h
qgscolorschemeregistry.h
@@ -0,0 +1,134 @@
/***************************************************************************
qgscommandlineutils.cpp
---------------------------
begin : June 2021
copyright : (C) 2021 by Etienne Trimaille
email : etienne dot trimaille at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsapplication.h"
#include "qgscommandlineutils.h"
#include "qgsgeos.h"
#include "qgsprojutils.h"
#include "qgsversion.h"

#include <sqlite3.h>
#include <ogr_api.h>
#include <gdal_version.h>
#include <proj.h>
#include <QSysInfo>

QString QgsCommandLineUtils::allVersions( )
{

// QGIS main version
QString versionString = QStringLiteral( "QGIS %1 '%2' (%3)\n" ).arg( VERSION, RELEASE_NAME, QGSVERSION );

// QGIS code revision
if ( QString( Qgis::devVersion() ) == QLatin1String( "exported" ) )
{
versionString += QLatin1String( "QGIS code branch" );
if ( Qgis::version().endsWith( QLatin1String( "Master" ) ) )
{
versionString += QLatin1String( "master\n" );
}
else
{
versionString += QStringLiteral( "Release %1.%2\n" ).arg( Qgis::versionInt() / 10000 ).arg( Qgis::versionInt() / 100 % 100 );
}
}
else
{
versionString += QStringLiteral( "QGIS code revision %1\n" ).arg( Qgis::devVersion() );
}

// Qt version
const QString qtVersionCompiled{ QT_VERSION_STR };
const QString qtVersionRunning{ qVersion() };
if ( qtVersionCompiled != qtVersionRunning )
{
versionString += QStringLiteral( "Compiled against Qt %1\n" ).arg( qtVersionCompiled );
versionString += QStringLiteral( "Running against Qt %1\n" ).arg( qtVersionRunning );
}
else
{
versionString += QStringLiteral( "Qt version %1\n" ).arg( qtVersionCompiled );
}

// Python version
versionString += QStringLiteral( "Python version %1\n" ).arg( PYTHON_VERSION );

// GDAL version
const QString gdalVersionCompiled { GDAL_RELEASE_NAME };
const QString gdalVersionRunning { GDALVersionInfo( "RELEASE_NAME" ) };
if ( gdalVersionCompiled != gdalVersionRunning )
{
versionString += QStringLiteral( "Compiled against GDAL/OGR %1\n" ).arg( gdalVersionCompiled );
versionString += QStringLiteral( "Running against GDAL/OGR %1\n" ).arg( gdalVersionRunning );
}
else
{
versionString += QStringLiteral( "GDAL/OGR version %1\n" ).arg( gdalVersionCompiled );
}

// proj
PJ_INFO info = proj_info();
const QString projVersionCompiled { QStringLiteral( "%1.%2.%3" ).arg( PROJ_VERSION_MAJOR ).arg( PROJ_VERSION_MINOR ).arg( PROJ_VERSION_PATCH ) };
const QString projVersionRunning { info.version };
if ( projVersionCompiled != projVersionRunning )
{
versionString += QStringLiteral( "Compiled against PROJ %1\n" ).arg( projVersionCompiled );
versionString += QStringLiteral( "Running against PROJ %2\n" ).arg( projVersionRunning );
}
else
{
versionString += QStringLiteral( "PROJ version %1\n" ).arg( projVersionCompiled );
}

// CRS database versions
versionString += QStringLiteral( "EPSG Registry database version %1 (%2)\n" ).arg( QgsProjUtils::epsgRegistryVersion(), QgsProjUtils::epsgRegistryDate().toString( Qt::ISODate ) );

// GEOS version
const QString geosVersionCompiled { GEOS_CAPI_VERSION };
const QString geosVersionRunning { GEOSversion() };
if ( geosVersionCompiled != geosVersionRunning )
{
versionString += QStringLiteral( "Compiled against GEOS %1\n" ).arg( geosVersionCompiled );
versionString += QStringLiteral( "Running against GEOS %1\n" ).arg( geosVersionRunning );
}
else
{
versionString += QStringLiteral( "GEOS version %1\n" ).arg( geosVersionCompiled );
}

// SQLite version
const QString sqliteVersionCompiled { SQLITE_VERSION };
const QString sqliteVersionRunning { sqlite3_libversion() };
if ( sqliteVersionCompiled != sqliteVersionRunning )
{
versionString += QStringLiteral( "Compiled against SQLite %1\n" ).arg( sqliteVersionCompiled );
versionString += QStringLiteral( "Running against SQLite %1\n" ).arg( sqliteVersionRunning );
}
else
{
versionString += QStringLiteral( "SQLite version %1\n" ).arg( sqliteVersionCompiled );
}

// Operating system
versionString += QStringLiteral( "OS %1\n" ).arg( QSysInfo::prettyProductName() );

#ifdef QGISDEBUG
versionString += QStringLiteral( "This copy of QGIS writes debugging output.\n" );
#endif

return versionString;
}
@@ -0,0 +1,41 @@
/***************************************************************************
qgscommandlineutils.h
---------------------------
begin : June 2021
copyright : (C) 2021 by Etienne Trimaille
email : etienne dot trimaille at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSCOMMANDLINEUTILS_H
#define QGSCOMMANDLINEUTILS_H

#include "qgis.h"

/**
* \ingroup core
* \class QgsCommandLineUtils
* \brief Utils class for QGIS command line tools.
* \since QGIS 3.22
*/
class CORE_EXPORT QgsCommandLineUtils
{
public:

/**
* Display all versions in the standard output stream
* \since QGIS 3.22
*/
static QString allVersions( );

};

#endif // QGSCOMMANDLINEUTILS_H
@@ -15,6 +15,7 @@
* *
***************************************************************************/

#include "qgscommandlineutils.h"
#include "qgsprocess.h"
#include "qgsprocessingregistry.h"
#include "qgsprocessingalgorithm.h"
@@ -298,6 +299,16 @@ int QgsProcessingExec::run( const QStringList &constArgs )
listAlgorithms( useJson );
return 0;
}
else if ( command == QLatin1String( "--version" ) || command == QLatin1String( "-v" ) )
{
std::cout << QgsCommandLineUtils::allVersions().toStdString();
return 0;
}
else if ( command == QLatin1String( "--help" ) || command == QLatin1String( "-h" ) )
{
showUsage( args.at( 0 ) );
return 0;
}
else if ( command == QLatin1String( "help" ) )
{
if ( args.size() < 3 )
@@ -445,8 +456,10 @@ void QgsProcessingExec::showUsage( const QString &appName )

msg << "QGIS Processing Executor - " << VERSION << " '" << RELEASE_NAME << "' ("
<< Qgis::version() << ")\n"
<< "Usage: " << appName << " [--json] [--verbose] [command] [algorithm id or path to model file] [parameters]\n"
<< "Usage: " << appName << " [--help] [--version] [--json] [--verbose] [command] [algorithm id or path to model file] [parameters]\n"
<< "\nOptions:\n"
<< "\t--help or -h\t\tOutput the help\n"
<< "\t--version or -v\t\tOutput all versions related to QGIS Process\n"
<< "\t--json\t\tOutput results as JSON objects\n"
<< "\t--verbose\tOutput verbose logs\n"
<< "\nAvailable commands:\n"
@@ -22,6 +22,7 @@
#include "qgsfcgiserverresponse.h"
#include "qgsfcgiserverrequest.h"
#include "qgsapplication.h"
#include "qgscommandlineutils.h"

#include <fcgi_stdio.h>
#include <cstdlib>
@@ -43,6 +44,15 @@ int fcgi_accept()

int main( int argc, char *argv[] )
{
if ( argc >= 2 )
{
if ( argv[1] == QLatin1String( "--version" ) || argv[1] == QLatin1String( "-v" ) )
{
std::cout << QgsCommandLineUtils::allVersions().toStdString();
return 0;
}
}

// Test if the environ variable DISPLAY is defined
// if it's not, the server is running in offscreen mode
// Qt supports using various QPA (Qt Platform Abstraction) back ends
@@ -30,6 +30,7 @@ while QGIS server internal logging is printed to stderr.
#include <condition_variable>

//for CMAKE_INSTALL_PREFIX
#include "qgscommandlineutils.h"
#include "qgsconfig.h"
#include "qgsserver.h"
#include "qgsbufferserverrequest.h"
@@ -554,7 +555,10 @@ int main( int argc, char *argv[] )
QCommandLineParser parser;
parser.setApplicationDescription( QObject::tr( "QGIS Development Server %1" ).arg( VERSION ) );
parser.addHelpOption();
parser.addVersionOption();

QCommandLineOption versionOption( QStringList() << "v" << "version", QObject::tr( "Version of QGIS and libraries" ) );
parser.addOption( versionOption );

parser.addPositionalArgument( QStringLiteral( "addressAndPort" ),
QObject::tr( "Address and port (default: \"localhost:8000\")\n"
"address and port can also be specified with the environment\n"
@@ -571,6 +575,13 @@ int main( int argc, char *argv[] )
parser.addOption( projectOption );

parser.process( app );

if ( parser.isSet( versionOption ) )
{
std::cout << QgsCommandLineUtils::allVersions().toStdString();
return 0;
}

const QStringList args = parser.positionalArguments();

if ( args.size() == 1 )

0 comments on commit bb9c45d

Please sign in to comment.