Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
3a9a62eb
提交
3a9a62eb
authored
11月 16, 2014
作者:
Matt Bell
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commands/cli: Open file paths when parsing and use in request.Files()
上级
132e7402
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
34 行增加
和
27 行删除
+34
-27
parse.go
commands/cli/parse.go
+34
-27
没有找到文件。
commands/cli/parse.go
浏览文件 @
3a9a62eb
...
...
@@ -15,11 +15,9 @@ var ErrInvalidSubcmd = errors.New("subcommand not found")
// Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object.
// Parse will search each root to find the one that best matches the requested subcommand.
func
Parse
(
input
[]
string
,
stdin
*
os
.
File
,
root
*
cmds
.
Command
)
(
cmds
.
Request
,
*
cmds
.
Command
,
[]
string
,
error
)
{
// use the root that matches the longest path (most accurately matches request)
path
,
input
,
cmd
:=
parsePath
(
input
,
root
)
opts
,
string
Arg
s
,
err
:=
parseOptions
(
input
)
opts
,
string
Val
s
,
err
:=
parseOptions
(
input
)
if
err
!=
nil
{
return
nil
,
cmd
,
path
,
err
}
...
...
@@ -28,7 +26,7 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
return
nil
,
nil
,
path
,
ErrInvalidSubcmd
}
args
,
err
:=
parseArgs
(
stringArg
s
,
stdin
,
cmd
.
Arguments
)
stringArgs
,
fileArgs
,
err
:=
parseArgs
(
stringVal
s
,
stdin
,
cmd
.
Arguments
)
if
err
!=
nil
{
return
nil
,
cmd
,
path
,
err
}
...
...
@@ -46,7 +44,9 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
}
}
req
,
err
:=
cmds
.
NewRequest
(
path
,
opts
,
args
,
cmd
,
optDefs
)
file
:=
&
cmds
.
SliceFile
{
""
,
fileArgs
}
req
,
err
:=
cmds
.
NewRequest
(
path
,
opts
,
stringArgs
,
file
,
cmd
,
optDefs
)
if
err
!=
nil
{
return
nil
,
cmd
,
path
,
err
}
...
...
@@ -120,12 +120,12 @@ func parseOptions(input []string) (map[string]interface{}, []string, error) {
return
opts
,
args
,
nil
}
func
parseArgs
(
stringArgs
[]
string
,
stdin
*
os
.
File
,
arguments
[]
cmds
.
Argument
)
([]
interface
{}
,
error
)
{
func
parseArgs
(
inputs
[]
string
,
stdin
*
os
.
File
,
arguments
[]
cmds
.
Argument
)
([]
interface
{},
[]
cmds
.
File
,
error
)
{
// check if stdin is coming from terminal or is being piped in
if
stdin
!=
nil
{
stat
,
err
:=
stdin
.
Stat
()
if
err
!=
nil
{
return
nil
,
err
return
nil
,
nil
,
err
}
// if stdin isn't a CharDevice, set it to nil
...
...
@@ -136,22 +136,24 @@ func parseArgs(stringArgs []string, stdin *os.File, arguments []cmds.Argument) (
}
// count required argument definitions
len
Required
:=
0
num
Required
:=
0
for
_
,
argDef
:=
range
arguments
{
if
argDef
.
Required
{
len
Required
++
num
Required
++
}
}
valCount
:=
len
(
stringArgs
)
// count number of values provided by user
numInputs
:=
len
(
inputs
)
if
stdin
!=
nil
{
valCount
+=
1
numInputs
+=
1
}
args
:=
make
([]
interface
{},
0
,
valCount
)
stringArgs
:=
make
([]
interface
{},
0
,
numInputs
)
fileArgs
:=
make
([]
cmds
.
File
,
0
,
numInputs
)
argDefIndex
:=
0
// the index of the current argument definition
for
i
:=
0
;
i
<
valCount
;
i
++
{
for
i
,
input
:=
range
inputs
{
// get the argument definiton (should be arguments[argDefIndex],
// but if argDefIndex > len(arguments) we use the last argument definition)
var
argDef
cmds
.
Argument
...
...
@@ -161,43 +163,48 @@ func parseArgs(stringArgs []string, stdin *os.File, arguments []cmds.Argument) (
argDef
=
arguments
[
len
(
arguments
)
-
1
]
}
// skip optional argument definitions if there aren't sufficient remaining
value
s
if
valCount
-
i
<=
len
Required
&&
!
argDef
.
Required
{
// skip optional argument definitions if there aren't sufficient remaining
input
s
if
numInputs
-
i
<=
num
Required
&&
!
argDef
.
Required
{
continue
}
else
if
argDef
.
Required
{
len
Required
--
num
Required
--
}
if
argDef
.
Type
==
cmds
.
ArgString
{
if
stdin
==
nil
{
// add string values
args
=
append
(
args
,
stringArgs
[
0
]
)
stringArgs
=
stringArg
s
[
1
:
]
stringArgs
=
append
(
stringArgs
,
input
)
inputs
=
input
s
[
1
:
]
}
else
if
argDef
.
SupportsStdin
{
// if we have a stdin, read it in and use the data as a string value
var
buf
bytes
.
Buffer
_
,
err
:=
buf
.
ReadFrom
(
stdin
)
if
err
!=
nil
{
return
nil
,
err
return
nil
,
nil
,
err
}
args
=
append
(
a
rgs
,
buf
.
String
())
stringArgs
=
append
(
stringA
rgs
,
buf
.
String
())
stdin
=
nil
}
}
else
if
argDef
.
Type
==
cmds
.
ArgFile
{
if
stdin
==
nil
{
// treat stringArg values as file paths
file
,
err
:=
os
.
Open
(
stringArgs
[
0
])
path
:=
input
inputs
=
inputs
[
1
:
]
file
,
err
:=
os
.
Open
(
path
)
if
err
!=
nil
{
return
nil
,
err
return
nil
,
nil
,
err
}
args
=
append
(
args
,
file
)
stringArgs
=
stringArgs
[
1
:
]
fileArg
:=
&
cmds
.
ReaderFile
{
path
,
file
}
fileArgs
=
append
(
fileArgs
,
fileArg
)
}
else
if
argDef
.
SupportsStdin
{
// if we have a stdin, use that as a reader
args
=
append
(
args
,
stdin
)
// if we have a stdin, create a file from it
fileArg
:=
&
cmds
.
ReaderFile
{
""
,
stdin
}
fileArgs
=
append
(
fileArgs
,
fileArg
)
stdin
=
nil
}
}
...
...
@@ -205,5 +212,5 @@ func parseArgs(stringArgs []string, stdin *os.File, arguments []cmds.Argument) (
argDefIndex
++
}
return
a
rgs
,
nil
return
stringArgs
,
fileA
rgs
,
nil
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论