Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
anbox
提交
f5ec3d13
A
anbox
项目概览
openeuler
/
anbox
通知
24
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
anbox
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f5ec3d13
编写于
2月 06, 2017
作者:
S
Simon Fels
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add BoolSwitchFlag class to model boolean input parameters
上级
d732c8da
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
68 addition
and
26 deletion
+68
-26
src/anbox/cli.cpp
src/anbox/cli.cpp
+4
-3
src/anbox/cli.h
src/anbox/cli.h
+64
-23
未找到文件。
src/anbox/cli.cpp
浏览文件 @
f5ec3d13
...
...
@@ -44,9 +44,10 @@ static constexpr const char* option = " --%1% %2%";
void
add_to_desc_for_flags
(
po
::
options_description
&
desc
,
const
std
::
set
<
cli
::
Flag
::
Ptr
>&
flags
)
{
for
(
auto
flag
:
flags
)
{
auto
v
=
po
::
value
<
std
::
string
>
()
->
notifier
(
[
flag
](
const
std
::
string
&
s
)
{
flag
->
notify
(
s
);
});
desc
.
add_options
()(
flag
->
name
().
as_string
().
c_str
(),
v
,
po
::
value_semantic
*
spec
=
nullptr
;
flag
->
specify_option
(
spec
);
if
(
!
spec
)
continue
;
desc
.
add_options
()(
flag
->
name
().
as_string
().
c_str
(),
spec
,
flag
->
description
().
as_string
().
c_str
());
}
}
...
...
src/anbox/cli.h
浏览文件 @
f5ec3d13
/*
/*
* Copyright (C) 2016 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
...
...
@@ -31,6 +31,8 @@
#include "anbox/do_not_copy_or_move.h"
#include "anbox/optional.h"
#include <boost/program_options.hpp>
namespace
anbox
{
namespace
cli
{
...
...
@@ -76,16 +78,19 @@ typedef SizeConstrainedString<80> Description;
/// @brief Flag models an input parameter to a command.
class
Flag
:
public
DoNotCopyOrMove
{
public:
typedef
boost
::
program_options
::
value_semantic
*
Specification
;
// Safe us some typing.
typedef
std
::
shared_ptr
<
Flag
>
Ptr
;
/// @brief notify announces a new value to the flag.
virtual
void
notify
(
const
std
::
string
&
value
)
=
0
;
/// @brief name returns the name of the Flag.
const
Name
&
name
()
const
;
/// @brief description returns a human-readable description of the flag.
const
Description
&
description
()
const
;
/// @brief specify the program option of the flag.
virtual
void
specify_option
(
Specification
&
spec
)
=
0
;
protected:
/// @brief Flag creates a new instance, initializing name and description
/// from the given values.
...
...
@@ -115,12 +120,15 @@ class TypedFlag : public Flag {
/// @brief value returns the optional value associated with the flag.
const
Optional
<
T
>&
value
()
const
{
return
value_
;
}
/// @brief notify tries to unwrap a value of type T from value.
void
notify
(
const
std
::
string
&
s
)
override
{
std
::
stringstream
ss
{
s
};
T
value
;
ss
>>
value
;
value_
=
value
;
/// @brief Option generated by specify_option tries to unwrap a value
/// of type T from value.
void
specify_option
(
Flag
::
Specification
&
spec
)
override
{
spec
=
boost
::
program_options
::
value
<
std
::
string
>
()
->
notifier
([
&
](
const
std
::
string
&
s
)
{
std
::
stringstream
ss
{
s
};
T
value
;
ss
>>
value
;
value_
=
value
;
});
}
private:
...
...
@@ -141,11 +149,15 @@ class TypedReferenceFlag : public Flag {
TypedReferenceFlag
(
const
Name
&
name
,
const
Description
&
description
,
T
&
value
)
:
Flag
{
name
,
description
},
value_
{
value
}
{}
/// @brief notify tries to unwrap a value of type T from value,
/// relying on operator>> to read from given string s.
void
notify
(
const
std
::
string
&
s
)
override
{
std
::
stringstream
ss
{
s
};
ss
>>
value_
.
get
();
/// @brief Option generated by specify_option tries to unwrap a value of type T
/// from value, relying on operator>> to read from given string s.
void
specify_option
(
Flag
::
Specification
&
spec
)
override
{
spec
=
boost
::
program_options
::
value
<
std
::
string
>
()
->
notifier
([
&
](
const
std
::
string
&
s
)
{
std
::
stringstream
ss
{
s
};
T
value
;
ss
>>
value
;
value_
=
value
;
});
}
private:
...
...
@@ -153,9 +165,8 @@ class TypedReferenceFlag : public Flag {
};
/// @brief OptionalTypedReferenceFlag handles Optional<T> references, making
/// sure that
/// a value is always read on notify, even if the Optional<T> wasn't initialized
/// previously.
/// sure that a value is always read on notify, even if the Optional<T> wasn't
/// initialized previously.
template
<
typename
T
>
class
OptionalTypedReferenceFlag
:
public
Flag
{
public:
...
...
@@ -165,18 +176,40 @@ class OptionalTypedReferenceFlag : public Flag {
Optional
<
T
>&
value
)
:
Flag
{
name
,
description
},
value_
{
value
}
{}
/// @brief notify tries to unwrap a value of type T from value.
void
notify
(
const
std
::
string
&
s
)
override
{
std
::
stringstream
ss
{
s
};
T
value
;
ss
>>
value
;
value_
.
get
()
=
value
;
/// @brief Option generated by specify_option tries to unwrap a value of
/// type T from value.
void
specify_option
(
Flag
::
Specification
&
spec
)
override
{
spec
=
boost
::
program_options
::
value
<
std
::
string
>
()
->
notifier
([
&
](
const
std
::
string
&
s
)
{
std
::
stringstream
ss
{
s
};
T
value
;
ss
>>
value
;
value_
.
get
()
=
value
;
});
}
private:
std
::
reference_wrapper
<
Optional
<
T
>>
value_
;
};
/// @brief BoolSwitchFlag implements Flag, updating the given mutable reference
/// to a boolean value.
class
BoolSwitchFlag
:
public
Flag
{
public:
typedef
std
::
shared_ptr
<
BoolSwitchFlag
>
Ptr
;
BoolSwitchFlag
(
const
Name
&
name
,
const
Description
&
description
,
bool
&
value
)
:
Flag
{
name
,
description
},
value_
(
value
)
{}
/// @brief Option generated by specify_option tries to unwrap a boolean
/// value from value.
void
specify_option
(
Flag
::
Specification
&
spec
)
override
{
spec
=
boost
::
program_options
::
bool_switch
(
&
value_
.
get
());
}
private:
std
::
reference_wrapper
<
bool
>
value_
;
};
/// @brief Command abstracts an individual command available from the daemon.
class
Command
:
public
DoNotCopyOrMove
{
public:
...
...
@@ -337,6 +370,14 @@ typename OptionalTypedReferenceFlag<T>::Ptr make_flag(const Name& name,
return
std
::
make_shared
<
OptionalTypedReferenceFlag
<
T
>>
(
name
,
desc
,
value
);
}
/// @brief make_flag returns a flag with the given name and description,
/// updating the given boolean value.
inline
BoolSwitchFlag
::
Ptr
make_flag
(
const
Name
&
name
,
const
Description
&
desc
,
bool
&
value
)
{
return
std
::
make_shared
<
BoolSwitchFlag
>
(
name
,
desc
,
value
);
}
}
// namespace cli
}
// namespace anbox
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录