[vlmc-devel] main.cpp: Use QCommandLineParser to handle commandline arguments

Yikai Lu git at videolan.org
Mon Jul 17 16:41:24 CEST 2017


vlmc | branch: master | Yikai Lu <luyikei.qmltu at gmail.com> | Mon Jul 17 16:26:11 2017 +0200| [57f716370daaf98766c869dd623d1476594ac50b] | committer: Yikai Lu

main.cpp: Use QCommandLineParser to handle commandline arguments

Be sure to move ~/.config/VideoLAN/vlmc.conf to ~/.config/VideoLAN/VLMC.conf as the application name has changed :)

> https://code.videolan.org/videolan/vlmc/commit/57f716370daaf98766c869dd623d1476594ac50b
---

 src/Main/main.cpp | 153 ++++++++++++++++++++++++++++--------------------------
 src/Main/vlmc.cpp |  63 +---------------------
 2 files changed, 81 insertions(+), 135 deletions(-)

diff --git a/src/Main/main.cpp b/src/Main/main.cpp
index 568f3909..13eb19c3 100644
--- a/src/Main/main.cpp
+++ b/src/Main/main.cpp
@@ -55,21 +55,45 @@
 #include <QSettings>
 #include <QUuid>
 #include <QTextCodec>
+#include <QCommandLineParser>
 
 #ifdef Q_WS_X11
 #include <X11/Xlib.h>
 #endif
 
 static void
-VLMCmainCommon( const QCoreApplication &app, Backend::IBackend** backend )
+setupCommandLine( QCommandLineParser& parser )
 {
-    app.setApplicationName( "vlmc" );
-    app.setOrganizationName( "VideoLAN" );
-    app.setOrganizationDomain( "videolan.org" );
-    app.setApplicationVersion( PACKAGE_VERSION );
+    parser.setSingleDashWordOptionMode( QCommandLineParser::ParseAsLongOptions );
+    parser.setApplicationDescription(
+                QString(
+                    "VideoLAN Movie Creator (VLMC) is a cross-platform, non-linear\n"
+                    "video editing software."
+                    ) );
+    parser.addHelpOption();
+    parser.addVersionOption();
+
+    parser.addPositionalArgument( "project",
+                                  QCoreApplication::translate( "main", "Project file to open." ),
+                                  "[filename|URI]" );
+    parser.addPositionalArgument( "output",
+                                  QCoreApplication::translate( "main", "Output file to write to." ),
+                                  "[filename]" );
+
+    parser.addOption( { "v",
+                        QCoreApplication::translate( "main", "Log level - Verbose" ) } );
+    parser.addOption( { "vv",
+                        QCoreApplication::translate( "main", "Log level - Debug" ) } );
+    parser.addOption( { { "b", "backendverbose" },
+                        QCoreApplication::translate( "main", "Backend Log level to set" ),
+                        "value" } );
+    parser.process( *qApp );
+}
 
-    QSettings s;
 
+static void
+VLMCmainCommon( Backend::IBackend** backend )
+{
     qRegisterMetaType<Workflow::TrackType>( "Workflow::TrackType" );
     qRegisterMetaType<Vlmc::FrameChangedReason>( "Vlmc::FrameChangedReason" );
     qRegisterMetaType<QVariant>( "QVariant" );
@@ -81,36 +105,18 @@ VLMCmainCommon( const QCoreApplication &app, Backend::IBackend** backend )
 /**
  *  VLMC Entry point
  *  \brief this is the VLMC entry point
- *  \param argc
- *  \param argv
  *  \return Return value of vlmc
  */
 #ifdef HAVE_GUI
 int
-VLMCGuimain( int argc, char **argv )
+VLMCGuimain( const QString& projectFile )
 {
 #ifdef Q_WS_X11
     XInitThreads();
 #endif
 
-    QApplication app( argc, argv );
-
     Backend::IBackend* backend;
-    VLMCmainCommon( app, &backend );
-
-    /* Load a project file */
-    bool        project = false;
-    for ( int i = 1; i < argc; i++ )
-    {
-        QString arg = argv[i];
-
-        if ( argc > ( i + 1 ) && ( arg == "--project" || arg == "-p" ) )
-        {
-            Core::instance()->loadProject( argv[i+1] );
-            project = true;
-            break;
-        }
-    }
+    VLMCmainCommon( &backend );
 
     /* Translations */
     QSettings s;
@@ -167,83 +173,82 @@ VLMCGuimain( int argc, char **argv )
     }
 
     //Don't show the wizard if a project has been passed through command line.
-    if ( project == false )
+    if ( projectFile.isEmpty() == true )
         w.showWizard();
 
     /* Main Window display */
     w.show();
-    auto res = app.exec();
+
+    if ( projectFile.isEmpty() == false )
+        Core::instance()->loadProject( projectFile );
+
+    auto res = qApp->exec();
     Core::instance()->settings()->save();
     return res;
 }
 #endif
+
 /**
  *  VLMC Entry point
  *  \brief this is the VLMC entry point
- *  \param argc
- *  \param argv
  *  \return Return value of vlmc
  */
 int
