Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
116041c5
提交
116041c5
authored
11月 04, 2014
作者:
Matt Bell
提交者:
Juan Batiz-Benet
11月 14, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commands: Fixed argument value/definition mapping
上级
d180832f
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
85 行增加
和
19 行删除
+85
-19
parse.go
commands/cli/parse.go
+42
-9
command.go
commands/command.go
+19
-5
parse.go
commands/http/parse.go
+24
-5
没有找到文件。
commands/cli/parse.go
浏览文件 @
116041c5
...
...
@@ -117,25 +117,58 @@ func parseOptions(input []string) (map[string]interface{}, []string, error) {
}
func
parseArgs
(
stringArgs
[]
string
,
cmd
*
cmds
.
Command
)
([]
interface
{},
error
)
{
var
argDef
cmds
.
Argument
args
:=
make
([]
interface
{},
len
(
stringArgs
))
args
:=
make
([]
interface
{},
0
)
for
i
,
arg
:=
range
stringArgs
{
if
i
<
len
(
cmd
.
Arguments
)
{
argDef
=
cmd
.
Arguments
[
i
]
// count required argument definitions
lenRequired
:=
0
for
_
,
argDef
:=
range
cmd
.
Arguments
{
if
argDef
.
Required
{
lenRequired
++
}
}
if
argDef
.
Type
==
cmds
.
ArgString
{
args
[
i
]
=
arg
j
:=
0
for
_
,
argDef
:=
range
cmd
.
Arguments
{
// skip optional argument definitions if there aren't sufficient remaining values
if
len
(
stringArgs
)
-
j
<=
lenRequired
&&
!
argDef
.
Required
{
continue
}
if
j
>=
len
(
stringArgs
)
{
break
}
if
argDef
.
Variadic
{
for
_
,
arg
:=
range
stringArgs
[
j
:
]
{
var
err
error
args
,
err
=
appendArg
(
args
,
argDef
,
arg
)
if
err
!=
nil
{
return
nil
,
err
}
}
}
else
{
in
,
err
:=
os
.
Open
(
arg
)
var
err
error
args
,
err
=
appendArg
(
args
,
argDef
,
stringArgs
[
j
])
if
err
!=
nil
{
return
nil
,
err
}
args
[
i
]
=
in
}
j
++
}
return
args
,
nil
}
func
appendArg
(
args
[]
interface
{},
argDef
cmds
.
Argument
,
value
string
)
([]
interface
{},
error
)
{
if
argDef
.
Type
==
cmds
.
ArgString
{
return
append
(
args
,
value
),
nil
}
else
{
in
,
err
:=
os
.
Open
(
value
)
if
err
!=
nil
{
return
nil
,
err
}
return
append
(
args
,
in
),
nil
}
}
commands/command.go
浏览文件 @
116041c5
...
...
@@ -149,13 +149,27 @@ func (c *Command) CheckArguments(req Request) error {
return
fmt
.
Errorf
(
"Expected %v arguments, got %v"
,
len
(
argDefs
),
len
(
args
))
}
// count required argument definitions
lenRequired
:=
0
for
_
,
argDef
:=
range
c
.
Arguments
{
if
argDef
.
Required
{
lenRequired
++
}
}
// iterate over the arg definitions
for
i
,
argDef
:=
range
c
.
Arguments
{
j
:=
0
for
_
,
argDef
:=
range
c
.
Arguments
{
// skip optional argument definitions if there aren't sufficient remaining values
if
len
(
args
)
-
j
<=
lenRequired
&&
!
argDef
.
Required
{
continue
}
// the value for this argument definition. can be nil if it wasn't provided by the caller
var
v
interface
{}
if
i
<
len
(
args
)
{
v
=
args
[
i
]
if
j
<
len
(
args
)
{
v
=
args
[
j
]
j
++
}
err
:=
checkArgValue
(
v
,
argDef
)
...
...
@@ -164,8 +178,8 @@ func (c *Command) CheckArguments(req Request) error {
}
// any additional values are for the variadic arg definition
if
argDef
.
Variadic
&&
i
<
len
(
args
)
-
1
{
for
_
,
val
:=
range
args
[
i
+
1
:
]
{
if
argDef
.
Variadic
&&
j
<
len
(
args
)
-
1
{
for
_
,
val
:=
range
args
[
j
+
1
:
]
{
err
:=
checkArgValue
(
val
,
argDef
)
if
err
!=
nil
{
return
err
...
...
commands/http/parse.go
浏览文件 @
116041c5
...
...
@@ -39,13 +39,32 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
opts
,
stringArgs2
:=
parseOptions
(
r
)
stringArgs
=
append
(
stringArgs
,
stringArgs2
...
)
// Note that the argument handling here is dumb, it does not do any error-checking.
// (Arguments are further processed when the request is passed to the command to run)
args
:=
make
([]
interface
{},
0
)
for
_
,
arg
:=
range
cmd
.
Arguments
{
if
arg
.
Type
==
cmds
.
ArgString
{
if
arg
.
Variadic
{
// count required argument definitions
lenRequired
:=
0
for
_
,
argDef
:=
range
cmd
.
Arguments
{
if
argDef
.
Required
{
lenRequired
++
}
}
// count the number of provided argument values
valCount
:=
len
(
stringArgs
)
// TODO: add total number of parts in request body (instead of just 1 if body is present)
if
r
.
Body
!=
nil
{
valCount
+=
1
}
for
_
,
argDef
:=
range
cmd
.
Arguments
{
// skip optional argument definitions if there aren't sufficient remaining values
if
valCount
<=
lenRequired
&&
!
argDef
.
Required
{
continue
}
valCount
--
if
argDef
.
Type
==
cmds
.
ArgString
{
if
argDef
.
Variadic
{
for
_
,
s
:=
range
stringArgs
{
args
=
append
(
args
,
s
)
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论