Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #749 from Oslandia/issue8045b
Unit tests for command line arguments
  • Loading branch information
timlinux committed Sep 15, 2013
2 parents 503d32b + a8b9d43 commit 64c947b
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 211 deletions.
291 changes: 84 additions & 207 deletions src/app/main.cpp
Expand Up @@ -443,208 +443,105 @@ int main( int argc, char *argv[] )
//put all QGIS settings in the same place
configpath = QgsApplication::qgisSettingsDirPath();
QgsDebugMsg( QString( "Android: configpath set to %1" ).arg( configpath ) );
#elif defined(Q_WS_WIN)
for ( int i = 1; i < argc; i++ )
{
QString arg = argv[i];
#endif

if ( arg == "--help" || arg == "-?" )
{
usage( argv[0] );
return 2;
}
else if ( arg == "-nologo" || arg == "-n" )
{
myHideSplash = true;
}
else if ( arg == "--noplugins" || arg == "-P" )
{
myRestorePlugins = false;
}
else if ( arg == "--nocustomization" || arg == "-C" )
{
myCustomization = false;
}
else if ( i + 1 < argc && ( arg == "--snapshot" || arg == "-s" ) )
{
mySnapshotFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[++i] ) ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == "--width" || arg == "-w" ) )
{
mySnapshotWidth = QString( argv[++i] ).toInt();
}
else if ( i + 1 < argc && ( arg == "--height" || arg == "-h" ) )
{
mySnapshotHeight = QString( argv[++i] ).toInt();
}
else if ( i + 1 < argc && ( arg == "--lang" || arg == "-l" ) )
{
myTranslationCode = argv[++i];
}
else if ( i + 1 < argc && ( arg == "--project" || arg == "-p" ) )
{
myProjectFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[++i] ) ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == "--extent" || arg == "-e" ) )
{
myInitialExtent = argv[++i];
}
else if ( i + 1 < argc && ( arg == "--optionspath" || arg == "-o" ) )
{
optionpath = argv[++i];
}
else if ( i + 1 < argc && ( arg == "--configpath" || arg == "-c" ) )
{
configpath = argv[++i];
}
else if ( i + 1 < argc && ( arg == "--code" || arg == "-f" ) )
{
pythonfile = argv[++i];
}
else if ( i + 1 < argc && ( arg == "--customizationfile" || arg == "-z" ) )
{
customizationfile = argv[++i];
}
else
{
myFileList.append( QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[i] ) ).absoluteFilePath() ) );
}
QStringList args;
{
// Build a local QCoreApplication from arguments. This way, arguments are correctly parsed from their native locale
// It will use QString::fromLocal8Bit( argv ) under Unix and GetCommandLine() under Windows.
QCoreApplication coreApp( argc, argv );
args = QCoreApplication::arguments();
}
#else
if ( !bundleclicked( argc, argv ) )
{

////////////////////////////////////////////////////////////////
// Use the GNU Getopts utility to parse cli arguments
// Invokes ctor `GetOpt (int argc, char **argv, char *optstring);'
///////////////////////////////////////////////////////////////
int optionChar;
while ( 1 )
for ( int i = 1; i < args.size(); ++i )
{
static struct option long_options[] =
QString arg = args[i];

if ( arg == "--help" || arg == "-?" )
{
/* These options set a flag. */
{"help", no_argument, 0, '?'},
{"nologo", no_argument, 0, 'n'},
{"noplugins", no_argument, 0, 'P'},
{"nocustomization", no_argument, 0, 'C'},
/* These options don't set a flag.
* We distinguish them by their indices. */
{"snapshot", required_argument, 0, 's'},
{"width", required_argument, 0, 'w'},
{"height", required_argument, 0, 'h'},
{"lang", required_argument, 0, 'l'},
{"project", required_argument, 0, 'p'},
{"extent", required_argument, 0, 'e'},
{"optionspath", required_argument, 0, 'o'},
{"configpath", required_argument, 0, 'c'},
{"customizationfile", required_argument, 0, 'z'},
{"code", required_argument, 0, 'f'},
{"android", required_argument, 0, 'a'},
{0, 0, 0, 0}
};

/* getopt_long stores the option index here. */
int option_index = 0;

optionChar = getopt_long( argc, argv, "swhlpeoc",
long_options, &option_index );
QgsDebugMsg( QString( "Qgis main Debug" ) + optionChar );
/* Detect the end of the options. */
if ( optionChar == -1 )
break;

switch ( optionChar )
usage( args[0].toStdString() );
return 2;
}
else if ( arg == "--nologo" || arg == "-n" )
{
myHideSplash = true;
}
else if ( arg == "--noplugins" || arg == "-P" )
{
myRestorePlugins = false;
}
else if ( arg == "--nocustomization" || arg == "-C" )
{
myCustomization = false;
}
else if ( i + 1 < argc && ( arg == "--snapshot" || arg == "-s" ) )
{
mySnapshotFileName = QDir::convertSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == "--width" || arg == "-w" ) )
{
mySnapshotWidth = QString( args[++i] ).toInt();
}
else if ( i + 1 < argc && ( arg == "--height" || arg == "-h" ) )
{
mySnapshotHeight = QString( args[++i] ).toInt();
}
else if ( i + 1 < argc && ( arg == "--lang" || arg == "-l" ) )
{
myTranslationCode = args[++i];
}
else if ( i + 1 < argc && ( arg == "--project" || arg == "-p" ) )
{
myProjectFileName = QDir::convertSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == "--extent" || arg == "-e" ) )
{
myInitialExtent = args[++i];
}
else if ( i + 1 < argc && ( arg == "--optionspath" || arg == "-o" ) )
{
optionpath = QDir::convertSeparators( QDir( args[++i] ).absolutePath() );
}
else if ( i + 1 < argc && ( arg == "--configpath" || arg == "-c" ) )
{
configpath = QDir::convertSeparators( QDir( args[++i] ).absolutePath() );
}
else if ( i + 1 < argc && ( arg == "--code" || arg == "-f" ) )
{
pythonfile = QDir::convertSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == "--customizationfile" || arg == "-z" ) )
{
case 0:
/* If this option set a flag, do nothing else now. */
if ( long_options[option_index].flag != 0 )
break;
printf( "option %s", long_options[option_index].name );
if ( optarg )
printf( " with arg %s", optarg );
printf( "\n" );
break;

case 's':
mySnapshotFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( optarg ) ).absoluteFilePath() );
break;

case 'w':
mySnapshotWidth = QString( optarg ).toInt();
break;

case 'h':
mySnapshotHeight = QString( optarg ).toInt();
break;

case 'n':
myHideSplash = true;
break;

case 'l':
myTranslationCode = optarg;
break;

case 'p':
myProjectFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( optarg ) ).absoluteFilePath() );
break;

case 'P':
myRestorePlugins = false;
break;

case 'C':
myCustomization = false;
break;

case 'e':
myInitialExtent = optarg;
break;

case 'o':
optionpath = optarg;
break;

case 'c':
configpath = optarg;
break;

case 'f':
pythonfile = optarg;
break;

case 'z':
customizationfile = optarg;
break;

case '?':
usage( argv[0] );
return 2; // XXX need standard exit codes
break;

default:
QgsDebugMsg( QString( "%1: getopt returned character code %2" ).arg( argv[0] ).arg( optionChar ) );
return 1; // XXX need standard exit codes
customizationfile = QDir::convertSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
}
else
{
myFileList.append( QDir::convertSeparators( QFileInfo( args[i] ).absoluteFilePath() ) );
}
}
}

// Add any remaining args to the file list - we will attempt to load them
// as layers in the map view further down....
QgsDebugMsg( QString( "Files specified on command line: %1" ).arg( optind ) );
if ( optind < argc )
/////////////////////////////////////////////////////////////////////
// If no --project was specified, parse the args to look for a //
// .qgs file and set myProjectFileName to it. This allows loading //
// of a project file by clicking on it in various desktop managers //
// where an appropriate mime-type has been set up. //
/////////////////////////////////////////////////////////////////////
if ( myProjectFileName.isEmpty() )
{
// check for a .qgs
for ( int i = 0; i < args.size(); i++ )
{
while ( optind < argc )
QString arg = QDir::convertSeparators( QFileInfo( args[i] ).absoluteFilePath() );
if ( arg.contains( ".qgs" ) )
{
#ifdef QGISDEBUG
int idx = optind;
QgsDebugMsg( QString( "%1: %2" ).arg( idx ).arg( argv[idx] ) );
#endif
myFileList.append( QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[optind++] ) ).absoluteFilePath() ) );
myProjectFileName = arg;
break;
}
}
}
#endif


/////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -956,26 +853,6 @@ int main( int argc, char *argv[] )
QgsCustomization::instance(), SLOT( preNotify( QObject *, QEvent *, bool * ) )
);

/////////////////////////////////////////////////////////////////////
// If no --project was specified, parse the args to look for a //
// .qgs file and set myProjectFileName to it. This allows loading //
// of a project file by clicking on it in various desktop managers //
// where an appropriate mime-type has been set up. //
/////////////////////////////////////////////////////////////////////
if ( myProjectFileName.isEmpty() )
{
// check for a .qgs
for ( int i = 0; i < argc; i++ )
{
QString arg = QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[i] ) ).absoluteFilePath() );
if ( arg.contains( ".qgs" ) )
{
myProjectFileName = arg;
break;
}
}
}

/////////////////////////////////////////////////////////////////////
// Load a project file if one was specified
/////////////////////////////////////////////////////////////////////
Expand Down
16 changes: 13 additions & 3 deletions src/python/qgspythonutilsimpl.cpp
Expand Up @@ -77,7 +77,17 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
#ifdef Q_OS_WIN
p = p.replace( '\\', "\\\\" );
#endif
pluginpaths << '"' + p + '"';
if ( !QDir( p ).exists() )
{
QgsMessageOutput* msg = QgsMessageOutput::createMessageOutput();
msg->setTitle( QObject::tr( "Python error" ) );
msg->setMessage( QString( QObject::tr("The extra plugin path '%1' does not exist !") ).arg(p), QgsMessageOutput::MessageText );
msg->showMessage();
}
// we store here paths in unicode strings
// the str constant will contain utf8 code (through runString)
// so we call '...'.decode('utf-8') to make a unicode string
pluginpaths << '"' + p + "\".decode('utf-8')";
}
pluginpaths << homePluginsPath();
pluginpaths << '"' + pluginsPath() + '"';
Expand Down Expand Up @@ -469,11 +479,11 @@ QString QgsPythonUtilsImpl::homePythonPath()
QString settingsDir = QgsApplication::qgisSettingsDirPath();
if ( QDir::cleanPath( settingsDir ) == QDir::homePath() + QString( "/.qgis%1" ).arg( 2 /* FIXME QGis::QGIS_VERSION_INT / 10000 */ ) )
{
return QString( "os.path.expanduser(\"~/.qgis%1/python\")" ).arg( 2 /* FIXME: QGis::QGIS_VERSION_INT / 10000 */ );
return QString( "os.path.expanduser(\"~/.qgis%1/python\").decode('utf-8')" ).arg( 2 /* FIXME: QGis::QGIS_VERSION_INT / 10000 */ );
}
else
{
return '"' + settingsDir.replace( '\\', "\\\\" ) + "python\"";
return '"' + settingsDir.replace( '\\', "\\\\" ) + "python\".decode('utf-8')";
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/CMakeLists.txt
Expand Up @@ -29,4 +29,4 @@ ADD_PYTHON_TEST(PyQgsPalLabelingServer test_qgspallabeling_server.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_qgsspatialiteprovider.py)
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)

ADD_PYTHON_TEST(PyQgsAppStartup test_qgsappstartup.py)

0 comments on commit 64c947b

Please sign in to comment.