forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLArgs.cpp
1366 lines (1176 loc) · 45.3 KB
/
CLArgs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron 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.
*
* Natron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "CLArgs.h"
#include <cstddef>
#include <iostream>
#include <cassert>
#include <stdexcept>
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include "Global/GlobalDefines.h"
#include "Global/GitVersion.h"
#include "Global/QtCompat.h"
#include "Global/StrUtils.h"
#include "Engine/AppManager.h"
NATRON_NAMESPACE_ENTER
struct CLArgsPrivate
{
Q_DECLARE_TR_FUNCTIONS(CLArgs)
public:
QString executable;
QStringList args;
QString filename;
bool isPythonScript;
QString defaultOnProjectLoadedScript;
std::list<CLArgs::WriterArg> writers;
std::list<CLArgs::ReaderArg> readers;
std::list<std::string> pythonCommands;
std::list<std::string> settingCommands; //!< executed after loading the settings
bool isBackground;
bool useDefaultSettings;
bool clearCacheOnLaunch;
bool clearOpenFXCacheOnLaunch;
QString ipcPipe;
int error;
bool isInterpreterMode;
std::list<std::pair<int, std::pair<int, int> > > frameRanges;
bool rangeSet;
bool enableRenderStats;
bool isEmpty;
mutable QString imageFilename;
#ifdef NATRON_USE_BREAKPAD
QString breakpadPipeFilePath;
QString breakpadComPipeFilePath;
int breakpadPipeClientID;
QString breakpadProcessFilePath;
qint64 breakpadProcessPID;
#endif
QString exportDocsPath;
CLArgsPrivate()
: args()
, filename()
, isPythonScript(false)
, defaultOnProjectLoadedScript()
, writers()
, readers()
, pythonCommands()
, settingCommands()
, isBackground(false)
, useDefaultSettings(false)
, clearCacheOnLaunch(false)
, clearOpenFXCacheOnLaunch(false)
, ipcPipe()
, error(0)
, isInterpreterMode(false)
, frameRanges()
, rangeSet(false)
, enableRenderStats(false)
, isEmpty(true)
, imageFilename()
#ifdef NATRON_USE_BREAKPAD
, breakpadPipeFilePath()
, breakpadComPipeFilePath()
, breakpadPipeClientID(-1)
, breakpadProcessFilePath()
, breakpadProcessPID(-1)
#endif
, exportDocsPath()
{
}
void parse();
QStringList::iterator hasToken(const QString& longName, const QString& shortName);
QStringList::iterator hasOutputToken(QString& indexStr);
QStringList::iterator findFileNameWithExtension(const QString& extension);
};
CLArgs::CLArgs()
: _imp( new CLArgsPrivate() )
{
}
// ctor
// Be careful that there are 3 constructors with very similar code, see below
CLArgs::CLArgs(int& argc,
char* argv[],
bool forceBackground)
: _imp( new CLArgsPrivate() )
{
_imp->isEmpty = false;
if (forceBackground) {
_imp->isBackground = true;
}
// Ensure application has correct locale before doing anything
AppManager::setApplicationLocale();
std::vector<std::string> utf8Args;
ensureCommandLineArgsUtf8(argc, argv, &utf8Args);
// save the program name (argv[0])
_imp->executable = QString::fromUtf8(utf8Args[0].c_str());
for (std::size_t i = 1; i < utf8Args.size(); ++i) {
QString str = QString::fromUtf8(utf8Args[i].c_str());
if ( (str.size() >= 2) && ( str[0] == QChar::fromLatin1('"') ) && ( str[str.size() - 1] == QChar::fromLatin1('"') ) ) {
str.remove(0, 1);
str.remove(str.size() - 1, 1);
}
_imp->args.push_back(str);
}
_imp->parse();
}
// ctor
// Be careful that there are 3 constructors with very similar code, see above and below
CLArgs::CLArgs(int& argc,
wchar_t* argv[],
bool forceBackground)
: _imp( new CLArgsPrivate() )
{
_imp->isEmpty = false;
if (forceBackground) {
_imp->isBackground = true;
}
// Ensure application has correct locale before doing anything
AppManager::setApplicationLocale();
std::vector<std::string> utf8Args;
for (int i = 0; i < argc; ++i) {
std::wstring ws(argv[i]);
std::string utf8Str = StrUtils::utf16_to_utf8(ws);
assert(StrUtils::is_utf8(utf8Str.c_str()));
QString str = QString::fromUtf8(utf8Str.c_str());
if (i == 0) {
// save the program name (argv[0])
_imp->executable = str;
continue;
}
if ( (str.size() >= 2) && ( str[0] == QChar::fromLatin1('"') ) && ( str[str.size() - 1] == QChar::fromLatin1('"') ) ) {
str.remove(0, 1);
str.remove(str.size() - 1, 1);
}
_imp->args.push_back(str);
}
_imp->parse();
}
// ctor
// Be careful that there are 3 constructors with very similar code, see above
CLArgs::CLArgs(const QStringList &arguments,
bool forceBackground)
: _imp( new CLArgsPrivate() )
{
_imp->isEmpty = false;
if (forceBackground) {
_imp->isBackground = true;
}
// Ensure application has correct locale before doing anything
AppManager::setApplicationLocale();
bool first = true;
Q_FOREACH(const QString &arg, arguments) {
if (first) {
// save the program name (argv[0])
_imp->executable = arg;
first = false;
continue;
}
QString str = arg;
// Remove surrounding quotes
if ( (str.size() >= 2) && ( str[0] == QChar::fromLatin1('"') ) && ( str[str.size() - 1] == QChar::fromLatin1('"') ) ) {
str.remove(0, 1);
str.remove(str.size() - 1, 1);
}
_imp->args.push_back(str);
}
_imp->parse();
}
// GCC 4.2 requires the copy constructor
CLArgs::CLArgs(const CLArgs& other)
: _imp( new CLArgsPrivate() )
{
*this = other;
}
CLArgs::~CLArgs()
{
}
void
CLArgs::operator=(const CLArgs& other)
{
_imp->executable = other._imp->executable;
_imp->args = other._imp->args;
_imp->filename = other._imp->filename;
_imp->isPythonScript = other._imp->isPythonScript;
_imp->defaultOnProjectLoadedScript = other._imp->defaultOnProjectLoadedScript;
_imp->clearCacheOnLaunch = other._imp->clearCacheOnLaunch;
_imp->clearOpenFXCacheOnLaunch = other._imp->clearOpenFXCacheOnLaunch;
_imp->writers = other._imp->writers;
_imp->readers = other._imp->readers;
_imp->pythonCommands = other._imp->pythonCommands;
_imp->settingCommands = other._imp->settingCommands;
_imp->isBackground = other._imp->isBackground;
_imp->ipcPipe = other._imp->ipcPipe;
_imp->error = other._imp->error;
_imp->isInterpreterMode = other._imp->isInterpreterMode;
_imp->frameRanges = other._imp->frameRanges;
_imp->rangeSet = other._imp->rangeSet;
_imp->enableRenderStats = other._imp->enableRenderStats;
_imp->isEmpty = other._imp->isEmpty;
_imp->imageFilename = other._imp->imageFilename;
_imp->exportDocsPath = other._imp->exportDocsPath;
}
bool
CLArgs::isEmpty() const
{
return _imp->isEmpty;
}
void
CLArgs::printBackGroundWelcomeMessage()
{
QString msg = tr("%1 Version %2\n"
"(C) 2018-2021 The Natron developers\n"
"(C) 2013-2018 INRIA and Alexandre Gauthier-Foichat\n"
">>>Use the --help or -h option to print usage.<<<").arg( QString::fromUtf8(NATRON_APPLICATION_NAME) ).arg( QString::fromUtf8(NATRON_VERSION_STRING) );
std::cout << msg.toStdString() << std::endl;
}
void
CLArgs::printUsage(const std::string& programName)
{
QString msg = tr( /* Text must hold in 80 columns ************************************************/
"%3 usage:\n"
"Three distinct execution modes exist in background mode:\n"
"- The execution of %1 projects (.%2)\n"
"- The execution of Python scripts that contain commands for %1\n"
"- An interpreter mode where commands can be given directly to the Python\n"
" interpreter\n"
"\n"
"General options:\n"
" -h [ --help ]\n"
" Produce help message.\n"
" -v [ --version ]\n"
" Print information about %1 version.\n"
" -b [ --background ]\n"
" Enable background rendering mode. No graphical interface is shown.\n"
" When using %1Renderer or the -t option, this argument is implicit\n"
" and does not have to be given.\n"
" If using %1 and this option is not specified, the project is loaded\n"
" as if opened from the file menu.\n"
" -t [ --interpreter ] <python script file path>\n"
" Enable Python interpreter mode.\n"
" Python commands can be given to the interpreter and executed on the fly.\n"
" An optional Python script filename can be specified to source a script\n"
" before the interpreter is made accessible.\n"
" Note that %1 will not start rendering any Write node of the sourced\n"
" script: it must be started explicitly.\n"
" %1Renderer and %1 do the same thing in this mode, only the\n"
" init.py script is loaded.\n"
" --clear-cache\n"
" Clears the image cache on startup.\n"
" --clear-openfx-cache\n"
" Clears the OpenFX plugins cache on startup.\n"
" --no-settings\n"
" When passed on the command-line, the %1 settings will not be restored\n"
" from the preferences file on disk so that %1 uses the default ones.\n"
" --settings name=value\n"
" Sets the named %1 setting to the given value. This is done after loading\n"
" the settings and prior to executing Python commands or loading the project.\n"
" -c [ --cmd ] \"PythonCommand\"\n"
" Execute custom Python code passed as a script prior to executing the Python\n"
" script or loading the project passed as parameter. This option may be used\n"
" multiple times and each python command is executed in the order given on\n"
" the command-line.\n\n"
"\n"
/* Text must hold in 80 columns ************************************************/
"Usage for the execution of %1 projects:\n"
" %3 [options] [--] <project file path> [<frameRanges>]\n"
"Options/arguments:\n"
" -w [ --writer ] <Writer node script name> [<filename>]\n"
" Specify a Write node to render.\n"
" When in background mode, the renderer only renders the node script name\n"
" following this argument. If no such node exists in the project file, the\n"
" process will abort.\n"
" Note that if there is no --writer option, it renders all the writers in\n"
" the project.\n"
" After the writer node script name you can pass an optional output\n"
" filename.\n"
" Note that several -w options can be set to specify multiple Write nodes\n"
" to render.\n"
" You may only specify absolute file paths to the writer optional filename.\n"
" -i [ --reader ] <reader node script name> <filename>\n"
" Specify the input file/sequence/video to load for the given Reader node.\n"
" If the specified reader node cannot be found, the process will abort."
" You may only specify absolute file paths to the reader filename.\n"
" -l [ --onload ] <python script file path>\n"
" Specify a Python script to be executed after a project is created or\n"
" loaded.\n"
" Note that this is executed in GUI mode or with NatronRenderer, after\n"
" executing the callbacks onProjectLoaded and onProjectCreated.\n"
" The rules on the execution of Python scripts (see below) also apply to\n"
" this script.\n"
" -s [ --render-stats]\n"
" Enable render statistics that will be produced for\n"
" each frame in form of a file located next to the image produced by\n"
" the Writer node, with the same name and a -stats.txt extension. The\n"
" breakdown contains information about each nodes, render times etc...\n"
" This option is useful for debugging purposes or to control that a render\n"
" is working correctly.\n"
" **Please note** that it does not work when writing video files."
" <frameRanges>\n"
" One or more frame ranges, separated by commas.\n"
" Each frame range must be one of the following:\n"
" - <frame> (a single frame number, e.g. 57)\n"
" - <firstFrame>-<lastFrame> (e.g. 10-40)\n"
" - <firstFrame>-<lastFrame>:<frameStep> (e.g. 1-10:2 would render 1,3,5,7,9)\n"
" Examples:\n"
" 1-10:1,20-30:2,40-50\n"
" 1329,2450,123,1-10:2\n"
"Sample uses:\n"
" %1 /Users/Me/MyNatronProjects/MyProject.ntp\n"
" %1 -b -w MyWriter /Users/Me/MyNatronProjects/MyProject.ntp\n"
" %1Renderer -w MyWriter /Users/Me/MyNatronProjects/MyProject.ntp\n"
" %1Renderer -w MyWriter /FastDisk/Pictures/sequence'###'.exr 1-100 /Users/Me/MyNatronProjects/MyProject.ntp\n"
" %1Renderer -w MyWriter -w MySecondWriter 1-10 /Users/Me/MyNatronProjects/MyProject.ntp\n"
" %1Renderer -w MyWriter 1-10 -l /Users/Me/Scripts/onProjectLoaded.py /Users/Me/MyNatronProjects/MyProject.ntp\n"
"\n"
/* Text must hold in 80 columns ************************************************/
"Options for the execution of Python scripts:\n"
" %3 [options] [--] <Python script path>\n"
" [Note that the following does not apply if the -t option was given.]\n"
" The script argument can either be the script of a Group that was exported\n"
" from the graphical user interface, or an exported project, or a script\n"
" written by hand.\n"
" When executing a script, %1 first looks for a function with the\n"
" following signature:\n"
" def createInstance(app,group):\n"
" If this function is found, it is executed, otherwise the whole content of\n"
" the script is interpreted as though it were given to Python natively.\n"
" In either case, the \"app\" variable is always defined and points to the\n"
" correct application instance.\n"
" Note that the GUI version of the program (%1) sources the script before\n"
" creating the graphical user interface and does not start rendering.\n"
" If in background mode, the nodes to render have to be given using the -w\n"
" option (as described above) or with the following option:\n"
" -o [ --output ] <filename> <frameRange>\n"
" Specify an Output node in the script that should be replaced with a\n"
" Write node.\n"
" The option looks for a node named Output1 in the script and replaces it\n"
" by a Write node (like when creating a Write node in interactive GUI mode).\n"
" <filename> is a pattern for the output file names.\n"
" <frameRange> must be specified if it was not specified earlier on the\n"
" command line.\n"
" This option can also be used to render out multiple Output nodes, in\n"
" which case it has to be used like this:\n"
" -o1 [ --output1 ] : look for a node named Output1.\n"
" -o2 [ --output2 ] : look for a node named Output2 \n"
" etc...\n"
"Sample uses:\n"
" %1 /Users/Me/MyNatronScripts/MyScript.py\n"
" %1 -b -w MyWriter /Users/Me/MyNatronScripts/MyScript.py\n"
" %1Renderer -w MyWriter /Users/Me/MyNatronScripts/MyScript.py\n"
" %1Renderer -o /FastDisk/Pictures/sequence'###'.exr 1-100 /Users/Me/MyNatronScripts/MyScript.py\n"
" %1Renderer -o1 /FastDisk/Pictures/sequence'###'.exr -o2 /FastDisk/Pictures/test'###'.exr 1-100 /Users/Me/MyNatronScripts/MyScript.py\n"
" %1Renderer -w MyWriter -o /FastDisk/Pictures/sequence'###'.exr 1-100 /Users/Me/MyNatronScripts/MyScript.py\n"
"\n"
/* Text must hold in 80 columns ************************************************/
"Options for the execution of the interpreter mode:\n"
" %3 -t [--] [<Python script path>]\n"
" %1 sources the optional script given as argument, if any, and then reads\n"
" Python commands from the standard input, which are interpreted by Python.\n"
"Sample uses:\n"
" %1 -t\n"
" %1Renderer -t\n"
" %1Renderer -t /Users/Me/MyNatronScripts/MyScript.py\n")
.arg( /*%1=*/ QString::fromUtf8(NATRON_APPLICATION_NAME) ).arg( /*%2=*/ QString::fromUtf8(NATRON_PROJECT_FILE_EXT) ).arg( /*%3=*/ QString::fromUtf8( programName.c_str() ) );
std::cout << msg.toStdString() << std::endl;
} // CLArgs::printUsage
int
CLArgs::getError() const
{
return _imp->error;
}
const std::list<CLArgs::WriterArg>&
CLArgs::getWriterArgs() const
{
return _imp->writers;
}
const std::list<CLArgs::ReaderArg>&
CLArgs::getReaderArgs() const
{
return _imp->readers;
}
const std::list<std::string>&
CLArgs::getPythonCommands() const
{
return _imp->pythonCommands;
}
const std::list<std::string>&
CLArgs::getSettingCommands() const
{
return _imp->settingCommands;
}
bool
CLArgs::hasFrameRange() const
{
return _imp->rangeSet;
}
const std::list<std::pair<int, std::pair<int, int> > >&
CLArgs::getFrameRanges() const
{
return _imp->frameRanges;
}
bool
CLArgs::isLoadedUsingDefaultSettings() const
{
return _imp->useDefaultSettings;
}
bool
CLArgs::isCacheClearRequestedOnLaunch() const
{
return _imp->clearCacheOnLaunch;
}
bool
CLArgs::isOpenFXCacheClearRequestedOnLaunch() const
{
return _imp->clearOpenFXCacheOnLaunch;
}
bool
CLArgs::isBackgroundMode() const
{
return _imp->isBackground;
}
bool
CLArgs::isInterpreterMode() const
{
return _imp->isInterpreterMode;
}
const QString&
CLArgs::getScriptFilename() const
{
return _imp->filename;
}
const QString&
CLArgs::getImageFilename() const
{
if ( _imp->imageFilename.isEmpty() && !_imp->args.empty() ) {
///Check for image file passed to command line
QStringList::iterator it = _imp->args.begin();
for (; it != _imp->args.end(); ++it) {
if ( !it->startsWith( QChar::fromLatin1('-') ) ) {
QString fileCopy = *it;
QString ext = QtCompat::removeFileExtension(fileCopy);
if ( !ext.isEmpty() ) {
std::string readerId = appPTR->getReaderPluginIDForFileType( ext.toStdString() );
if ( !readerId.empty() ) {
_imp->imageFilename = *it;
_imp->args.erase(it);
break;
}
}
}
}
}
return _imp->imageFilename;
}
const QString&
CLArgs::getDefaultOnProjectLoadedScript() const
{
return _imp->defaultOnProjectLoadedScript;
}
const QString&
CLArgs::getIPCPipeName() const
{
return _imp->ipcPipe;
}
bool
CLArgs::areRenderStatsEnabled() const
{
return _imp->enableRenderStats;
}
bool
CLArgs::isPythonScript() const
{
return _imp->isPythonScript;
}
#ifdef NATRON_USE_BREAKPAD
const QString&
CLArgs::getBreakpadProcessExecutableFilePath() const
{
return _imp->breakpadProcessFilePath;
}
qint64
CLArgs::getBreakpadProcessPID() const
{
return _imp->breakpadProcessPID;
}
int
CLArgs::getBreakpadClientFD() const
{
return _imp->breakpadPipeClientID;
}
const QString&
CLArgs::getBreakpadPipeFilePath() const
{
return _imp->breakpadPipeFilePath;
}
const QString&
CLArgs::getBreakpadComPipeFilePath() const
{
return _imp->breakpadComPipeFilePath;
}
#endif // NATRON_USE_BREAKPAD
const QString &
CLArgs::getExportDocsPath() const
{
return _imp->exportDocsPath;
}
QStringList::iterator
CLArgsPrivate::findFileNameWithExtension(const QString& extension)
{
for (QStringList::iterator it = args.begin(); it != args.end(); ++it) {
if ( it->endsWith(QChar::fromLatin1('.') + extension, Qt::CaseInsensitive) ) {
return it;
}
}
return args.end();
}
QStringList::iterator
CLArgsPrivate::hasToken(const QString& longName,
const QString& shortName)
{
const QString longToken( QString::fromUtf8("--") + longName );
const QString shortToken = !shortName.isEmpty() ? QChar::fromLatin1('-') + shortName : QString();
const QString endOfOptions( QString::fromUtf8("--") );
for (QStringList::iterator it = args.begin(); it != args.end() && *it != endOfOptions; ++it) {
if ( (*it == longToken) || ( !shortToken.isEmpty() && (*it == shortToken) ) ) {
return it;
}
}
return args.end();
}
QStringList::iterator
CLArgsPrivate::hasOutputToken(QString& indexStr)
{
const QString outputLong( QString::fromUtf8("--output") );
const QString outputShort( QString::fromUtf8("-o") );
const QString endOfOptions( QString::fromUtf8("--") );
for (QStringList::iterator it = args.begin(); it != args.end() && *it != endOfOptions; ++it) {
int indexOf = it->indexOf(outputLong);
if (indexOf != -1) {
indexOf += outputLong.size();
if ( indexOf < it->size() ) {
indexStr = it->mid(indexOf);
bool ok;
indexStr.toInt(&ok);
if (!ok) {
error = 1;
std::cout << tr("Wrong formatting for the -o option").toStdString() << std::endl;
return args.end();
}
} else {
indexStr = QChar::fromLatin1('1');
}
return it;
} else {
indexOf = it->indexOf(outputShort);
if (indexOf != -1) {
if ( (it->size() > 2) && !it->at(2).isDigit() ) {
//This is probably the --onload option
return args.end();
}
indexOf += outputShort.size();
if ( indexOf < it->size() ) {
indexStr = it->mid(indexOf);
bool ok;
indexStr.toInt(&ok);
if (!ok) {
error = 1;
std::cout << tr("Wrong formatting for the -o option").toStdString() << std::endl;
return args.end();
}
} else {
indexStr = QChar::fromLatin1('1');
}
return it;
}
}
}
return args.end();
} // CLArgsPrivate::hasOutputToken
static bool
tryParseFrameRange(const QString& arg,
std::pair<int, int>& range,
int& frameStep)
{
bool ok;
int singleNumber = arg.toInt(&ok);
if (ok) {
//this is a single frame
range.first = range.second = singleNumber;
frameStep = INT_MIN;
return true;
}
QStringList strRange = arg.split( QChar::fromLatin1('-') );
if (strRange.size() != 2) {
return false;
}
for (int i = 0; i < strRange.size(); ++i) {
//whitespace removed from the start and the end.
strRange[i] = strRange.at(i).trimmed();
}
range.first = strRange.at(0).toInt(&ok);
if (!ok) {
return false;
}
int foundColon = strRange.at(1).indexOf( QChar::fromLatin1(':') );
if (foundColon != -1) {
///A frame-step has been specified
QString lastFrameStr = strRange.at(1).mid(0, foundColon);
QString frameStepStr = strRange.at(1).mid(foundColon + 1);
range.second = lastFrameStr.toInt(&ok);
if (!ok) {
return false;
}
frameStep = frameStepStr.toInt(&ok);
if (!ok) {
return false;
}
} else {
frameStep = INT_MIN;
///Frame range without frame-step specified
range.second = strRange.at(1).toInt(&ok);
if (!ok) {
return false;
}
}
return true;
}
static bool
tryParseMultipleFrameRanges(const QString& args,
std::list<std::pair<int, std::pair<int, int> > >& frameRanges)
{
QStringList splits = args.split( QChar::fromLatin1(',') );
std::list<std::pair<int, std::pair<int, int> > > newFrameRanges;
Q_FOREACH(const QString &split, splits) {
std::pair<int, int> frameRange;
int frameStep = 0;
if ( !tryParseFrameRange(split, frameRange, frameStep) ) {
// not a frame range.
return false;
} else {
newFrameRanges.push_back( std::make_pair(frameStep, frameRange) );
}
}
if ( newFrameRanges.empty() ) {
return false;
}
frameRanges.merge(newFrameRanges); // empties newFrameRanges
return true;
}
void
CLArgsPrivate::parse()
{
#ifdef DEBUG
qDebug() << "args:";
for (QStringList::iterator it = args.begin(); it != args.end(); ++it) {
qDebug() << *it;
}
#endif
{
QStringList::iterator it = hasToken( QString::fromUtf8("version"), QString::fromUtf8("v") );
if ( it != args.end() ) {
it = args.erase(it);
QString msg = tr("%1 version %2 at commit %3 on branch %4").arg( QString::fromUtf8(NATRON_APPLICATION_NAME) ).arg( QString::fromUtf8(NATRON_VERSION_STRING) ).arg( QString::fromUtf8(GIT_COMMIT) ).arg( QString::fromUtf8(GIT_BRANCH) );
# if defined(NATRON_CONFIG_SNAPSHOT) || defined(DEBUG)
msg += tr(" built on %1").arg( QString::fromUtf8(__DATE__) );
# endif
std::cout << msg.toStdString() << std::endl;
error = 1;
return;
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8("help"), QString::fromUtf8("h") );
if ( it != args.end() ) {
it = args.erase(it);
CLArgs::printUsage( executable.toStdString() );
error = 1;
return;
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8("clear-cache"), QString() );
if ( it != args.end() ) {
it = args.erase(it);
clearCacheOnLaunch = true;
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8("clear-openfx-cache"), QString() );
if ( it != args.end() ) {
it = args.erase(it);
clearOpenFXCacheOnLaunch = true;
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8("no-settings"), QString() );
if ( it != args.end() ) {
it = args.erase(it);
useDefaultSettings = true;
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8("background"), QString::fromUtf8("b") );
if ( it != args.end() ) {
it = args.erase(it);
isBackground = true;
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8("interpreter"), QString::fromUtf8("t") );
if ( it != args.end() ) {
it = args.erase(it);
isInterpreterMode = true;
isBackground = true;
std::cout << tr("Note: -t argument given, loading in command-line interpreter mode, only Python commands / scripts are accepted").toStdString()
<< std::endl;
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8("render-stats"), QString::fromUtf8("s") );
if ( it != args.end() ) {
it = args.erase(it);
enableRenderStats = true;
}
}
#ifdef NATRON_USE_BREAKPAD
{
QStringList::iterator it = hasToken( QString::fromUtf8(NATRON_BREAKPAD_PROCESS_PID), QString() );
if ( it != args.end() ) {
it = args.erase(it);
if ( it == args.end() ) {
std::cout << tr("You must specify the breakpad process executable file path").toStdString() << std::endl;
error = 1;
return;
}
breakpadProcessPID = it->toLongLong();
it = args.erase(it);
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8(NATRON_BREAKPAD_PROCESS_EXEC), QString() );
if ( it != args.end() ) {
it = args.erase(it);
if ( it == args.end() || it->startsWith( QChar::fromLatin1('-') ) ) {
std::cout << tr("You must specify the breakpad process executable file path").toStdString() << std::endl;
error = 1;
return;
}
breakpadProcessFilePath = *it;
it = args.erase(it);
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8(NATRON_BREAKPAD_CLIENT_FD_ARG), QString() );
if ( it != args.end() ) {
it = args.erase(it);
if ( it == args.end() || it->startsWith( QChar::fromLatin1('-') ) ) {
std::cout << tr("You must specify the breakpad pipe client FD").toStdString() << std::endl;
error = 1;
return;
}
breakpadPipeClientID = it->toInt();
it = args.erase(it);
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8(NATRON_BREAKPAD_PIPE_ARG), QString() );
if ( it != args.end() ) {
it = args.erase(it);
if ( it == args.end() || it->startsWith( QChar::fromLatin1('-') ) ) {
std::cout << tr("You must specify the breakpad pipe path").toStdString() << std::endl;
error = 1;
return;
}
breakpadPipeFilePath = *it;
args.erase(it);
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8(NATRON_BREAKPAD_COM_PIPE_ARG), QString() );
if ( it != args.end() ) {
it = args.erase(it);
if ( it == args.end() || it->startsWith( QChar::fromLatin1('-') ) ) {
std::cout << tr("You must specify the breakpad communication pipe path").toStdString() << std::endl;
error = 1;
return;
}
breakpadComPipeFilePath = *it;
it = args.erase(it);
}
}
#endif // NATRON_USE_BREAKPAD
{
QStringList::iterator it = hasToken( QString::fromUtf8("export-docs"), QString() );
if ( it != args.end() ) {
it = args.erase(it);
if ( it == args.end() || it->startsWith( QChar::fromLatin1('-') ) ) {
std::cout << tr("You must specify the doc dir path").toStdString() << std::endl;
error = 1;
return;
}
exportDocsPath = *it;
it = args.erase(it);
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8("IPCpipe"), QString() );
if ( it != args.end() ) {
it = args.erase(it);
if ( it == args.end() || it->startsWith( QChar::fromLatin1('-') ) ) {
std::cout << tr("You must specify the IPC pipe filename").toStdString() << std::endl;
error = 1;
return;
}
ipcPipe = *it;
it = args.erase(it);
}
}
{
QStringList::iterator it = hasToken( QString::fromUtf8("onload"), QString::fromUtf8("l") );
if ( it != args.end() ) {
it = args.erase(it);
if ( it == args.end() || it->startsWith( QChar::fromLatin1('-') ) ) {
std::cout << tr("--onload or -l specified, you must enter a script filename afterwards.").toStdString() << std::endl;
error = 1;
return;
}
defaultOnProjectLoadedScript = *it;
it = args.erase(it);
#ifdef __NATRON_UNIX__
defaultOnProjectLoadedScript = AppManager::qt_tildeExpansion(defaultOnProjectLoadedScript);
#endif
QFileInfo fi(defaultOnProjectLoadedScript);
if ( !fi.exists() ) {
std::cout << tr("WARNING: --onload %1 ignored because the file does not exist.").arg(defaultOnProjectLoadedScript).toStdString() << std::endl;
defaultOnProjectLoadedScript.clear();
} else {
defaultOnProjectLoadedScript = fi.canonicalFilePath();
}
if ( !defaultOnProjectLoadedScript.endsWith( QString::fromUtf8(".py") ) ) {
std::cout << tr("The optional on project load script must be a Python script (filename ending with .py).").toStdString() << std::endl;
error = 1;