-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcommandlinehelper.m
143 lines (117 loc) · 4.08 KB
/
commandlinehelper.m
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
function [varargout] = commandlinehelper(arg,varargin)
%Helper function to handle commandline options
%Einar Heiberg
persistent s %store options in persistent variable
%--- Define commandline options
c = [];
c(1).switch = 'StudiesFolder'; c(1).nargin = 1; c(1).default = ''; c(1).description = '<dir>: folder software reads input files (dicoms), each subfolder is one series';
c(2).switch = 'DICOMStorageFolder'; c(2).nargin = 1; c(2).default = ''; c(2).description = '<dir>: folder where output dicom files are stored';
c(3).switch = 'LogFolder'; c(3).nargin = 1; c(3).default = ''; c(3).description = '<dir>: folder where log-files are stored';
c(4).switch = 'NoQuit'; c(4).nargin = 0; c(4).default = false; c(4).description = ': Disable quit options';
c(5).switch = 'NoPopUp'; c(5).nargin = 0; c(5).default = false; c(5).description = ': Disable pop-up when starting';
c(6).switch = 'License'; c(6).nargin = 1; c(6).default = ''; c(6).description = '<file>: License file';
c(7).switch = 'TempStorageFolder'; c(7).nargin = 1; c(7).default = ''; c(7).description = '<dir>: folder for temporal file storage';
c(8).switch = 'UseDefaultStudiesFolder'; c(8).nargin = 0; c(8).default = false; c(8).description = '<dir>: Create studies folder if it not existing according to default preference settings';
c(9).switch = 'UseComputerAETitle'; c(9).nargin = 0; c(9).default = false; c(9).description = '<dir>: Use computer AETitle in case if in segpref was different one assigned';
switch arg
case 'parse'
%do nothing here, do later after switch clause
case 'getparameters'
%return parameters
varargout = cell(1,1);
varargout{1} = s;
return
case 'storetopref'
%store settings to prefstruct
storetopref(s);
return;
case 'reset'
%reset persistent variable
s = [];
for loop = 1:length(c)
s.(c(loop).switch) = c(loop).default;
end
otherwise
error('Invalid options to commandlinehelper');
end
%--- parse clause
if isempty(varargin)
return
end
disp('------------------------------');
disp('Parsing commandline options');
%Initialize s
s = [];
for loop = 1:length(c)
s.(c(loop).switch) = c(loop).default;
end
%Add -help
n = length(c)+1;
c(n).switch = 'help'; c(n).nargin = 0; c(n).default = '';
%--- parse options
parameters = varargin;
while ~isempty(parameters)
found = false;
thisparameter = parameters{1}; %first of parameters
if isequal(thisparameter,'-help')
displayhelp(c);
parameters = parameters(2:end);
else
%loop over to find
for loop = 1:length(c)
if isequal(['-' lower(c(loop).switch)],lower(thisparameter))
found = true;
numargs = c(loop).nargin;
option = c(loop).switch;
if numargs==1
arg = cleanstring(parameters{2}); %more than one argument not yet implemented
parameters = parameters(3:end);
disp(sprintf(' * %s=%s',c(loop).switch,arg)); %#ok<DSPS>
else
arg = true; %to indicate that it is found
parameters = parameters(2:end);
disp(sprintf(' * %s=true',c(loop).switch)); %#ok<DSPS>
end
end
end
if found
s.(option) = arg; %store it
else
%warn user
disp(sprintf(' * option %s not recognized, ignored.',thisparameter)); %#ok<DSPS>
parameters = parameters(2:end); %Remove parameters
end
end
end
disp('------------------------------');
%----------------------
function displayhelp(c)
%----------------------
%Displays helptext
disp(sprintf('Help information, version %s',changelog)); %#ok<DSPS>
disp(' ');
for loop = 1:(length(c)-1)
disp(sprintf('-%s %s',lower(c(loop).switch),c(loop).description)); %#ok<DSPS>
disp(' ');
end
%----------------------
function storetopref(s)
%----------------------
%Store settings to pref struct (DATA.Pref)
global DATA
if isempty(s)
return
end
f = fieldnames(s);
for loop = 1:length(f)
DATA.Pref.(f{loop}) = s.(f{loop});
end
%--------------------------------
function stri = cleanstring(stri)
%--------------------------------
%Cleans string from " signs
%Remove "
stri = stri(stri~='"');
%Remove '
c = '''';
stri = stri(stri~=c);