-VLMCCoremain( int argc, char **argv )
+VLMCCoremain( const QString& projectFile , const QString& outputFile)
 {
-#ifdef HAVE_GUI
-    QApplication app( argc, argv );
-#else
-    QCoreApplication app( argc, argv );
-#endif
-
     Backend::IBackend* backend;
-    VLMCmainCommon( app, &backend );
+    VLMCmainCommon( &backend );
 
-    const QString* projectFile = nullptr;
-    const QString* outputFile = nullptr;
-    QStringList&& args = app.arguments();
-
-    // We don't check the first argument, possibly "./vlmc"
-    args.pop_front();
-
-    for ( const auto& arg : args )
-    {
-        // Make sure it's not an option
-        if ( projectFile == nullptr && arg.at( 0 ) != '-' )
-            projectFile = &arg;
-        else if ( outputFile == nullptr && arg.at( 0 ) != '-' )
-            outputFile = &arg;
-    }
-
-    /* Load a project file */
-    if ( projectFile == nullptr || outputFile == nullptr )
-    {
-        vlmcCritical() << "Usage:" << argv[0]
-#ifdef HAVE_GUI
-                << "--no-gui"
-#endif
-                << "project.vlmc output_file";
-        return 1;
-    }
-
-
-    ConsoleRenderer renderer( *outputFile );
+    ConsoleRenderer renderer( outputFile );
     Project  *p = Core::instance()->project();
 
     QCoreApplication::connect( p, &Project::projectLoaded, &renderer, &ConsoleRenderer::startRender );
-    QCoreApplication::connect( &renderer, &ConsoleRenderer::finished, &app, &QCoreApplication::quit, Qt::QueuedConnection );
+    QCoreApplication::connect( &renderer, &ConsoleRenderer::finished, qApp, &QCoreApplication::quit, Qt::QueuedConnection );
     Core::instance()->settings()->load();
-    p->load( *projectFile );
+    p->load( projectFile );
 
-    auto res = app.exec();
+    auto res = qApp->exec();
     Core::instance()->settings()->save();
     return res;
 }
 
 int
-VLMCmain( int argc, char **argv, bool gui )
+VLMCmain( int argc, char **argv )
 {
 #ifdef HAVE_GUI
-    if ( gui == true )
-        return VLMCGuimain( argc, argv );
+    QApplication app( argc, argv );
+#else
+    QCoreApplication app( argc, argv );
+#endif
+
+    app.setApplicationName( "VLMC" );
+    app.setOrganizationName( "VideoLAN" );
+    app.setOrganizationDomain( "videolan.org" );
+    app.setApplicationVersion(
+                QString(
+                    "%1 '%2'\n"
+                    "VideoLAN Movie Creator (VLMC) is a cross-platform, non-linear\n"
+                    "video editing software.\n"
+                    "Copyright (C) 2008-10 VideoLAN\n"
+                    "This is free software; see the source for copying conditions. There is NO\n"
+                    "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
+                    ).arg( PACKAGE_VERSION ).arg( CODENAME ) );
+
+    QCommandLineParser parser;
+    setupCommandLine( parser );
+
+    const auto& args = parser.positionalArguments();
+
+    if ( args.size() >= 2  )
+        return VLMCCoremain( args.at( 0 ), args.at( 1 ) );
+#ifdef HAVE_GUI
+    else if ( args.size() == 1 )
+        return VLMCGuimain( args.at( 0 ) );
+    else
+        return VLMCGuimain( "" );
+#else
+    else
+        parser.showHelp( 1 ); // This function exits the application. No need to return any value.
 #endif
-    return VLMCCoremain( argc, argv );
 }
 
diff --git a/src/Main/vlmc.cpp b/src/Main/vlmc.cpp
index 4fc54779..f6e24332 100644
--- a/src/Main/vlmc.cpp
+++ b/src/Main/vlmc.cpp
@@ -37,7 +37,7 @@
 #include <unistd.h>
 
 
-int VLMCmain( int , char**, bool );
+int VLMCmain( int , char** );
 
 #if defined(WITH_CRASHHANDLER) && defined(Q_OS_UNIX)
 
@@ -65,68 +65,9 @@ signalHandler( int sig )
 }
 #endif
 
-/**
- * Print version text
- */
-void
-version( void )
-{
-    QTextStream out( stdout );
-    out << "VLMC-" << PACKAGE_VERSION << " '" << CODENAME << "'\n"
-        << "VideoLAN Movie Creator (VLMC) is a cross-platform, non-linear\n"
-        << "video editing software.\n"
-        << "Copyright (C) 2008-10 VideoLAN\n"
-        << "This is free software; see the source for copying conditions. There is NO\n"
-        << "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
-}
-
-/**
- * Print usage text
- */
-void
-usage( QString const& appName )
-{
-    QTextStream out( stdout );
-    out << "Usage: " << appName << " [options] [filename|URI]...\n"
-        << "Options:\n"
-        << "\t[--project|-p projectfile]\tload the given VLMC project\n"
-        << "\t[--version]\tversion information\n"
-        << "\t[--help|-?]\tthis text\n\n"
-        << "\tFILES:\n"
-        << "\t\tFiles specified on the command line should include \n"
-        << "\t\tVLMC project files (.vlmc)\n";
-}
-
 int
 main( int argc, char **argv )
 {
-#ifdef HAVE_GUI
-    bool gui = true;
-#else
-    bool gui = false;
-#endif
-
-    /* Check for command arguments */
-    for ( int i = 1; i < argc; i++ )
-    {
-        QString arg = argv[i];
-
-        if ( arg == "--help" || arg == "-?" )
-        {
-            ::usage( QString( argv[0] ) );
-            return 2;
-        }
-        else if ( arg == "--version" )
-        {
-            ::version();
-            return 2;
-        }
-#ifdef HAVE_GUI
-        else if ( arg == "--no-gui" )
-            gui = false;
-#endif
-    }
-
     #ifdef WITH_CRASHHANDLER
         while( true )
         {
@@ -165,5 +106,5 @@ main( int argc, char **argv )
         }
     #endif
 
-    return VLMCmain( argc, argv, gui );
+    return VLMCmain( argc, argv );
 }



More information about the Vlmc-devel mailing list