Commit f129dd6e authored by Michael Reeves's avatar Michael Reeves

*complete port from KCmdLinesArgs.

*refactor argument handling while where at it.
parent d0408b6f
......@@ -40,14 +40,13 @@
#include <QFileDialog>
#include <QMenuBar>
#include <QStatusBar>
#include <QCommandLineParser>
// include files for KDE
#include <kiconloader.h>
#include <kmessagebox.h>
#include <klocalizedstring.h>
#include <kconfig.h>
#include <kstandardaction.h>
#include <kcmdlineargs.h>
//#include <kkeydialog.h>
#include <ktoggleaction.h>
#include <ktoolbar.h>
......@@ -55,6 +54,7 @@
#include <kactioncollection.h>
// application specific includes
#include "kdiff3_shell.h"
#include "kdiff3.h"
#include "optiondialog.h"
#include "fileaccess.h"
......@@ -150,20 +150,19 @@ KDiff3App::KDiff3App( QWidget* pParent, const char* /*name*/, KDiff3Part* pKDiff
m_pOptions = &m_pOptionDialog->m_options;
m_pOptionDialog->readOptions( KSharedConfig::openConfig() );
//TODO: Port to KF5/qt5
// Option handling: Only when pParent==0 (no parent)
KCmdLineArgs *args = isPart() ? 0 : KCmdLineArgs::parsedArgs();
if( args ) {
bool hasArgs = !isPart() && KDiff3Shell::getParser()->optionNames().count() > 0;
if( hasArgs ) {
QString s;
QString title;
if( args->isSet( "confighelp" ) ) {
if( KDiff3Shell::getParser()->isSet( "confighelp" ) ) {
s = m_pOptionDialog->calcOptionHelp();
title = i18n( "Current Configuration:" );
}
else {
s = m_pOptionDialog->parseOptions( args->getOptionList( "cs" ) );
s = m_pOptionDialog->parseOptions( KDiff3Shell::getParser()->values( "cs" ) );
title = i18n( "Config Option Error:" );
}
if( !s.isEmpty() ) {
......@@ -194,51 +193,50 @@ KDiff3App::KDiff3App( QWidget* pParent, const char* /*name*/, KDiff3Part* pKDiff
m_sd2.setOptions( m_pOptions );
m_sd3.setOptions( m_pOptions );
if( args != 0 ) {
m_outputFilename = args->getOption( "output" );
if( m_outputFilename.isEmpty() )
m_outputFilename = args->getOption( "out" );
m_bAutoFlag = false;//disable --auto option git hard codes this unwanted flag.
m_bAutoMode = m_bAutoFlag || m_pOptions->m_bAutoSaveAndQuitOnMergeWithoutConflicts;
if( hasArgs ) {
m_outputFilename = KDiff3Shell::getParser()->value( "output" );
if( m_outputFilename.isEmpty() )
m_outputFilename = KDiff3Shell::getParser()->value( "out" );
if( ! m_outputFilename.isEmpty() )
m_outputFilename = FileAccess( m_outputFilename, true ).absoluteFilePath();
}
m_bAutoFlag = false;//disable --auto option git hard codes this unwanted flag.
m_bAutoMode = m_bAutoFlag || m_pOptions->m_bAutoSaveAndQuitOnMergeWithoutConflicts;
if( m_bAutoMode && m_outputFilename.isEmpty() ) {
if( m_bAutoFlag ) {
//KMessageBox::information(this, i18n("Option --auto used, but no output file specified."));
fprintf( stderr, "%s\n", ( const char* )i18n( "Option --auto used, but no output file specified." ).toLatin1() );
if( m_bAutoMode && m_outputFilename.isEmpty() ) {
if( m_bAutoFlag ) {
//KMessageBox::information(this, i18n("Option --auto used, but no output file specified."));
fprintf( stderr, "%s\n", ( const char* )i18n( "Option --auto used, but no output file specified." ).toLatin1() );
}
m_bAutoMode = false;
}
m_bAutoMode = false;
}
g_pProgressDialog->setStayHidden( m_bAutoMode );
if( m_outputFilename.isEmpty() && args != 0 && args->isSet( "merge" ) ) {
m_outputFilename = "unnamed.txt";
m_bDefaultFilename = true;
}
else
m_bDefaultFilename = false;
g_bAutoSolve = args != 0 && !args->isSet( "qall" ); // Note that this is effective only once.
if( args != 0 ) {
m_sd1.setFilename( args->getOption( "base" ) );
if( m_outputFilename.isEmpty() && KDiff3Shell::getParser()->isSet( "merge" ) ) {
m_outputFilename = "unnamed.txt";
m_bDefaultFilename = true;
}
else {
m_bDefaultFilename = false;
}
g_bAutoSolve = !KDiff3Shell::getParser()->isSet( "qall" ); // Note that this is effective only once.
//TODO validate these options.
QStringList args = KDiff3Shell::getParser()->positionalArguments();
m_sd1.setFilename( KDiff3Shell::getParser()->value( "base" ) );
if( m_sd1.isEmpty() ) {
if( args->count() > 0 ) m_sd1.setFilename( args->url( 0 ).url() ); // args->arg(0)
if( args->count() > 1 ) m_sd2.setFilename( args->url( 1 ).url() );
if( args->count() > 2 ) m_sd3.setFilename( args->url( 2 ).url() );
if( args.count() > 0 ) m_sd1.setFilename( args[0] ); // args->arg(0)
if( args.count() > 1 ) m_sd2.setFilename( args[1] );
if( args.count() > 2 ) m_sd3.setFilename( args[2] );
}
else {
if( args->count() > 0 ) m_sd2.setFilename( args->url( 0 ).url() );
if( args->count() > 1 ) m_sd3.setFilename( args->url( 1 ).url() );
if( args.count() > 0 ) m_sd2.setFilename( args[0] );
if( args.count() > 1 ) m_sd3.setFilename( args[1] );
}
QStringList aliasList = args->getOptionList( "fname" );
QStringList aliasList = KDiff3Shell::getParser()->values( "fname" );
QStringList::Iterator ali = aliasList.begin();
QString an1 = args->getOption( "L1" );
QString an1 = KDiff3Shell::getParser()->value( "L1" );
if( !an1.isEmpty() ) {
m_sd1.setAliasName( an1 );
}
......@@ -247,7 +245,7 @@ KDiff3App::KDiff3App( QWidget* pParent, const char* /*name*/, KDiff3Part* pKDiff
++ali;
}
QString an2 = args->getOption( "L2" );
QString an2 = KDiff3Shell::getParser()->value( "L2" );
if( !an2.isEmpty() ) {
m_sd2.setAliasName( an2 );
}
......@@ -256,7 +254,7 @@ KDiff3App::KDiff3App( QWidget* pParent, const char* /*name*/, KDiff3Part* pKDiff
++ali;
}
QString an3 = args->getOption( "L3" );
QString an3 = KDiff3Shell::getParser()->value( "L3" );
if( !an3.isEmpty() ) {
m_sd3.setAliasName( an3 );
}
......@@ -265,6 +263,11 @@ KDiff3App::KDiff3App( QWidget* pParent, const char* /*name*/, KDiff3Part* pKDiff
++ali;
}
}
else {
m_bDefaultFilename = false;
g_bAutoSolve = false;
}
g_pProgressDialog->setStayHidden( m_bAutoMode );
///////////////////////////////////////////////////////////////////
// call inits to invoke all other construction parts
......@@ -326,8 +329,9 @@ KDiff3App::KDiff3App( QWidget* pParent, const char* /*name*/, KDiff3Part* pKDiff
connect( m_pDirectoryMergeWindow, &DirectoryMergeWindow::statusBarMessage, this, &KDiff3App::slotStatusMsg );
m_pDirectoryMergeWindow->initDirectoryMergeActions( this, actionCollection() );
if( args != 0 ) args->clear(); // Free up some memory.
delete KDiff3Shell::getParser();
//if( args != 0 ) args->clear(); // Free up some memory.
if( m_pKDiff3Shell == 0 ) {
completeInit();
......@@ -696,7 +700,7 @@ void KDiff3App::slotFileSave() {
void KDiff3App::slotFileSaveAs() {
slotStatusMsg( i18n( "Saving file with a new filename..." ) );
QString s = QFileDialog::getSaveFileUrl(this, i18n( "Save As..." ), QDir::currentPath(), 0 ).url();
QString s = QFileDialog::getSaveFileUrl( this, i18n( "Save As..." ), QDir::currentPath(), 0 ).url();
if( !s.isEmpty() ) {
m_outputFilename = s;
m_pMergeResultWindowTitle->setFileName( m_outputFilename );
......
......@@ -43,7 +43,6 @@
#include <QCloseEvent>
#include <ktoolbar.h>
KDiff3Shell::KDiff3Shell( bool bCompleteInit )
: KParts::MainWindow( ) {
m_bUnderConstruction = true;
......
......@@ -22,6 +22,7 @@
#include <config-kdiff3.h>
#include <QCommandLineParser>
#include <QApplication>
#include <kparts/mainwindow.h>
#include <kparts/readwritepart.h>
......@@ -52,7 +53,8 @@ public:
bool queryClose();
bool queryExit();
virtual void closeEvent(QCloseEvent*e);
static inline QCommandLineParser* getParser(){ static QCommandLineParser *parser = new QCommandLineParser(); return parser;};
private Q_SLOTS:
void optionsShowToolbar();
void optionsShowStatusbar();
......
......@@ -42,7 +42,7 @@
#endif
#include "common.h"
void initialiseCmdLineArgs( QCommandLineParser& parser ) {
void initialiseCmdLineArgs( ) {
QString configFileName = QStandardPaths::locate(QStandardPaths::GenericConfigLocation , "kdiff3rc" );
QFile configFile( configFileName );
QString ignorableOptionsLine = "-u;-query;-html;-abort";
......@@ -71,10 +71,10 @@ void initialiseCmdLineArgs( QCommandLineParser& parser ) {
( *i ).remove( '-' );
if( !( *i ).isEmpty() ) {
if( i->length() == 1 ) {
parser.addOption( QCommandLineOption( QStringList() << i->toLatin1() << QLatin1String( "ignore" ), i18n( "Ignored. (User defined.)" ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << i->toLatin1() << QLatin1String( "ignore" ), i18n( "Ignored. (User defined.)" ) ) );
}
else {
parser.addOption( QCommandLineOption( QStringList() << i->toLatin1(), i18n( "Ignored. (User defined.)" ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << i->toLatin1(), i18n( "Ignored. (User defined.)" ) ) );
}
}
}
......@@ -196,36 +196,35 @@ int main( int argc, char *argv[] ) {
aboutData.addCredit( i18n( "+ Many thanks to those who reported bugs and contributed ideas!" ) );
QCommandLineParser parser;
KAboutData::setApplicationData( aboutData );
parser.setApplicationDescription(aboutData.shortDescription());
parser.addVersionOption();
parser.addHelpOption();
initialiseCmdLineArgs( parser );
KDiff3Shell::getParser()->setApplicationDescription(aboutData.shortDescription());
KDiff3Shell::getParser()->addVersionOption();
KDiff3Shell::getParser()->addHelpOption();
initialiseCmdLineArgs( );
// ignorable command options
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "m" ) << QLatin1String( "merge" ), i18n( "Merge the input." ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "b" ) << QLatin1String( "base file" ), i18n( "Explicit base file. For compatibility with certain tools." ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "o" ) << QLatin1String( "output file" ), i18n( "Output file. Implies -m. E.g.: -o newfile.txt" ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "out file" ), i18n( "Output file, again. (For compatibility with certain tools.)" ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "auto" ), i18n( "No GUI if all conflicts are auto-solvable. (Needs -o file)" ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "qall" ), i18n( "Don't solve conflicts automatically." ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "L1 alias1" ), i18n( "Visible name replacement for input file 1 (base)." ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "L2 alias2" ), i18n( "Visible name replacement for input file 2." ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "L3 alias3" ), i18n( "Visible name replacement for input file 3." ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "L" ) << QLatin1String( "fname alias" ), i18n( "Alternative visible name replacement. Supply this once for every input." ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "cs string" ), i18n( "Override a config setting. Use once for every setting. E.g.: --cs \"AutoAdvance=1\"" ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "confighelp" ), i18n( "Show list of config settings and current values." ) ) );
parser.addOption( QCommandLineOption( QStringList() << QLatin1String( "config file" ), i18n( "Use a different config file." ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "m" ) << QLatin1String( "merge" ), i18n( "Merge the input." ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "b" ) << QLatin1String( "base file" ), i18n( "Explicit base file. For compatibility with certain tools." ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "o" ) << QLatin1String( "output file" ), i18n( "Output file. Implies -m. E.g.: -o newfile.txt" ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "out file" ), i18n( "Output file, again. (For compatibility with certain tools.)" ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "auto" ), i18n( "No GUI if all conflicts are auto-solvable. (Needs -o file)" ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "qall" ), i18n( "Don't solve conflicts automatically." ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "L1 alias1" ), i18n( "Visible name replacement for input file 1 (base)." ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "L2 alias2" ), i18n( "Visible name replacement for input file 2." ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "L3 alias3" ), i18n( "Visible name replacement for input file 3." ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "L" ) << QLatin1String( "fname alias" ), i18n( "Alternative visible name replacement. Supply this once for every input." ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "cs string" ), i18n( "Override a config setting. Use once for every setting. E.g.: --cs \"AutoAdvance=1\"" ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "confighelp" ), i18n( "Show list of config settings and current values." ) ) );
KDiff3Shell::getParser()->addOption( QCommandLineOption( QStringList() << QLatin1String( "config file" ), i18n( "Use a different config file." ) ) );
// other command options
parser.addPositionalArgument( QLatin1String( "[File1]" ), i18n( "file1 to open (base, if not specified via --base)" ) );
parser.addPositionalArgument( QLatin1String( "[File2]" ), i18n( "file2 to open" ) );
parser.addPositionalArgument( QLatin1String( "[File3]" ), i18n( "file3 to open" ) );
KDiff3Shell::getParser()->addPositionalArgument( QLatin1String( "[File1]" ), i18n( "file1 to open (base, if not specified via --base)" ) );
KDiff3Shell::getParser()->addPositionalArgument( QLatin1String( "[File2]" ), i18n( "file2 to open" ) );
KDiff3Shell::getParser()->addPositionalArgument( QLatin1String( "[File3]" ), i18n( "file3 to open" ) );
parser.process( app ); // PORTING SCRIPT: move this to after any parser.addOption
KDiff3Shell::getParser()->process( app ); // PORTING SCRIPT: move this to after any parser.addOption
//must be after process or parse call
aboutData.setupCommandLine( &parser );
aboutData.setupCommandLine( KDiff3Shell::getParser() );
/**
* take component name and org. name from KAboutData
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment