Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
bbf3a1f4
提交
bbf3a1f4
authored
11月 19, 2014
作者:
Matt Bell
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commands: Changed Option to an interface
上级
277ba261
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
37 行增加
和
30 行删除
+37
-30
helptext.go
commands/cli/helptext.go
+2
-2
command.go
commands/command.go
+2
-2
option.go
commands/option.go
+25
-11
request.go
commands/request.go
+8
-15
没有找到文件。
commands/cli/helptext.go
浏览文件 @
bbf3a1f4
...
...
@@ -239,7 +239,7 @@ func optionText(cmd ...*cmds.Command) []string {
lines
=
append
(
lines
,
""
)
}
names
:=
sortByLength
(
opt
.
Names
)
names
:=
sortByLength
(
opt
.
Names
()
)
if
len
(
names
)
>=
j
+
1
{
lines
[
i
]
+=
fmt
.
Sprintf
(
optionFlag
,
names
[
j
])
}
...
...
@@ -268,7 +268,7 @@ func optionText(cmd ...*cmds.Command) []string {
// add option descriptions to output
for
i
,
opt
:=
range
options
{
lines
[
i
]
+=
" - "
+
opt
.
Description
lines
[
i
]
+=
" - "
+
opt
.
Description
()
}
return
lines
...
...
commands/command.go
浏览文件 @
bbf3a1f4
...
...
@@ -167,7 +167,7 @@ func (c *Command) Get(path []string) (*Command, error) {
// GetOptions gets the options in the given path of commands
func
(
c
*
Command
)
GetOptions
(
path
[]
string
)
(
map
[
string
]
Option
,
error
)
{
options
:=
make
([]
Option
,
len
(
c
.
Options
))
options
:=
make
([]
Option
,
0
,
len
(
c
.
Options
))
cmds
,
err
:=
c
.
Resolve
(
path
)
if
err
!=
nil
{
...
...
@@ -181,7 +181,7 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) {
optionsMap
:=
make
(
map
[
string
]
Option
)
for
_
,
opt
:=
range
options
{
for
_
,
name
:=
range
opt
.
Names
{
for
_
,
name
:=
range
opt
.
Names
()
{
if
_
,
found
:=
optionsMap
[
name
];
found
{
return
nil
,
fmt
.
Errorf
(
"Option name '%s' used multiple times"
,
name
)
}
...
...
commands/option.go
浏览文件 @
bbf3a1f4
...
...
@@ -17,14 +17,28 @@ const (
)
// Option is used to specify a field that will be provided by a consumer
type
Option
struct
{
Names
[]
string
// a list of unique names to
Type
reflect
.
Kind
// value must be this type
Description
string
// a short string to describe this option
type
Option
interface
{
Names
()
[]
string
// a list of unique names matched with user-provided flags
Type
()
reflect
.
Kind
// value must be this type
Description
()
string
// a short string that describes this option
}
type
option
struct
{
names
[]
string
kind
reflect
.
Kind
description
string
}
func
(
o
*
option
)
Names
()
[]
string
{
return
o
.
names
}
func
(
o
*
option
)
Type
()
reflect
.
Kind
{
return
o
.
kind
}
// MAYBE_TODO: add more features(?):
//Default interface{} // the default value (ignored if `Required` is true)
//Required bool // whether or not the option must be provided
func
(
o
*
option
)
Description
()
string
{
return
o
.
description
}
// constructor helper functions
...
...
@@ -37,10 +51,10 @@ func NewOption(kind reflect.Kind, names ...string) Option {
desc
:=
names
[
len
(
names
)
-
1
]
names
=
names
[
:
len
(
names
)
-
1
]
return
O
ption
{
N
ames
:
names
,
Type
:
kind
,
D
escription
:
desc
,
return
&
o
ption
{
n
ames
:
names
,
kind
:
kind
,
d
escription
:
desc
,
}
}
...
...
commands/request.go
浏览文件 @
bbf3a1f4
...
...
@@ -92,13 +92,6 @@ func (r *request) Path() []string {
// Option returns the value of the option for given name.
func
(
r
*
request
)
Option
(
name
string
)
*
OptionValue
{
val
,
found
:=
r
.
options
[
name
]
if
found
{
return
&
OptionValue
{
val
,
found
}
}
// if a value isn't defined for that name, we will try to look it up by its aliases
// find the option with the specified name
option
,
found
:=
r
.
optionDefs
[
name
]
if
!
found
{
...
...
@@ -106,8 +99,8 @@ func (r *request) Option(name string) *OptionValue {
}
// try all the possible names, break if we find a value
for
_
,
n
:=
range
option
.
Names
{
val
,
found
=
r
.
options
[
n
]
for
_
,
n
:=
range
option
.
Names
()
{
val
,
found
:
=
r
.
options
[
n
]
if
found
{
return
&
OptionValue
{
val
,
found
}
}
...
...
@@ -135,7 +128,7 @@ func (r *request) SetOption(name string, val interface{}) {
}
// try all the possible names, if we already have a value then set over it
for
_
,
n
:=
range
option
.
Names
{
for
_
,
n
:=
range
option
.
Names
()
{
_
,
found
:=
r
.
options
[
n
]
if
found
{
r
.
options
[
n
]
=
val
...
...
@@ -222,9 +215,9 @@ func (r *request) ConvertOptions() error {
}
kind
:=
reflect
.
TypeOf
(
v
)
.
Kind
()
if
kind
!=
opt
.
Type
{
if
kind
!=
opt
.
Type
()
{
if
kind
==
String
{
convert
:=
converters
[
opt
.
Type
]
convert
:=
converters
[
opt
.
Type
()
]
str
,
ok
:=
v
.
(
string
)
if
!
ok
{
return
u
.
ErrCast
()
...
...
@@ -236,19 +229,19 @@ func (r *request) ConvertOptions() error {
value
=
"empty value"
}
return
fmt
.
Errorf
(
"Could not convert %s to type '%s' (for option '-%s')"
,
value
,
opt
.
Type
.
String
(),
k
)
value
,
opt
.
Type
()
.
String
(),
k
)
}
r
.
options
[
k
]
=
val
}
else
{
return
fmt
.
Errorf
(
"Option '%s' should be type '%s', but got type '%s'"
,
k
,
opt
.
Type
.
String
(),
kind
.
String
())
k
,
opt
.
Type
()
.
String
(),
kind
.
String
())
}
}
else
{
r
.
options
[
k
]
=
v
}
for
_
,
name
:=
range
opt
.
Names
{
for
_
,
name
:=
range
opt
.
Names
()
{
if
_
,
ok
:=
r
.
options
[
name
];
name
!=
k
&&
ok
{
return
fmt
.
Errorf
(
"Duplicate command options were provided ('%s' and '%s')"
,
k
,
name
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论