convert-kcmdlineargs.pl 10.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
#!/usr/bin/perl -w

# David Faure <faure@kde.org>
# KCommandLineArgs -> QCommandLineParser

use strict;
use File::Basename;
use lib dirname($0);
use functionUtilkde;
David Faure's avatar
David Faure committed
10
my $use_tr;
11
my $use_aboutdata;
David Faure's avatar
David Faure committed
12
13
14
15

# Set $use_tr to generate code that uses QCoreApplication::translate
# If it's not set, i18n will be used.
#$use_tr = 1;
16

17
18
19
20
21
22
23
# Set $use_aboutdata_tr to generate code that uses kaboutdata
$use_aboutdata = 1;

my $port_kapplicationAndK4AboutData;
# Set $port_kapplicationAndK4AboutData if you want to port k4aboutdata + kapplication
$port_kapplicationAndK4AboutData = 1;

24
25
26
foreach my $file (@ARGV) {
    my $context = "\"main\"";
    my $opt;
27
28
29
    my $short = "";
    my $args;
    my %negatedOptions = ();
30
31
    my $needRemoveKApplication;
    my %varname = ();
32
    my $QCommandLineParserAdded;
33
    my $needQCommandLineOption;
34
    functionUtilkde::substInFile {
35
        if (defined $port_kapplicationAndK4AboutData) {
36
           if (/KApplication a/) {
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
              $_ ="";
              $needRemoveKApplication = 1;
           }
           my $regexpK4AboutDataLocal = qr/
              ^(\s*)           # (1) Indentation
              K4AboutData\s+
              (\w+)            # (2) variable name
              /x; # /x Enables extended whitespace mode
           if (my ($left, $var) = $_ =~ $regexpK4AboutDataLocal) {
               $varname{$var} = 1;
               s/ki18n/i18n/g;
           }
           if (/(\w+)\.addAuthor\s*\(/) {
              my $var = $1;
              if ( defined $varname{$var} ) {
                  s/ki18n/i18n/g;
                  s/KLocalizedString\b/QString/g;
              }
           }
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
           if (/(\w+)\.addCredit\s*\(/) {
              my $var = $1;
              if ( defined $varname{$var} ) {
                  s/ki18n/i18n/g;
                  s/KLocalizedString\b/QString/g;
              }
           }
           if (/(\w+)\.setTranslator\s*\(/) {
              my $var = $1;
              if ( defined $varname{$var} ) {
                  s/ki18n/i18n/g;
                  s/KLocalizedString\b/QString/g;
              }
           }


72
           s/K4AboutData/KAboutData/g;
73
           s/KAboutData::License_/KAboutLicense::/;
74
        }
75
76
        if (/KCmdLineOptions (\w*)/) {
            $opt = $1;
77

Laurent Montel's avatar
Laurent Montel committed
78
79
            s/KCmdLineOptions /QCommandLineParser /;
            s/$opt/parser/;
80
81
82
83
            if (defined $QCommandLineParserAdded) {
              $_ = "";
            } else {
              if (defined $port_kapplicationAndK4AboutData) {
84
                 $_ .= "    QApplication app(argc, argv); // PORTING SCRIPT: move this to before the KAboutData initialization\n";
85
86
87
88
                 $_ .= "    KAboutData::setApplicationData(aboutData);\n";
              }
              if ( defined $use_aboutdata) {
                $_ .= "    //PORTING SCRIPT: adapt aboutdata variable if necessary\n";
89
                $_ .= "    aboutData.setupCommandLine(&parser);\n";
90
              }
91
              $_ .= "    parser.process(app); // PORTING SCRIPT: move this to after any parser.addOption\n";
92
93
94
              if ( defined $use_aboutdata) {
                $_ .= "    aboutData.processCommandLine(&parser);\n";
              }
95
            }
96
            $QCommandLineParserAdded = 1;
97

98
        } elsif (defined $opt && /KCmdLineArgs::addCmdLineOptions\s*\(\s*$opt\s*\)/ || /KCmdLineArgs::init/) {
Laurent Montel's avatar
Laurent Montel committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
           my $addNewAboutData;
           my $regexp = qr/
             ^(.*)                        # (1) Indentation
              KCmdLineArgs::init\s*\((.*)\)   # (2)  KCmdLineArgs::init(...,...,...,...);
              (.*)$                         # (3) afterreg
              /x; # /x Enables extended whitespace mode
           if (my ($indent, $argument, $afterreg) = $_ =~ $regexp) {
              my ($argc, $argv, $appname, $catalog, $programname, $version, $description);
              my $constructor_regexp = qr/
                                 ^([^,]*)\s*        # argc
                                 ,\s*([^,]*)\s*     # argv
			         ,\s*([^,]*)\s*     # appname
                                 ,\s*([^,]*)         # catalog
                                 ,\s*([^,]*)         # programname
                                 ,\s*([^,]*)         # version
                                 (?:,\s*([^,]*))?    # description
                                 (.*)$              # after
                                 /x;
              if ( ($argc, $argv, $appname, $catalog, $programname, $version, $description) = $argument =~ $constructor_regexp ) {
                 $appname =~ s/ki18n/i18n/;
                 $appname =~ s/ki18n/i18n/;
                 $programname =~ s/ki18n/i18n/;
                 if ( $appname =~ /^\"/ ) {
                    if ( $appname =~ /QLatin1String/ ) {
                       #nothing
                    } else {
                       $appname = "QLatin1String($appname)";
                    }
                 }
                 if ( $version =~ /QLatin1String/ ) {
                    #nothing
                 } else {
                    $version = "QLatin1String($version)";
                 }
                 $_ = $indent . "KAboutData aboutData( $appname, $programname, $version);\n";
                 if (defined $description) {
                    warn "DESCRIPION : $description\n";
                    $description =~ s/^,//;
                    $description =~ s/ki18n/i18n/;
                    $_ .= $indent . "aboutData.setShortDescription($description);\n";
                 }
                 $addNewAboutData = 1;
Laurent Montel's avatar
Laurent Montel committed
141
142
              } else {
                 $_ = "";
Laurent Montel's avatar
Laurent Montel committed
143
144
145
              }
            }

146
            if ( defined $QCommandLineParserAdded) {
Laurent Montel's avatar
Laurent Montel committed
147
148
149
150
151
               if (defined $addNewAboutData) {
                  #nothing
               } else {
                  $_ = "";
               }
152
            } else {
153
              if (defined $port_kapplicationAndK4AboutData) {
Laurent Montel's avatar
Laurent Montel committed
154
                 $_ .= "    QApplication app(argc, argv); // PORTING SCRIPT: move this to before the KAboutData initialization\n";
155
                 $_ .= "    QCommandLineParser parser;\n";
156
157
158
159
160
161
                 $_ .= "    KAboutData::setApplicationData(aboutData);\n";
              }
              if ( defined $use_aboutdata) {
                $_ .= "    //PORTING SCRIPT: adapt aboutdata variable if necessary\n";
                $_ .= "    aboutData.setupCommandLine(&parser);\n";
              }
162
              $_ .= "    parser.process(app); // PORTING SCRIPT: move this to after any parser.addOption\n";
163
164
165
166
167
              if ( defined $use_aboutdata) {
                $_ .= "    aboutData.processCommandLine(&parser);\n";
              }
            }
            $QCommandLineParserAdded = 1;
168
        } elsif (defined $opt && s/(.*)$opt\.add\s*\(\s*"([^\"]*)"\s*\)/$1$opt/) { # short option
Laurent Montel's avatar
Laurent Montel committed
169

170
            $short = "QLatin1String(\"$2\") << ";
171
            $_ = "" if (!/\.add/);
Laurent Montel's avatar
Laurent Montel committed
172
            warn "$file: Be sure that option is added \'$2\'\n";
173
174
        }
        if (defined $opt && /(.*)$opt.add\s*\(\s*"([^\"]*)"\s*,\s*k(i18nc?)\((.*)\)\s*(?:,\s*([^\)]*))?\)/) {
175
            my $prefix = $1; # e.g. indent
176
            my $name = $2;
David Faure's avatar
David Faure committed
177
178
179
            my $i18n = $3;
            my $description = $4;
            my $defaultValue = $5;
180
            my $trail = "";
David Faure's avatar
David Faure committed
181
            if ($name =~ /([\w\-_]*) <(.*)>/) { # "stylesheet <xsl>"
182
                $name = $1;
Laurent Montel's avatar
Laurent Montel committed
183
                $trail = ", QLatin1String(\"$2\")";
184
185
            }
            if (defined $defaultValue) {
Laurent Montel's avatar
Laurent Montel committed
186
187
188
189
190
                if ( $defaultValue =~ /QLatin1String/ ) {
                   $trail .= ", $defaultValue";
                } else {
                    $trail .= ", QLatin1String($defaultValue)";
                }
191
192
193
            } elsif ($name =~ /^no/) { # negative option, e.g. --nosignal
                $negatedOptions{$name} = 1;
            }
David Faure's avatar
David Faure committed
194
            my $translate = defined $use_tr ? "QCoreApplication::translate($context, $description)" : "$i18n($description)";
195
196
197
198
199
            if ($name =~ s/^\+//) {
                $_ = "${prefix}parser.addPositionalArgument(QLatin1String(\"$name\"), $translate$trail);\n";
            } else {
                $_ = "${prefix}parser.addOption(QCommandLineOption(QStringList() << ${short}QLatin1String(\"$name\"), $translate$trail));\n";
            }
200
            $needQCommandLineOption = 1;
201
            $short = "";
David Faure's avatar
David Faure committed
202
        } elsif (/KCmdLineArgs\s*\*\s*(\w*)\s*=\s*KCmdLineArgs::parsedArgs\(\s*\)/) {
203
204
205
206
207
208
            $args = $1;
            $_ = "";
        } else {
            s/KCmdLineArgs::qtArgc\(\)/argc/;
            s/KCmdLineArgs::qtArgv\(\)/argv/;
            if (defined $args) {
Laurent Montel's avatar
Laurent Montel committed
209
210
211
                s/${args}\->getOptionList/parser.values/g;
                s/${args}\->getOption/parser.value/g;
                s/${args}\->isSet/parser.isSet/g;
Laurent Montel's avatar
Laurent Montel committed
212
213
                s/${args}\->count/parser.positionalArguments().count/;
                s/${args}\->usage\s*\(\)/parser.showHelp()/;
214
                s/${args}\->clear\s*\(\);//;
Laurent Montel's avatar
Laurent Montel committed
215
                s/KCmdLineArgs::usage\s*\(\)/parser.showHelp()/;
David Faure's avatar
David Faure committed
216
                if (/arguments?\(\"([\w\-_]*)/ || /isSet\(\"([\w\-_]*)/) {
217
218
                    my $optionName = $1;
                    if (defined $negatedOptions{"no$optionName"}) {
219
                        s/$/\/\/ TODO: negate check (and ensure nobody passes the no-op --$optionName argument)/;
220
221
222
                        s/$optionName/no$optionName/g;
                    }
                }
223
224
225
226
            }
        }
        $_;
    } $file;
227
228
229
230
231
232
233
234

    if (defined $needRemoveKApplication) {
      functionUtilkde::removeIncludeInFile($file, "KApplication");
      functionUtilkde::removeIncludeInFile($file, "kapplication.h");
      functionUtilkde::addIncludeInFile($file, "QApplication");
      functionUtilkde::removeIncludeInFile($file, "K4AboutData");
      functionUtilkde::removeIncludeInFile($file, "k4aboutdata.h");
      functionUtilkde::addIncludeInFile($file, "KAboutData");
Laurent Montel's avatar
Laurent Montel committed
235
      functionUtilkde::addIncludeInFile($file, "KLocalizedString");
236
237
238

    }

239
240
    if (`grep QCommand $file | grep -v '#include'`) {
      functionUtilkde::removeIncludeInFile($file, "kcmdlineargs.h");
Laurent Montel's avatar
Laurent Montel committed
241
242
243
      functionUtilkde::removeIncludeInFile($file, "KCmdLineArgs");
      functionUtilkde::removeIncludeInFile($file, "KCmdLineOptions");
      functionUtilkde::addIncludeInFile($file, "QCommandLineParser");
244
245
246
      if (defined $needQCommandLineOption) {
        functionUtilkde::addIncludeInFile($file, "QCommandLineOption");
      }
247
248
249
250
    }
}

functionUtilkde::diffFile( "@ARGV" );