-
Notifications
You must be signed in to change notification settings - Fork 181
/
app.cpp
1433 lines (1146 loc) · 44.4 KB
/
app.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
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#include <unistd.h>
#include <fcntl.h>
#include <locale>
#include <clocale>
#include <algorithm>
#include "app.h"
#include "debug.h"
#include "progressbar.h"
#include "file/path.h"
#include "file/config.h"
#include "signal_handler.h"
#define MRTRIX_HELP_COMMAND "less -X"
#define HELP_WIDTH 80
#define HELP_PURPOSE_INDENT 0, 4
#define HELP_ARG_INDENT 8, 20
#define HELP_OPTION_INDENT 2, 20
#define HELP_EXAMPLE_INDENT 7
#define MRTRIX_CORE_REFERENCE "Tournier, J.-D.; Smith, R. E.; Raffelt, D.; Tabbara, R.; Dhollander, T.; Pietsch, M.; Christiaens, D.; Jeurissen, B.; Yeh, C.-H. & Connelly, A. " \
"MRtrix3: A fast, flexible and open software framework for medical image processing and visualisation. " \
"NeuroImage, 2019, 202, 116137"
namespace MR
{
namespace App
{
Description DESCRIPTION;
ExampleList EXAMPLES;
ArgumentList ARGUMENTS;
OptionList OPTIONS;
Description REFERENCES;
bool REQUIRES_AT_LEAST_ONE_ARGUMENT = true;
OptionGroup __standard_options = OptionGroup ("Standard options")
+ Option ("info", "display information messages.")
+ Option ("quiet", "do not display information messages or progress status; "
"alternatively, this can be achieved by setting the MRTRIX_QUIET environment variable to a non-empty string.")
+ Option ("debug", "display debugging messages.")
+ Option ("force", "force overwrite of output files "
"(caution: using the same file as input and output might cause unexpected behaviour).")
+ Option ("nthreads", "use this number of threads in multi-threaded applications (set to 0 to disable multi-threading).")
+ Argument ("number").type_integer (0)
+ Option ("config", "temporarily set the value of an MRtrix config file entry.").allow_multiple()
+ Argument ("key").type_text()
+ Argument ("value").type_text()
+ Option ("help", "display this information page and exit.")
+ Option ("version", "display version information and exit.");
const char* AUTHOR = nullptr;
const char* COPYRIGHT =
"Copyright (c) 2008-2022 the MRtrix3 contributors.\n"
"\n"
"This Source Code Form is subject to the terms of the Mozilla Public\n"
"License, v. 2.0. If a copy of the MPL was not distributed with this\n"
"file, You can obtain one at http://mozilla.org/MPL/2.0/.\n"
"\n"
"Covered Software is provided under this License on an \"as is\"\n"
"basis, without warranty of any kind, either expressed, implied, or\n"
"statutory, including, without limitation, warranties that the\n"
"Covered Software is free of defects, merchantable, fit for a\n"
"particular purpose or non-infringing.\n"
"See the Mozilla Public License v. 2.0 for more details.\n"
"\n"
"For more details, see http://www.mrtrix.org/.\n";
const char* SYNOPSIS = nullptr;
std::string NAME;
std::string command_history_string;
vector<ParsedArgument> argument;
vector<ParsedOption> option;
//ENVVAR name: MRTRIX_QUIET
//ENVVAR Do not display information messages or progress status. This has
//ENVVAR the same effect as the ``-quiet`` command-line option. If set,
//ENVVAR supersedes the MRTRIX_LOGLEVEL environment variable.
//ENVVAR name: MRTRIX_LOGLEVEL
//ENVVAR Set the default terminal verbosity. Default terminal verbosity
//ENVVAR is 1. This has the same effect as the ``-quiet`` (0),
//ENVVAR ``-info`` (2) or ``-debug`` (3) comand-line options.
int log_level = getenv("MRTRIX_QUIET") ?
0 :
(getenv("MRTRIX_LOGLEVEL") ? to<int>(getenv("MRTRIX_LOGLEVEL")) : 1);
int exit_error_code = 0;
bool fail_on_warn = false;
bool terminal_use_colour = true;
const std::thread::id main_thread_ID = std::this_thread::get_id();
const char* project_version = nullptr;
const char* project_build_date = nullptr;
const char* executable_uses_mrtrix_version = nullptr;
int argc = 0;
const char* const* argv = nullptr;
bool overwrite_files = false;
void (*check_overwrite_files_func) (const std::string& name) = nullptr;
namespace
{
inline void get_matches (vector<const Option*>& candidates, const OptionGroup& group, const std::string& stub)
{
for (size_t i = 0; i < group.size(); ++i) {
if (stub.compare (0, stub.size(), group[i].id, stub.size()) == 0)
candidates.push_back (&group[i]);
}
}
inline int size (const std::string& text)
{
return text.size() - 2*std::count (text.begin(), text.end(), 0x08U);
}
inline void resize (std::string& text, size_t new_size, char fill)
{
text.resize (text.size() + new_size - size(text), fill);
}
std::string paragraph (
const std::string& header,
const std::string& text,
int header_indent,
int indent)
{
std::string out, line = std::string (header_indent, ' ') + header + " ";
if (size (line) < indent)
resize (line, indent, ' ');
vector<std::string> paragraphs = split (text, "\n");
for (size_t n = 0; n < paragraphs.size(); ++n) {
size_t i = 0;
vector<std::string> words = split (paragraphs[n]);
while (i < words.size()) {
do {
line += " " + words[i++];
if (i >= words.size())
break;
}
while (size (line) + 1 + size (words[i]) < HELP_WIDTH);
out += line + "\n";
line = std::string (indent, ' ');
}
}
return out;
}
std::string bold (const std::string& text)
{
std::string retval (3*text.size(), '\0');
for (size_t n = 0; n < text.size(); ++n) {
retval[3*n] = retval[3*n+2] = text[n];
retval[3*n+1] = 0x08U;
}
return retval;
}
std::string underline (const std::string& text, bool ignore_whitespace = false)
{
size_t m (0);
std::string retval (3*text.size(), '\0');
for (size_t n = 0; n < text.size(); ++n) {
if (ignore_whitespace and text[n]==' ')
retval[m++] = ' ';
else
retval[m++] = '_';
retval[m++] = 0x08U;
retval[m++] = text[n];
}
return retval;
}
}
const char* argtype_description (ArgType type)
{
switch (type) {
case Integer:
return ("integer");
case Float:
return ("float");
case Text:
return ("string");
case ArgFileIn:
return ("file in");
case ArgFileOut:
return ("file out");
case ArgDirectoryIn:
return ("directory in");
case ArgDirectoryOut:
return ("directory out");
case ImageIn:
return ("image in");
case ImageOut:
return ("image out");
case Choice:
return ("choice");
case IntSeq:
return ("int seq");
case FloatSeq:
return ("float seq");
case TracksIn:
return ("tracks in");
case TracksOut:
return ("tracks out");
case Various:
return ("various");
default:
return ("undefined");
}
}
std::string help_head (int format)
{
if (!format) {
return std::string (NAME) + ": " + (project_version ?
std::string ("external MRtrix3 project, version ") + project_version + "\nbuilt against MRtrix3 version " + mrtrix_version :
std::string ("part of the MRtrix3 package, version ") + mrtrix_version) + "\n\n";
}
std::string version_string = project_version ?
std::string ("Version ") + project_version :
std::string ("MRtrix ") + mrtrix_version;
std::string date (project_version ? project_build_date : build_date);
std::string topline = version_string +
std::string (std::max (1, 40-size(version_string)-size(App::NAME)/2), ' ') +
bold (App::NAME);
topline += std::string (80-size(topline)-size(date), ' ') + date;
if (project_version)
topline += std::string("\nusing MRtrix3 ") + mrtrix_version;
return topline + "\n\n " + bold (NAME) + ": " +
(project_version ? "external MRtrix3 project" : "part of the MRtrix3 package") + "\n\n";
}
std::string help_synopsis (int format)
{
if (!format)
return SYNOPSIS;
return bold("SYNOPSIS") + "\n\n" + paragraph ("", SYNOPSIS, HELP_PURPOSE_INDENT) + "\n";
}
std::string help_tail (int format)
{
std::string retval;
if (!format)
return retval;
return bold ("AUTHOR") + "\n"
+ paragraph ("", AUTHOR, HELP_PURPOSE_INDENT) + "\n"
+ bold ("COPYRIGHT") + "\n"
+ paragraph ("", COPYRIGHT, HELP_PURPOSE_INDENT) + "\n"
+ [&](){
std::string s = bold ("REFERENCES") + "\n";
for (size_t n = 0; n < REFERENCES.size(); ++n)
s += paragraph ("", REFERENCES[n], HELP_PURPOSE_INDENT) + "\n";
s += paragraph ("", MRTRIX_CORE_REFERENCE, HELP_PURPOSE_INDENT) + "\n";
return s;
}();
}
std::string usage_syntax (int format)
{
std::string s = "USAGE";
if (format)
s = bold (s) + "\n\n ";
else
s += ": ";
s += ( format ? underline (NAME, true) : NAME ) + " [ options ]";
for (size_t i = 0; i < ARGUMENTS.size(); ++i) {
if (ARGUMENTS[i].flags & Optional)
s += " [";
s += std::string(" ") + ARGUMENTS[i].id;
if (ARGUMENTS[i].flags & AllowMultiple) {
if (! (ARGUMENTS[i].flags & Optional))
s += std::string(" [ ") + ARGUMENTS[i].id;
s += " ...";
}
if (ARGUMENTS[i].flags & (Optional | AllowMultiple))
s += " ]";
}
return s + "\n\n";
}
std::string Description::syntax (int format) const
{
if (!size())
return std::string();
std::string s;
if (format)
s += bold ("DESCRIPTION") + "\n\n";
for (size_t i = 0; i < size(); ++i)
s += paragraph ("", (*this)[i], HELP_PURPOSE_INDENT) + "\n";
return s;
}
Example::operator std::string () const
{
return title + ": $ " + code + " " + description;
}
std::string Example::syntax (int format) const
{
std::string s = paragraph ("", format ? underline (title + ":") + "\n" : title + ": ", HELP_PURPOSE_INDENT);
s += std::string (HELP_EXAMPLE_INDENT, ' ') + "$ " + code + "\n";
if (description.size())
s += paragraph ("", description, HELP_PURPOSE_INDENT);
if (format)
s += "\n";
return s;
}
std::string ExampleList::syntax (int format) const
{
if (!size())
return std::string();
std::string s;
if (format)
s += bold ("EXAMPLE USAGES") + "\n\n";
for (size_t i = 0; i < size(); ++i)
s += (*this)[i].syntax (format);
return s;
}
std::string Argument::syntax (int format) const
{
std::string retval = paragraph (( format ? underline (id, true) : id ), desc, HELP_ARG_INDENT);
if (format)
retval += "\n";
return retval;
}
std::string ArgumentList::syntax (int format) const
{
std::string s;
for (size_t i = 0; i < size(); ++i)
s += (*this)[i].syntax (format);
return s + "\n";
}
std::string Option::syntax (int format) const
{
std::string opt ("-");
opt += id;
if (format)
opt = underline (opt);
for (size_t i = 0; i < size(); ++i)
opt += std::string (" ") + (*this)[i].id;
if (format && (flags & AllowMultiple))
opt += " (multiple uses permitted)";
if (format)
opt = " " + opt + "\n" + paragraph ("", desc, HELP_PURPOSE_INDENT) + "\n";
else
opt = paragraph (opt, desc, HELP_OPTION_INDENT);
return opt;
}
std::string OptionGroup::header (int format) const
{
return format ? bold (name) + "\n\n" : std::string (name) + ":\n";
}
std::string OptionGroup::contents (int format) const
{
std::string s;
for (size_t i = 0; i < size(); ++i)
s += (*this)[i].syntax (format);
return s;
}
std::string OptionGroup::footer (int format)
{
return format ? "" : "\n";
}
std::string OptionList::syntax (int format) const
{
vector<std::string> group_names;
for (size_t i = 0; i < size(); ++i) {
if (std::find (group_names.begin(), group_names.end(), (*this)[i].name) == group_names.end())
group_names.push_back ((*this)[i].name);
}
std::string s;
for (size_t i = 0; i < group_names.size(); ++i) {
size_t n = i;
while ((*this)[n].name != group_names[i])
++n;
s += (*this)[n].header (format);
while (n < size()) {
if ((*this)[n].name == group_names[i])
s += (*this)[n].contents (format);
++n;
}
s += OptionGroup::footer (format);
}
return s;
}
std::string Argument::usage () const
{
std::ostringstream stream;
stream << "ARGUMENT " << id << " "
<< (flags & Optional ? '1' : '0') << " "
<< (flags & AllowMultiple ? '1' : '0') << " ";
switch (type) {
case Undefined:
assert (0);
break;
case Integer:
stream << "INT " << limits.i.min << " " << limits.i.max;
break;
case Float:
stream << "FLOAT " << limits.f.min << " " << limits.f.max;
break;
case Text:
stream << "TEXT";
break;
case ArgFileIn:
stream << "FILEIN";
break;
case ArgFileOut:
stream << "FILEOUT";
break;
case ArgDirectoryIn:
stream << "DIRIN";
break;
case ArgDirectoryOut:
stream << "DIROUT";
break;
case Choice:
stream << "CHOICE";
for (const char* const* p = limits.choices; *p; ++p)
stream << " " << *p;
break;
case ImageIn:
stream << "IMAGEIN";
break;
case ImageOut:
stream << "IMAGEOUT";
break;
case IntSeq:
stream << "ISEQ";
break;
case FloatSeq:
stream << "FSEQ";
break;
case TracksIn:
stream << "TRACKSIN";
break;
case TracksOut:
stream << "TRACKSOUT";
break;
case Various:
stream << "VARIOUS";
break;
default:
assert (0);
}
stream << "\n";
if (desc.size())
stream << desc << "\n";
return stream.str();
}
std::string Option::usage () const
{
std::ostringstream stream;
stream << "OPTION " << id << " "
<< (flags & Optional ? '1' : '0') << " "
<< (flags & AllowMultiple ? '1' : '0') << "\n";
if (desc.size())
stream << desc << "\n";
for (size_t i = 0; i < size(); ++i)
stream << (*this)[i].usage ();
return stream.str();
}
std::string get_help_string (int format)
{
return
help_head (format)
+ help_synopsis (format)
+ usage_syntax (format)
+ ARGUMENTS.syntax (format)
+ DESCRIPTION.syntax (format)
+ EXAMPLES.syntax (format)
+ OPTIONS.syntax (format)
+ __standard_options.header (format)
+ __standard_options.contents (format)
+ __standard_options.footer (format)
+ help_tail (format);
}
void print_help ()
{
File::Config::init ();
//CONF option: HelpCommand
//CONF default: less
//CONF The command to use to display each command's help page (leave
//CONF empty to send directly to the terminal).
const std::string help_display_command = File::Config::get ("HelpCommand", MRTRIX_HELP_COMMAND);
if (help_display_command.size()) {
std::string help_string = get_help_string (1);
FILE* file = popen (help_display_command.c_str(), "w");
if (!file) {
INFO ("error launching help display command \"" + help_display_command + "\": " + strerror (errno));
}
else if (fwrite (help_string.c_str(), 1, help_string.size(), file) != help_string.size()) {
INFO ("error sending help page to display command \"" + help_display_command + "\": " + strerror (errno));
}
if (pclose (file) == 0)
return;
INFO ("error launching help display command \"" + help_display_command + "\"");
}
if (help_display_command.size())
INFO ("displaying help page using fail-safe output:\n");
print (get_help_string (0));
}
#ifndef MRTRIX_BUILD_TYPE
#error "MRtrix build type is not defined; you need to re-run configure script"
#endif
std::string version_string ()
{
std::string version =
"== " + App::NAME + " " + ( project_version ? project_version : mrtrix_version ) + " ==\n" +
str(8*sizeof (size_t)) + " bit " + MRTRIX_BUILD_TYPE + ", built " + build_date
+ ( project_version ? std::string(" against MRtrix ") + mrtrix_version : std::string("") )
+ ", using Eigen " + str(EIGEN_WORLD_VERSION) + "." + str(EIGEN_MAJOR_VERSION) + "." + str(EIGEN_MINOR_VERSION) + "\n"
"Author(s): " + AUTHOR + "\n" +
COPYRIGHT + "\n";
return version;
}
std::string full_usage ()
{
std::string s;
s += SYNOPSIS + std::string("\n");
for (size_t i = 0; i < DESCRIPTION.size(); ++i)
s += DESCRIPTION[i] + std::string("\n");
for (size_t i = 0; i < EXAMPLES.size(); ++i)
s += std::string (EXAMPLES[i]) + std::string("\n");
for (size_t i = 0; i < ARGUMENTS.size(); ++i)
s += ARGUMENTS[i].usage();
for (size_t i = 0; i < OPTIONS.size(); ++i)
for (size_t j = 0; j < OPTIONS[i].size(); ++j)
s += OPTIONS[i][j].usage();
for (size_t i = 0; i < __standard_options.size(); ++i)
s += __standard_options[i].usage ();
return s;
}
std::string markdown_usage ()
{
/*
help_head (format)
+ help_synopsis (format)
+ usage_syntax (format)
+ ARGUMENTS.syntax (format)
+ DESCRIPTION.syntax (format)
+ EXAMPLES.syntax (format)
+ OPTIONS.syntax (format)
+ __standard_options.header (format)
+ __standard_options.contents (format)
+ __standard_options.footer (format)
+ help_tail (format);
*/
std::string s = std::string("## Synopsis\n\n") + SYNOPSIS + "\n\n";
s += "## Usage\n\n "
+ std::string(NAME) + " [ options ] ";
// Syntax line:
for (size_t i = 0; i < ARGUMENTS.size(); ++i) {
if (ARGUMENTS[i].flags & Optional)
s += "[";
s += std::string(" ") + ARGUMENTS[i].id;
if (ARGUMENTS[i].flags & AllowMultiple) {
if (! (ARGUMENTS[i].flags & Optional))
s += std::string(" [ ") + ARGUMENTS[i].id;
s += " ...";
}
if (ARGUMENTS[i].flags & (Optional | AllowMultiple))
s += " ]";
}
s += "\n\n";
// Argument description:
for (size_t i = 0; i < ARGUMENTS.size(); ++i)
s += std::string("- *") + ARGUMENTS[i].id + "*: " + ARGUMENTS[i].desc + "\n";
if (DESCRIPTION.size()) {
s += "\n## Description\n\n";
for (size_t i = 0; i < DESCRIPTION.size(); ++i)
s += std::string (DESCRIPTION[i]) + "\n\n";
}
if (EXAMPLES.size()) {
s += "\n## Example usages\n\n";
for (size_t i = 0; i < EXAMPLES.size(); ++i) {
s += std::string ("__") + EXAMPLES[i].title + ":__\n";
s += std::string ("`$ ") + EXAMPLES[i].code + "`\n";
if (EXAMPLES[i].description.size())
s += EXAMPLES[i].description + "\n";
s += "\n";
}
}
vector<std::string> group_names;
for (size_t i = 0; i < OPTIONS.size(); ++i) {
if (std::find (group_names.begin(), group_names.end(), OPTIONS[i].name) == group_names.end())
group_names.push_back (OPTIONS[i].name);
}
auto format_option = [&](const Option& opt) {
std::string f = std::string ("+ **-") + opt.id;
for (size_t a = 0; a < opt.size(); ++a)
f += std::string (" ") + opt[a].id;
f += "**";
if (opt.flags & AllowMultiple)
f+= " *(multiple uses permitted)*";
f += std::string("<br>") + opt.desc + "\n\n";
return f;
};
s += "\n## Options\n\n";
for (size_t i = 0; i < group_names.size(); ++i) {
size_t n = i;
while (OPTIONS[n].name != group_names[i])
++n;
if (OPTIONS[n].name != std::string("OPTIONS"))
s += std::string ("#### ") + OPTIONS[n].name + "\n\n";
while (n < OPTIONS.size()) {
if (OPTIONS[n].name == group_names[i]) {
for (size_t o = 0; o < OPTIONS[n].size(); ++o)
s += format_option (OPTIONS[n][o]);
}
++n;
}
}
s += "#### Standard options\n\n";
for (size_t i = 0; i < __standard_options.size(); ++i)
s += format_option (__standard_options[i]);
s += std::string ("## References\n\n");
for (size_t i = 0; i < REFERENCES.size(); ++i)
s += std::string (REFERENCES[i]) + "\n\n";
s += std::string (MRTRIX_CORE_REFERENCE) + "\n\n";
s += std::string("---\n\nMRtrix ") + mrtrix_version + ", built " + build_date + "\n\n"
"\n\n**Author:** " + AUTHOR
+ "\n\n**Copyright:** " + COPYRIGHT + "\n\n";
return s;
}
std::string restructured_text_usage ()
{
/*
help_head (format)
+ help_synopsis (format)
+ usage_syntax (format)
+ ARGUMENTS.syntax (format)
+ DESCRIPTION.syntax (format)
+ EXAMPLES.syntax (format)
+ OPTIONS.syntax (format)
+ __standard_options.header (format)
+ __standard_options.contents (format)
+ __standard_options.footer (format)
+ help_tail (format);
*/
std::string s = std::string("Synopsis\n--------\n\n") + SYNOPSIS + "\n\n";
s += "Usage\n--------\n\n::\n\n "
+ std::string(NAME) + " [ options ] ";
// Syntax line:
for (size_t i = 0; i < ARGUMENTS.size(); ++i) {
if (ARGUMENTS[i].flags & Optional)
s += "[";
s += std::string(" ") + ARGUMENTS[i].id;
if (ARGUMENTS[i].flags & AllowMultiple) {
if (! (ARGUMENTS[i].flags & Optional))
s += std::string(" [ ") + ARGUMENTS[i].id;
s += " ...";
}
if (ARGUMENTS[i].flags & (Optional | AllowMultiple))
s += " ]";
}
s += "\n\n";
// Will need more sophisticated escaping of special characters
// if they start popping up in argument / option descriptions
auto escape_special = [] (std::string text) {
size_t index = 0;
while ((index = text.find("|", index)) != std::string::npos) {
text.replace (index, 1, "\\|");
index += 2;
}
return text;
};
// Argument description:
for (size_t i = 0; i < ARGUMENTS.size(); ++i) {
auto desc = split_lines (escape_special (ARGUMENTS[i].desc), false);
s += std::string("- *") + ARGUMENTS[i].id + "*: " + desc[0];
for (size_t n = 1; n < desc.size(); ++n)
s += " |br|\n " + desc[n];
s+= "\n";
}
s += "\n";
if (DESCRIPTION.size()) {
s += "Description\n-----------\n\n";
for (size_t i = 0; i < DESCRIPTION.size(); ++i) {
auto desc = split_lines (DESCRIPTION[i], false);
s += desc[0];
for (size_t n = 1; n < desc.size(); ++n)
s += " |br|\n" + desc[n];
s += "\n\n";
}
}
if (EXAMPLES.size()) {
s += "Example usages\n--------------\n\n";
for (size_t i = 0; i < EXAMPLES.size(); ++i) {
s += std::string ("- *") + EXAMPLES[i].title + "*::\n\n";
s += std::string (" $ ") + EXAMPLES[i].code + "\n\n";
if (EXAMPLES[i].description.size())
s += std::string (" ") + EXAMPLES[i].description + "\n\n";
}
}
vector<std::string> group_names;
for (size_t i = 0; i < OPTIONS.size(); ++i) {
if (std::find (group_names.begin(), group_names.end(), OPTIONS[i].name) == group_names.end())
group_names.push_back (OPTIONS[i].name);
}
auto format_option = [&](const Option& opt) {
std::string f = std::string ("- **-") + opt.id;
for (size_t a = 0; a < opt.size(); ++a)
f += std::string (" ") + opt[a].id;
f += std::string("** ");
if (opt.flags & AllowMultiple)
f += "*(multiple uses permitted)* ";
auto desc = split_lines (opt.desc, false);
f += escape_special (desc[0]);
for (size_t n = 1; n < desc.size(); ++n)
f += " |br|\n " + escape_special (desc[n]);
f += "\n\n";
return f;
};
s += "Options\n-------\n\n";
for (size_t i = 0; i < group_names.size(); ++i) {
size_t n = i;
while (OPTIONS[n].name != group_names[i])
++n;
if (OPTIONS[n].name != std::string("OPTIONS"))
s += OPTIONS[n].name + std::string("\n") + std::string(std::strlen(OPTIONS[n].name), '^') + "\n\n";
while (n < OPTIONS.size()) {
if (OPTIONS[n].name == group_names[i]) {
for (size_t o = 0; o < OPTIONS[n].size(); ++o)
s += format_option (OPTIONS[n][o]);
}
++n;
}
}
s += "Standard options\n^^^^^^^^^^^^^^^^\n\n";
for (size_t i = 0; i < __standard_options.size(); ++i)
s += format_option (__standard_options[i]);
s += std::string ("References\n^^^^^^^^^^\n\n");
for (size_t i = 0; i < REFERENCES.size(); ++i) {
auto refs = split_lines (REFERENCES[i], false);
s += refs[0];
for (size_t n = 1; n < refs.size(); ++n)
s += " |br|\n " + refs[n];
s += "\n\n";
}
s += std::string(MRTRIX_CORE_REFERENCE) + "\n\n";
s += std::string("--------------\n\n") +
"\n\n**Author:** " + (char*)AUTHOR
+ "\n\n**Copyright:** " + COPYRIGHT + "\n\n";
return s;
}
const Option* match_option (const char* arg)
{
if (consume_dash (arg) && *arg && !isdigit (*arg) && *arg != '.') {
while (consume_dash(arg));
vector<const Option*> candidates;
std::string root (arg);
for (size_t i = 0; i < OPTIONS.size(); ++i)
get_matches (candidates, OPTIONS[i], root);
get_matches (candidates, __standard_options, root);
// no matches
if (candidates.size() == 0)
throw Exception (std::string ("unknown option \"-") + root + "\"");
// return match if unique:
if (candidates.size() == 1)
return candidates[0];
// return match if fully specified:
for (size_t i = 0; i < candidates.size(); ++i)
if (root == candidates[i]->id)
return candidates[i];
// check if there is only one *unique* candidate
const auto cid = candidates[0]->id;
if ( std::all_of(++candidates.begin(), candidates.end(), [& cid](const Option* cand){return cand->id == cid;}) )
return candidates[0];
// report something useful:
root = "several matches possible for option \"-" + root + "\": \"-" + candidates[0]->id;
for (size_t i = 1; i < candidates.size(); ++i)
root += std::string ("\", \"-") + candidates[i]->id + "\"";
throw Exception (root);
}
return nullptr;
}
void sort_arguments (int argc, const char* const* argv)
{
for (int n = 1; n < argc; ++n) {
if (argv[n]) {
const Option* opt = match_option (argv[n]);
if (opt) {
if (n + int (opt->size()) >= argc)
throw Exception (std::string ("not enough parameters to option \"-") + opt->id + "\"");
option.push_back (ParsedOption (opt, argv+n+1));
n += opt->size();
}
else
argument.push_back (ParsedArgument (nullptr, nullptr, argv[n]));
}