Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
utils
Commits
f551f2b1
Commit
f551f2b1
authored
24 years ago
by
David Flitney
Browse files
Options
Downloads
Patches
Plain Diff
Initial revision
parents
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
options.cc
+202
-0
202 additions, 0 deletions
options.cc
with
202 additions
and
0 deletions
options.cc
0 → 100644
+
202
−
0
View file @
f551f2b1
#include
"options.h"
void
string_to_T
(
bool
&
b
,
const
string
&
s
)
{
if
(
s
==
"NO ARG"
)
b
=
!
b
;
else
b
=
(
s
==
"true"
);
}
void
string_to_T
(
string
&
d
,
const
string
&
s
)
{
d
=
s
;
}
void
string_to_T
(
int
&
i
,
const
string
&
s
)
{
i
=
atoi
(
s
.
c_str
());
}
void
string_to_T
(
float
&
v
,
const
string
&
s
)
{
v
=
atof
(
s
.
c_str
());
}
bool
BaseOption
::
matches
(
const
string
&
arg
)
{
string
::
size_type
pos
=
0
,
np
;
while
((
np
=
key_
.
find
(
","
,
pos
))
!=
string
::
npos
)
{
if
(
arg
==
key_
.
substr
(
pos
,
np
))
return
true
;
pos
=
np
+
1
;
}
if
(
arg
==
key_
.
substr
(
pos
,
string
::
npos
))
return
true
;
return
false
;
}
ostream
&
operator
<<
(
ostream
&
os
,
const
BaseOption
&
o
)
{
return
os
<<
"
\t
"
<<
o
.
key
()
<<
"
\t
"
<<
o
.
help_text
();
}
void
OptionParser
::
usage
()
{
cerr
<<
"Usage: "
<<
endl
<<
"
\t
"
<<
progname_
<<
" "
<<
example_
<<
endl
;
cerr
<<
endl
<<
"Compulsory arguments:"
<<
endl
;
for
(
Options
::
iterator
option
=
options_
.
begin
();
option
!=
options_
.
end
();
option
++
)
{
if
((
*
option
)
->
compulsory
())
cerr
<<
**
option
<<
endl
;
}
cerr
<<
endl
<<
"Optional arguments:"
<<
endl
;
for
(
Options
::
iterator
option
=
options_
.
begin
();
option
!=
options_
.
end
();
option
++
)
{
if
(
!
(
*
option
)
->
compulsory
())
cerr
<<
**
option
<<
endl
;
}
cerr
<<
endl
;
cerr
<<
endl
;
}
// BaseOption * OptionParser::operator[](const string& key)
// {
// OptionMap::iterator option = options_.find(key);
// if(option != options_.end())
// return option->second;
// else // An unknown option!
// return NULL;
// }
unsigned
int
OptionParser
::
parse_command_line
(
unsigned
int
argc
,
char
**
argv
)
{
unsigned
int
argcount
=
1
;
bool
usage_needed
=
false
;
while
(
argcount
<
argc
)
{
unsigned
int
nmatches
=
0
;
unsigned
int
increments
=
1
;
string
optstr
(
argv
[
argcount
]),
valstr
;
if
(
argcount
+
1
<
argc
)
valstr
=
string
(
argv
[
argcount
+
1
]);
else
valstr
=
string
(
"NO ARG"
);
if
(
optstr
[
0
]
!=
'-'
)
// No more parsable options
break
;
for
(
Options
::
iterator
option
=
options_
.
begin
();
option
!=
options_
.
end
();
option
++
)
{
if
((
*
option
)
->
matches
(
optstr
))
{
nmatches
++
;
if
((
*
option
)
->
required
())
{
if
(
valstr
!=
"NO ARG"
)
{
(
*
option
)
->
value
(
valstr
);
increments
=
2
;
}
else
{
cerr
<<
optstr
<<
" : Missing argument!"
<<
endl
;
usage_needed
=
true
;
}
}
else
if
((
*
option
)
->
optional
())
{
if
(
valstr
[
0
]
!=
'-'
)
{
(
*
option
)
->
value
(
valstr
);
increments
=
2
;
}
else
{
(
*
option
)
->
value
(
"NO ARG"
);
}
}
else
{
(
*
option
)
->
value
(
"NO ARG"
);
}
}
}
if
(
nmatches
==
0
)
{
cerr
<<
optstr
<<
" : Unknown option!"
<<
endl
;
usage_needed
=
true
;
}
argcount
+=
increments
;
}
// Now check that any compulsory options
// have been set
for
(
Options
::
iterator
option
=
options_
.
begin
();
option
!=
options_
.
end
();
option
++
)
{
static
bool
banner
=
true
;
if
((
*
option
)
->
compulsory
()
&&
(
*
option
)
->
unset
())
{
if
(
banner
)
{
cerr
<<
"***************************************************"
<<
endl
;
cerr
<<
"The following COMPULSORY options have not been set!"
<<
endl
;
banner
=
false
;
usage_needed
=
true
;
}
cerr
<<
**
option
<<
endl
;
}
}
if
(
usage_needed
)
{
cerr
<<
"***************************************************"
<<
endl
;
usage
();
exit
(
-
1
);
}
return
argcount
;
// User should process any remaining args
}
//
// And now for the test stub...
//
#if defined(TESTING)
Option
<
bool
>
verbose
(
string
(
"-V,--verbose"
),
false
,
string
(
"switch on diagnostic messages"
),
false
,
BaseOption
::
no_argument
);
Option
<
bool
>
help
(
string
(
"-h,--help"
),
false
,
string
(
"display this message"
),
false
,
BaseOption
::
no_argument
);
Option
<
float
>
dof
(
string
(
"-d,--dof"
),
100.0
,
string
(
"number of degrees of freedom"
),
true
,
BaseOption
::
requires_argument
);
Option
<
string
>
mask
(
string
(
"-m,--mask"
),
string
(
"mask"
),
string
(
"brain mask volume"
),
true
,
BaseOption
::
requires_argument
);
Option
<
string
>
resid
(
string
(
"-r,--res"
),
string
(
"res4d"
),
string
(
"4d `residual-of-fit' image"
),
true
,
BaseOption
::
requires_argument
);
int
main
(
unsigned
int
argc
,
char
**
argv
)
{
OptionParser
options
(
"options"
,
"-d <number> --mask <filename> --res <filename>"
);
options
.
add
(
&
verbose
);
options
.
add
(
&
help
);
options
.
add
(
&
dof
);
options
.
add
(
&
mask
);
options
.
add
(
&
resid
);
for
(
unsigned
int
a
=
options
.
parse_command_line
(
argc
,
argv
);
a
<
argc
;
a
++
)
cout
<<
argv
[
a
]
<<
endl
;
if
(
help
.
value
())
options
.
usage
();
if
(
verbose
.
value
())
{
cout
<<
"verbose = "
<<
verbose
.
value
()
<<
endl
;
cout
<<
"help = "
<<
help
.
value
()
<<
endl
;
cout
<<
"dof = "
<<
dof
.
value
()
<<
endl
;
cout
<<
"mask = "
<<
mask
.
value
()
<<
endl
;
cout
<<
"resid = "
<<
resid
.
value
()
<<
endl
;
}
}
#endif
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment