Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
ae929557
提交
ae929557
authored
5月 18, 2015
作者:
Jeromy Johnson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1238 from ipfs/improve-stdin-parsing
Improve stdin parsing
上级
88be96bc
275ec7c2
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
71 行增加
和
18 行删除
+71
-18
parse.go
commands/cli/parse.go
+22
-13
parse_test.go
commands/cli/parse_test.go
+49
-5
没有找到文件。
commands/cli/parse.go
浏览文件 @
ae929557
...
...
@@ -228,8 +228,8 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
// if we have more arg values provided than argument definitions,
// and the last arg definition is not variadic (or there are no definitions), return an error
notVariadic
:=
len
(
argDefs
)
==
0
||
!
argDefs
[
len
(
argDefs
)
-
1
]
.
Variadic
if
notVariadic
&&
numInputs
>
len
(
argDefs
)
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Expected %v arguments, got %v: %v"
,
len
(
argDefs
),
numInputs
,
inputs
)
if
notVariadic
&&
len
(
inputs
)
>
len
(
argDefs
)
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Expected %v arguments, got %v: %v"
,
len
(
argDefs
),
len
(
inputs
)
,
inputs
)
}
stringArgs
:=
make
([]
string
,
0
,
numInputs
)
...
...
@@ -250,29 +250,38 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
var
err
error
if
argDef
.
Type
==
cmds
.
ArgString
{
if
stdin
==
nil
{
if
stdin
==
nil
||
!
argDef
.
SupportsStdin
{
// add string values
stringArgs
,
inputs
=
appendString
(
stringArgs
,
inputs
)
}
else
if
argDef
.
SupportsStdin
{
// if we have a stdin, read it in and use the data as a string value
stringArgs
,
stdin
,
err
=
appendStdinAsString
(
stringArgs
,
stdin
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
else
{
if
len
(
inputs
)
>
0
{
// don't use stdin if we have inputs
stdin
=
nil
}
else
{
// if we have a stdin, read it in and use the data as a string value
stringArgs
,
stdin
,
err
=
appendStdinAsString
(
stringArgs
,
stdin
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
}
}
}
else
if
argDef
.
Type
==
cmds
.
ArgFile
{
if
stdin
==
nil
{
if
stdin
==
nil
||
!
argDef
.
SupportsStdin
{
// treat stringArg values as file paths
fileArgs
,
inputs
,
err
=
appendFile
(
fileArgs
,
inputs
,
argDef
,
recursive
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
}
else
if
argDef
.
SupportsStdin
{
// if we have a stdin, create a file from it
fileArgs
,
stdin
=
appendStdinAsFile
(
fileArgs
,
stdin
)
}
else
{
if
len
(
inputs
)
>
0
{
// don't use stdin if we have inputs
stdin
=
nil
}
else
{
// if we have a stdin, create a file from it
fileArgs
,
stdin
=
appendStdinAsFile
(
fileArgs
,
stdin
)
}
}
}
...
...
commands/cli/parse_test.go
浏览文件 @
ae929557
...
...
@@ -155,6 +155,23 @@ func TestArgumentParsing(t *testing.T) {
commands
.
StringArg
(
"a"
,
true
,
true
,
"some arg"
)
.
EnableStdin
(),
},
},
"stdinenabled2args"
:
&
commands
.
Command
{
Arguments
:
[]
commands
.
Argument
{
commands
.
StringArg
(
"a"
,
true
,
false
,
"some arg"
),
commands
.
StringArg
(
"b"
,
true
,
true
,
"another arg"
)
.
EnableStdin
(),
},
},
"stdinenablednotvariadic"
:
&
commands
.
Command
{
Arguments
:
[]
commands
.
Argument
{
commands
.
StringArg
(
"a"
,
true
,
false
,
"some arg"
)
.
EnableStdin
(),
},
},
"stdinenablednotvariadic2args"
:
&
commands
.
Command
{
Arguments
:
[]
commands
.
Argument
{
commands
.
StringArg
(
"a"
,
true
,
false
,
"some arg"
),
commands
.
StringArg
(
"b"
,
true
,
false
,
"another arg"
)
.
EnableStdin
(),
},
},
},
}
...
...
@@ -166,10 +183,10 @@ func TestArgumentParsing(t *testing.T) {
}
req
,
_
,
_
,
err
:=
Parse
(
cmd
,
f
,
rootCmd
)
if
err
!=
nil
{
t
.
Errorf
(
"Command '%v' should have passed parsing
"
,
cmd
)
t
.
Errorf
(
"Command '%v' should have passed parsing
: %v"
,
cmd
,
err
)
}
if
!
sameWords
(
req
.
Arguments
(),
res
)
{
t
.
Errorf
(
"Arguments parsed from '%v' are
not '%v'"
,
cmd
,
res
)
t
.
Errorf
(
"Arguments parsed from '%v' are
'%v' instead of '%v'"
,
cmd
,
req
.
Arguments
()
,
res
)
}
}
...
...
@@ -220,8 +237,35 @@ func TestArgumentParsing(t *testing.T) {
test
([]
string
{
"stdinenabled"
,
"value1"
,
"value2"
},
nil
,
[]
string
{
"value1"
,
"value2"
})
fstdin
:=
fileToSimulateStdin
(
t
,
"stdin1"
)
test
([]
string
{
"stdinenabled"
},
fstdin
,
[]
string
{
"stdin1"
})
test
([]
string
{
"stdinenabled"
,
"value1"
},
fstdin
,
[]
string
{
"stdin1"
,
"value1"
})
test
([]
string
{
"stdinenabled"
,
"value1"
,
"value2"
},
fstdin
,
[]
string
{
"stdin1"
,
"value1"
,
"value2"
})
test
([]
string
{
"stdinenabled"
,
"value1"
},
fstdin
,
[]
string
{
"value1"
})
test
([]
string
{
"stdinenabled"
,
"value1"
,
"value2"
},
fstdin
,
[]
string
{
"value1"
,
"value2"
})
fstdin
=
fileToSimulateStdin
(
t
,
"stdin1
\n
stdin2"
)
test
([]
string
{
"stdinenabled"
},
fstdin
,
[]
string
{
"stdin1"
,
"stdin2"
})
fstdin
=
fileToSimulateStdin
(
t
,
"stdin1
\n
stdin2
\n
stdin3"
)
test
([]
string
{
"stdinenabled"
},
fstdin
,
[]
string
{
"stdin1"
,
"stdin2"
,
"stdin3"
})
test
([]
string
{
"stdinenabled2args"
,
"value1"
,
"value2"
},
nil
,
[]
string
{
"value1"
,
"value2"
})
fstdin
=
fileToSimulateStdin
(
t
,
"stdin1"
)
test
([]
string
{
"stdinenabled2args"
,
"value1"
},
fstdin
,
[]
string
{
"value1"
,
"stdin1"
})
test
([]
string
{
"stdinenabled2args"
,
"value1"
,
"value2"
},
fstdin
,
[]
string
{
"value1"
,
"value2"
})
test
([]
string
{
"stdinenabled2args"
,
"value1"
,
"value2"
,
"value3"
},
fstdin
,
[]
string
{
"value1"
,
"value2"
,
"value3"
})
fstdin
=
fileToSimulateStdin
(
t
,
"stdin1
\n
stdin2"
)
test
([]
string
{
"stdinenabled2args"
,
"value1"
},
fstdin
,
[]
string
{
"value1"
,
"stdin1"
,
"stdin2"
})
test
([]
string
{
"stdinenablednotvariadic"
,
"value1"
},
nil
,
[]
string
{
"value1"
})
fstdin
=
fileToSimulateStdin
(
t
,
"stdin1"
)
test
([]
string
{
"stdinenablednotvariadic"
},
fstdin
,
[]
string
{
"stdin1"
})
test
([]
string
{
"stdinenablednotvariadic"
,
"value1"
},
fstdin
,
[]
string
{
"value1"
})
test
([]
string
{
"stdinenablednotvariadic2args"
,
"value1"
,
"value2"
},
nil
,
[]
string
{
"value1"
,
"value2"
})
fstdin
=
fileToSimulateStdin
(
t
,
"stdin1"
)
test
([]
string
{
"stdinenablednotvariadic2args"
,
"value1"
},
fstdin
,
[]
string
{
"value1"
,
"stdin1"
})
test
([]
string
{
"stdinenablednotvariadic2args"
,
"value1"
,
"value2"
},
fstdin
,
[]
string
{
"value1"
,
"value2"
})
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论