Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
7ac22638
Commit
7ac22638
authored
Jun 19, 2023
by
Samir Halilcevic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable space separated value syntax for long argument names
parent
87d449a8
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
100 additions
and
50 deletions
+100
-50
libcaf_core/caf/config_option.hpp
libcaf_core/caf/config_option.hpp
+8
-31
libcaf_core/src/actor_system_config.cpp
libcaf_core/src/actor_system_config.cpp
+1
-3
libcaf_core/src/config_option.cpp
libcaf_core/src/config_option.cpp
+34
-0
libcaf_core/src/config_option_set.cpp
libcaf_core/src/config_option_set.cpp
+25
-12
libcaf_core/test/config_option.cpp
libcaf_core/test/config_option.cpp
+4
-4
libcaf_core/test/config_option_set.cpp
libcaf_core/test/config_option_set.cpp
+28
-0
No files found.
libcaf_core/caf/config_option.hpp
View file @
7ac22638
...
@@ -104,37 +104,14 @@ private:
...
@@ -104,37 +104,14 @@ private:
mutable
void
*
value_
;
mutable
void
*
value_
;
};
};
/// Finds `config_option` string with a matching long name in
(`first`, `last`],
/// Finds `config_option` string with a matching long name in
the string
///
where each entry is a pointer to a string. Returns a `ForwardIterator` to
///
argument list [`first`, `last`). Returns an `argument_iterator` to the
///
the match
and a `string_view` of the option value if the entry is
///
matching location
and a `string_view` of the option value if the entry is
/// found
and a `ForwardI
terator` to `last` with an empty `string_view`
/// found
, or a `arugment_i
terator` to `last` with an empty `string_view`
/// otherwise.
/// otherwise.
template
<
class
ForwardIterator
,
class
Sentinel
>
std
::
pair
<
std
::
vector
<
std
::
string
>::
const_iterator
,
std
::
string_view
>
std
::
pair
<
ForwardIterator
,
std
::
string_view
>
find_by_long_name
(
const
config_option
&
x
,
find_by_long_name
(
const
config_option
&
x
,
ForwardIterator
first
,
std
::
vector
<
std
::
string
>::
const_iterator
first
,
Sentinel
last
)
{
std
::
vector
<
std
::
string
>::
const_iterator
last
);
auto
long_name
=
x
.
long_name
();
for
(;
first
!=
last
;
++
first
)
{
std
::
string_view
str
{
*
first
};
// Make sure this is a long option starting with "--".
if
(
!
starts_with
(
str
,
"--"
))
continue
;
str
.
remove_prefix
(
2
);
// Skip optional "caf#" prefix.
if
(
starts_with
(
str
,
"caf#"
))
str
.
remove_prefix
(
4
);
// Make sure we are dealing with the right key.
if
(
!
starts_with
(
str
,
long_name
))
continue
;
// Make sure the key is followed by an assignment.
str
.
remove_prefix
(
long_name
.
size
());
if
(
!
starts_with
(
str
,
"="
))
continue
;
// Remove leading '=' and return the value.
str
.
remove_prefix
(
1
);
return
{
first
,
str
};
}
return
{
first
,
std
::
string_view
{}};
}
}
// namespace caf
}
// namespace caf
libcaf_core/src/actor_system_config.cpp
View file @
7ac22638
...
@@ -462,9 +462,7 @@ std::pair<error, std::string>
...
@@ -462,9 +462,7 @@ std::pair<error, std::string>
actor_system_config
::
extract_config_file_path
(
string_list
&
args
)
{
actor_system_config
::
extract_config_file_path
(
string_list
&
args
)
{
auto
ptr
=
custom_options_
.
qualified_name_lookup
(
"global.config-file"
);
auto
ptr
=
custom_options_
.
qualified_name_lookup
(
"global.config-file"
);
CAF_ASSERT
(
ptr
!=
nullptr
);
CAF_ASSERT
(
ptr
!=
nullptr
);
string_list
::
iterator
i
;
auto
[
i
,
path
]
=
find_by_long_name
(
*
ptr
,
args
.
begin
(),
args
.
end
());
std
::
string_view
path
;
std
::
tie
(
i
,
path
)
=
find_by_long_name
(
*
ptr
,
args
.
begin
(),
args
.
end
());
if
(
i
==
args
.
end
())
{
if
(
i
==
args
.
end
())
{
return
{
none
,
std
::
string
{}};
return
{
none
,
std
::
string
{}};
}
else
if
(
path
.
empty
())
{
}
else
if
(
path
.
empty
())
{
...
...
libcaf_core/src/config_option.cpp
View file @
7ac22638
...
@@ -133,4 +133,38 @@ std::string_view config_option::buf_slice(size_t from,
...
@@ -133,4 +133,38 @@ std::string_view config_option::buf_slice(size_t from,
return
{
buf_
.
get
()
+
from
,
to
-
from
};
return
{
buf_
.
get
()
+
from
,
to
-
from
};
}
}
// TODO: consider deprecating this
std
::
pair
<
std
::
vector
<
std
::
string
>::
const_iterator
,
std
::
string_view
>
find_by_long_name
(
const
config_option
&
x
,
std
::
vector
<
std
::
string
>::
const_iterator
first
,
std
::
vector
<
std
::
string
>::
const_iterator
last
)
{
auto
long_name
=
x
.
long_name
();
for
(;
first
!=
last
;
++
first
)
{
std
::
string_view
str
{
*
first
};
// Make sure this is a long option starting with "--".
if
(
!
starts_with
(
str
,
"--"
))
continue
;
str
.
remove_prefix
(
2
);
// Make sure we are dealing with the right key.
if
(
!
starts_with
(
str
,
long_name
))
continue
;
str
.
remove_prefix
(
long_name
.
size
());
// check for flag
if
(
x
.
is_flag
()
&&
str
.
empty
())
{
return
{
first
,
str
};
}
else
if
(
starts_with
(
str
,
"="
))
{
// Remove leading '=' and return the value.
str
.
remove_prefix
(
1
);
return
{
first
,
str
};
}
else
if
(
str
.
empty
()
&&
(
first
+
1
)
!=
last
)
{
// Get the next argument as value
++
first
;
return
{
first
,
std
::
string_view
{
*
first
}};
}
else
{
continue
;
}
}
return
{
first
,
std
::
string_view
{}};
}
}
// namespace caf
}
// namespace caf
libcaf_core/src/config_option_set.cpp
View file @
7ac22638
...
@@ -175,24 +175,37 @@ auto config_option_set::parse(settings& config, argument_iterator first,
...
@@ -175,24 +175,37 @@ auto config_option_set::parse(settings& config, argument_iterator first,
if
(
*
i
==
"--"
)
if
(
*
i
==
"--"
)
return
{
pec
::
success
,
std
::
next
(
first
)};
return
{
pec
::
success
,
std
::
next
(
first
)};
if
(
i
->
compare
(
0
,
2
,
"--"
)
==
0
)
{
if
(
i
->
compare
(
0
,
2
,
"--"
)
==
0
)
{
// Long options use the syntax "--<name>=<value>" and consume only a
// Long options cone in three variaties:
// single argument.
// syntax "--<name>=<value>" formated as a single single argument,
auto
npos
=
std
::
string
::
npos
;
// sytnax "--<name> <value>", comes as two arguments
// special case "--<name>", boolean flag
const
auto
npos
=
std
::
string
::
npos
;
auto
assign_op
=
i
->
find
(
'='
);
auto
assign_op
=
i
->
find
(
'='
);
auto
name
=
assign_op
==
npos
?
i
->
substr
(
2
)
auto
name
=
assign_op
==
npos
?
i
->
substr
(
2
)
:
i
->
substr
(
2
,
assign_op
-
2
);
:
i
->
substr
(
2
,
assign_op
-
2
);
auto
opt
=
cli_long_name_lookup
(
name
);
auto
opt
=
cli_long_name_lookup
(
name
);
if
(
opt
==
nullptr
)
if
(
opt
==
nullptr
)
return
{
pec
::
not_an_option
,
i
};
return
{
pec
::
not_an_option
,
i
};
auto
code
if
(
opt
->
is_flag
()
||
assign_op
!=
npos
)
{
=
consume
(
*
opt
,
auto
code
assign_op
==
npos
=
consume
(
*
opt
,
?
i
->
end
()
assign_op
==
npos
:
i
->
begin
()
+
static_cast
<
ptrdiff_t
>
(
assign_op
+
1
),
?
i
->
end
()
i
->
end
());
:
i
->
begin
()
+
static_cast
<
ptrdiff_t
>
(
assign_op
+
1
),
if
(
code
!=
pec
::
success
)
i
->
end
());
return
{
code
,
i
};
if
(
code
!=
pec
::
success
)
++
i
;
return
{
code
,
i
};
++
i
;
}
else
{
auto
j
=
std
::
next
(
i
);
if
(
j
==
last
)
{
return
{
pec
::
missing_argument
,
j
};
}
auto
code
=
consume
(
*
opt
,
j
->
begin
(),
j
->
end
());
if
(
code
!=
pec
::
success
)
return
{
code
,
i
};
std
::
advance
(
i
,
2
);
}
}
else
if
(
i
->
front
()
==
'-'
)
{
}
else
if
(
i
->
front
()
==
'-'
)
{
// Short options have three possibilities.
// Short options have three possibilities.
auto
opt
=
cli_short_name_lookup
((
*
i
)[
1
]);
auto
opt
=
cli_short_name_lookup
((
*
i
)[
1
]);
...
...
libcaf_core/test/config_option.cpp
View file @
7ac22638
...
@@ -352,20 +352,20 @@ CAF_TEST(find by long opt) {
...
@@ -352,20 +352,20 @@ CAF_TEST(find by long opt) {
};
};
// Well formed, find val2.
// Well formed, find val2.
check
({
"--foo=val1"
,
"--bar=val2"
,
"--baz=val3"
},
true
,
true
);
check
({
"--foo=val1"
,
"--bar=val2"
,
"--baz=val3"
},
true
,
true
);
check
({
"--foo=val1"
,
"--bar"
,
"val2"
,
"--baz=val3"
},
true
,
true
);
// Dashes missing, no match.
// Dashes missing, no match.
check
({
"--foo=val1"
,
"bar=val2"
,
"--baz=val3"
},
false
,
false
);
check
({
"--foo=val1"
,
"bar=val2"
,
"--baz=val3"
},
false
,
false
);
// Equal missing.
// Equal missing.
check
({
"--fooval1"
,
"--barval2"
,
"--bazval3"
},
false
,
false
);
check
({
"--fooval1"
,
"--barval2"
,
"--bazval3"
},
false
,
false
);
// Option value missing.
// Option value missing.
check
({
"--foo=val1"
,
"--bar="
,
"--baz=val3"
},
true
,
false
);
check
({
"--foo=val1"
,
"--bar="
,
"--baz=val3"
},
true
,
false
);
// With prefix 'caf#'.
check
({
"--caf#foo=val1"
,
"--caf#bar=val2"
,
"--caf#baz=val3"
},
true
,
true
);
// Option not included.
// Option not included.
check
({
"--foo=val1"
,
"--b4r=val2"
,
"--baz=val3"
},
false
,
false
);
check
({
"--foo=val1"
,
"--b4r=val2"
,
"--baz=val3"
},
false
,
false
);
// Option not included, with prefix.
check
({
"--caf#foo=val1"
,
"--caf#b4r=val2"
,
"--caf#baz=val3"
},
false
,
false
);
// No options to look through.
// No options to look through.
check
({},
false
,
false
);
check
({},
false
,
false
);
// flag option for booleans
needle
=
make_config_option
<
bool
>
(
"?foo"
sv
,
"bar,b"
sv
,
"test option"
sv
);
check
({
"--foo=val1"
,
"--bar"
,
"--baz=val3"
},
true
,
false
);
}
}
END_FIXTURE_SCOPE
()
END_FIXTURE_SCOPE
()
libcaf_core/test/config_option_set.cpp
View file @
7ac22638
...
@@ -109,9 +109,22 @@ CAF_TEST(parse with ref syncing) {
...
@@ -109,9 +109,22 @@ CAF_TEST(parse with ref syncing) {
CHECK_EQ
(
get_as
<
int
>
(
cfg
,
"foo.i"
),
42
);
CHECK_EQ
(
get_as
<
int
>
(
cfg
,
"foo.i"
),
42
);
}
}
CAF_TEST
(
Long
format
for
flags
)
{
auto
foo_b
=
false
;
opts
.
add
<
bool
>
(
foo_b
,
"foo"
,
"b,b"
,
""
);
settings
cfg
;
vector
<
string
>
args
{
"--foo.b"
};
auto
res
=
opts
.
parse
(
cfg
,
args
);
CHECK_EQ
(
res
.
first
,
pec
::
success
);
if
(
res
.
second
!=
args
.
end
())
CAF_FAIL
(
"parser stopped at: "
<<
*
res
.
second
);
CHECK_EQ
(
foo_b
,
true
);
}
CAF_TEST
(
string
parameters
)
{
CAF_TEST
(
string
parameters
)
{
opts
.
add
<
std
::
string
>
(
"value,v"
,
"some value"
);
opts
.
add
<
std
::
string
>
(
"value,v"
,
"some value"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--value=foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--value=foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--value"
,
"foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"-v"
,
"foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"-v"
,
"foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"-vfoobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"-vfoobar"
}),
"foobar"
);
}
}
...
@@ -121,8 +134,11 @@ CAF_TEST(flat CLI options) {
...
@@ -121,8 +134,11 @@ CAF_TEST(flat CLI options) {
opts
.
add
<
std
::
string
>
(
"?foo"
,
"bar,b"
,
"some value"
);
opts
.
add
<
std
::
string
>
(
"?foo"
,
"bar,b"
,
"some value"
);
CHECK
(
opts
.
begin
()
->
has_flat_cli_name
());
CHECK
(
opts
.
begin
()
->
has_flat_cli_name
());
CHECK_EQ
(
read
<
std
::
string
>
({
"-b"
,
"foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"-b"
,
"foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"-bfoobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--bar=foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--bar=foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--foo.bar=foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--foo.bar=foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--bar"
,
"foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--foo.bar"
,
"foobar"
}),
"foobar"
);
}
}
CAF_TEST
(
flat
CLI
parsing
with
nested
categories
)
{
CAF_TEST
(
flat
CLI
parsing
with
nested
categories
)
{
...
@@ -130,8 +146,11 @@ CAF_TEST(flat CLI parsing with nested categories) {
...
@@ -130,8 +146,11 @@ CAF_TEST(flat CLI parsing with nested categories) {
opts
.
add
<
std
::
string
>
(
"?foo.goo"
,
"bar,b"
,
"some value"
);
opts
.
add
<
std
::
string
>
(
"?foo.goo"
,
"bar,b"
,
"some value"
);
CHECK
(
opts
.
begin
()
->
has_flat_cli_name
());
CHECK
(
opts
.
begin
()
->
has_flat_cli_name
());
CHECK_EQ
(
read
<
std
::
string
>
({
"-b"
,
"foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"-b"
,
"foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"-bfoobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--bar=foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--bar=foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--foo.goo.bar=foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--foo.goo.bar=foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--bar"
,
"foobar"
}),
"foobar"
);
CHECK_EQ
(
read
<
std
::
string
>
({
"--foo.goo.bar"
,
"foobar"
}),
"foobar"
);
}
}
CAF_TEST
(
square
brackets
are
optional
on
the
command
line
)
{
CAF_TEST
(
square
brackets
are
optional
on
the
command
line
)
{
...
@@ -146,6 +165,15 @@ CAF_TEST(square brackets are optional on the command line) {
...
@@ -146,6 +165,15 @@ CAF_TEST(square brackets are optional on the command line) {
CHECK_EQ
(
read
<
int_list
>
({
"--value=1"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value=1"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value=1,2,3"
}),
int_list
({
1
,
2
,
3
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value=1,2,3"
}),
int_list
({
1
,
2
,
3
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value=1, 2 , 3 , "
}),
int_list
({
1
,
2
,
3
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value=1, 2 , 3 , "
}),
int_list
({
1
,
2
,
3
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value"
,
"[1]"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value"
,
"[1,]"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value"
,
"[ 1 , ]"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value"
,
"[1,2]"
}),
int_list
({
1
,
2
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value"
,
"[1, 2, 3]"
}),
int_list
({
1
,
2
,
3
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value"
,
"[1, 2, 3, ]"
}),
int_list
({
1
,
2
,
3
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value"
,
"1"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value"
,
"1,2,3"
}),
int_list
({
1
,
2
,
3
}));
CHECK_EQ
(
read
<
int_list
>
({
"--value"
,
"1, 2 , 3 , "
}),
int_list
({
1
,
2
,
3
}));
CHECK_EQ
(
read
<
int_list
>
({
"-v"
,
"[1]"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"-v"
,
"[1]"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"-v"
,
"[1,]"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"-v"
,
"[1,]"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"-v"
,
"[ 1 , ]"
}),
int_list
({
1
}));
CHECK_EQ
(
read
<
int_list
>
({
"-v"
,
"[ 1 , ]"
}),
int_list
({
1
}));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment