Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
8350a945
提交
8350a945
authored
4月 26, 2016
作者:
Jeromy Johnson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2519 from thomas-gardner/commands
commands/cli: fix parsing of incorrect permutations
上级
7fe81dbe
7279153a
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
40 行增加
和
28 行删除
+40
-28
cmd_suggestion.go
commands/cli/cmd_suggestion.go
+14
-0
parse.go
commands/cli/parse.go
+7
-9
parse_test.go
commands/cli/parse_test.go
+19
-14
root.go
core/commands/root.go
+0
-5
没有找到文件。
commands/cli/cmd_suggestion.go
浏览文件 @
8350a945
package
cli
import
(
"fmt"
"sort"
"strings"
...
...
@@ -69,3 +70,16 @@ func suggestUnknownCmd(args []string, root *cmds.Command) []string {
}
return
sFinal
}
func
printSuggestions
(
inputs
[]
string
,
root
*
cmds
.
Command
)
(
err
error
)
{
suggestions
:=
suggestUnknownCmd
(
inputs
,
root
)
if
len
(
suggestions
)
>
1
{
err
=
fmt
.
Errorf
(
"Unknown Command
\"
%s
\"\n\n
Did you mean any of these?
\n\n\t
%s"
,
inputs
[
0
],
strings
.
Join
(
suggestions
,
"
\n\t
"
))
}
else
if
len
(
suggestions
)
>
0
{
err
=
fmt
.
Errorf
(
"Unknown Command
\"
%s
\"\n\n
Did you mean this?
\n\n\t
%s"
,
inputs
[
0
],
suggestions
[
0
])
}
else
{
err
=
fmt
.
Errorf
(
"Unknown Command
\"
%s
\"\n
"
,
inputs
[
0
])
}
return
}
commands/cli/parse.go
浏览文件 @
8350a945
...
...
@@ -227,6 +227,11 @@ func parseOpts(args []string, root *cmds.Command) (
}
}
else
{
stringVals
=
append
(
stringVals
,
arg
)
if
len
(
path
)
==
0
{
// found a typo or early argument
err
=
printSuggestions
(
stringVals
,
root
)
return
}
}
}
}
...
...
@@ -268,15 +273,8 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
// 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
&&
len
(
inputs
)
>
len
(
argDefs
)
{
suggestions
:=
suggestUnknownCmd
(
inputs
,
root
)
if
len
(
suggestions
)
>
1
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Unknown Command
\"
%s
\"\n\n
Did you mean any of these?
\n\n\t
%s"
,
inputs
[
0
],
strings
.
Join
(
suggestions
,
"
\n\t
"
))
}
else
if
len
(
suggestions
)
>
0
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Unknown Command
\"
%s
\"\n\n
Did you mean this?
\n\n\t
%s"
,
inputs
[
0
],
suggestions
[
0
])
}
else
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Unknown Command
\"
%s
\"\n
"
,
inputs
[
0
])
}
err
:=
printSuggestions
(
inputs
,
root
)
return
nil
,
nil
,
err
}
stringArgs
:=
make
([]
string
,
0
,
numInputs
)
...
...
commands/cli/parse_test.go
浏览文件 @
8350a945
...
...
@@ -78,6 +78,9 @@ func TestOptionParsing(t *testing.T) {
}
testHelper
:=
func
(
args
string
,
expectedOpts
kvs
,
expectedWords
words
,
expectErr
bool
)
{
var
opts
map
[
string
]
interface
{}
var
input
[]
string
_
,
opts
,
input
,
_
,
err
:=
parseOpts
(
strings
.
Split
(
args
,
" "
),
cmd
)
if
expectErr
{
if
err
==
nil
{
...
...
@@ -99,9 +102,8 @@ func TestOptionParsing(t *testing.T) {
testHelper
(
args
,
expectedOpts
,
expectedWords
,
false
)
}
test
(
"-"
,
kvs
{},
words
{
"-"
})
test
(
"
test
-"
,
kvs
{},
words
{
"-"
})
testFail
(
"-b -b"
)
test
(
"beep boop"
,
kvs
{},
words
{
"beep"
,
"boop"
})
test
(
"test beep boop"
,
kvs
{},
words
{
"beep"
,
"boop"
})
testFail
(
"-s"
)
test
(
"-s foo"
,
kvs
{
"s"
:
"foo"
},
words
{})
...
...
@@ -110,26 +112,29 @@ func TestOptionParsing(t *testing.T) {
test
(
"-b"
,
kvs
{
"b"
:
true
},
words
{})
test
(
"-bs foo"
,
kvs
{
"b"
:
true
,
"s"
:
"foo"
},
words
{})
test
(
"-sb"
,
kvs
{
"s"
:
"b"
},
words
{})
test
(
"-b foo"
,
kvs
{
"b"
:
true
},
words
{
"foo"
})
test
(
"--bool foo"
,
kvs
{
"bool"
:
true
},
words
{
"foo"
})
test
(
"-b
test
foo"
,
kvs
{
"b"
:
true
},
words
{
"foo"
})
test
(
"--bool
test
foo"
,
kvs
{
"bool"
:
true
},
words
{
"foo"
})
testFail
(
"--bool=foo"
)
testFail
(
"--string"
)
test
(
"--string foo"
,
kvs
{
"string"
:
"foo"
},
words
{})
test
(
"--string=foo"
,
kvs
{
"string"
:
"foo"
},
words
{})
test
(
"-- -b"
,
kvs
{},
words
{
"-b"
})
test
(
"foo -b"
,
kvs
{
"b"
:
true
},
words
{
"foo"
})
test
(
"
test
foo -b"
,
kvs
{
"b"
:
true
},
words
{
"foo"
})
test
(
"-b=false"
,
kvs
{
"b"
:
false
},
words
{})
test
(
"-b=true"
,
kvs
{
"b"
:
true
},
words
{})
test
(
"-b=false foo"
,
kvs
{
"b"
:
false
},
words
{
"foo"
})
test
(
"-b=true foo"
,
kvs
{
"b"
:
true
},
words
{
"foo"
})
test
(
"--bool=true foo"
,
kvs
{
"bool"
:
true
},
words
{
"foo"
})
test
(
"--bool=false foo"
,
kvs
{
"bool"
:
false
},
words
{
"foo"
})
test
(
"-b=FaLsE foo"
,
kvs
{
"b"
:
false
},
words
{
"foo"
})
test
(
"-b=TrUe foo"
,
kvs
{
"b"
:
true
},
words
{
"foo"
})
test
(
"-b true"
,
kvs
{
"b"
:
true
},
words
{
"true"
})
test
(
"-b false"
,
kvs
{
"b"
:
true
},
words
{
"false"
})
test
(
"-b --string foo bar"
,
kvs
{
"b"
:
true
,
"string"
:
"foo"
},
words
{
"bar"
})
test
(
"-b=false test foo"
,
kvs
{
"b"
:
false
},
words
{
"foo"
})
test
(
"-b=true test foo"
,
kvs
{
"b"
:
true
},
words
{
"foo"
})
test
(
"--bool=true test foo"
,
kvs
{
"bool"
:
true
},
words
{
"foo"
})
test
(
"--bool=false test foo"
,
kvs
{
"bool"
:
false
},
words
{
"foo"
})
test
(
"-b test true"
,
kvs
{
"b"
:
true
},
words
{
"true"
})
test
(
"-b test false"
,
kvs
{
"b"
:
true
},
words
{
"false"
})
test
(
"-b=FaLsE test foo"
,
kvs
{
"b"
:
false
},
words
{
"foo"
})
test
(
"-b=TrUe test foo"
,
kvs
{
"b"
:
true
},
words
{
"foo"
})
test
(
"-b test true"
,
kvs
{
"b"
:
true
},
words
{
"true"
})
test
(
"-b test false"
,
kvs
{
"b"
:
true
},
words
{
"false"
})
test
(
"-b --string foo test bar"
,
kvs
{
"b"
:
true
,
"string"
:
"foo"
},
words
{
"bar"
})
test
(
"-b=false --string bar"
,
kvs
{
"b"
:
false
,
"string"
:
"bar"
},
words
{})
testFail
(
"foo test"
)
}
func
TestArgumentParsing
(
t
*
testing
.
T
)
{
...
...
core/commands/root.go
浏览文件 @
8350a945
...
...
@@ -13,11 +13,6 @@ import (
var
log
=
logging
.
Logger
(
"core/commands"
)
type
TestOutput
struct
{
Foo
string
Bar
int
}
const
(
ApiOption
=
"api"
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论