Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
84fa7bc4
提交
84fa7bc4
authored
10月 20, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
AddOptionNames func
上级
09d2277f
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
32 行增加
和
13 行删除
+32
-13
command.go
commands/command.go
+32
-13
没有找到文件。
commands/command.go
浏览文件 @
84fa7bc4
...
@@ -30,15 +30,11 @@ func (c *Command) Register(id string, sub *Command) error {
...
@@ -30,15 +30,11 @@ func (c *Command) Register(id string, sub *Command) error {
}
}
// check for duplicate option names (only checks downwards)
// check for duplicate option names (only checks downwards)
names
:=
make
(
map
[
string
]
bool
)
if
err
:=
checkOptionClashes
(
globalCommand
,
c
,
sub
);
err
!=
nil
{
globalCommand
.
checkOptions
(
names
)
c
.
checkOptions
(
names
)
err
:=
sub
.
checkOptions
(
names
)
if
err
!=
nil
{
return
err
return
err
}
}
if
_
,
ok
:=
c
.
subcommands
[
id
];
ok
{
if
_
,
found
:=
c
.
subcommands
[
id
];
found
{
return
fmt
.
Errorf
(
"There is already a subcommand registered with id '%s'"
,
id
)
return
fmt
.
Errorf
(
"There is already a subcommand registered with id '%s'"
,
id
)
}
}
...
@@ -137,19 +133,42 @@ func (c *Command) Sub(id string) *Command {
...
@@ -137,19 +133,42 @@ func (c *Command) Sub(id string) *Command {
return
c
.
subcommands
[
id
]
return
c
.
subcommands
[
id
]
}
}
func
(
c
*
Command
)
checkOptions
(
names
map
[
string
]
bool
)
error
{
// AddOptionNames returns a map of all command options names, and the command
// they belong to. Will error if names clash in the command hierarchy.
func
AddOptionNames
(
c
*
Command
,
names
map
[
string
]
*
Command
)
error
{
for
_
,
opt
:=
range
c
.
Options
{
for
_
,
opt
:=
range
c
.
Options
{
for
_
,
name
:=
range
opt
.
Names
{
for
_
,
name
:=
range
opt
.
Names
{
if
_
,
ok
:=
names
[
name
];
ok
{
if
c2
,
found
:=
names
[
name
];
found
{
return
fmt
.
Errorf
(
"Multiple options are using the same name ('%s')"
,
name
)
// option can be reused in same command, but more often than not
// the clash will be across commands so error out with that, as
// commands tell us where the problem is
errstr
:=
"Option name ('%s') used multiple times (%v, %v)"
return
fmt
.
Errorf
(
errstr
,
c2
,
c
)
}
}
names
[
name
]
=
true
// mark the name as in use
names
[
name
]
=
c
}
}
// for every subcommand, recurse
for
_
,
c2
:=
range
c
.
subcommands
{
if
err
:=
AddOptionNames
(
c2
,
names
);
err
!=
nil
{
return
err
}
}
}
}
for
_
,
cmd
:=
range
c
.
subcommands
{
return
nil
err
:=
cmd
.
checkOptions
(
names
)
}
if
err
!=
nil
{
// checkOptionClashes checks all command option names for clashes
func
checkOptionClashes
(
cmds
...*
Command
)
error
{
names
:=
map
[
string
]
*
Command
{}
for
_
,
c
:=
range
cmds
{
if
err
:=
AddOptionNames
(
c
,
names
);
err
!=
nil
{
return
err
return
err
}
}
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论