Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
107409ce
提交
107409ce
authored
10月 09, 2015
作者:
ForrestWeston
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added ability to specify true/false for bool opts
License: MIT Signed-off-by:
ForrestWeston
<
Forrest.Weston@gmail.com
>
上级
bea47c9b
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
33 行增加
和
10 行删除
+33
-10
parse.go
commands/cli/parse.go
+20
-5
parse_test.go
commands/cli/parse_test.go
+13
-5
没有找到文件。
commands/cli/parse.go
浏览文件 @
107409ce
...
...
@@ -85,13 +85,28 @@ func parseOpts(args []string, root *cmds.Command) (
err
=
fmt
.
Errorf
(
"Unrecognized option '%s'"
,
name
)
return
false
,
err
}
// mustUse implies that you must use the argument given after the '='
// eg. -r=true means you must take true into consideration
// mustUse == true in the above case
// eg. ipfs -r <file> means disregard <file> since there is no '='
// mustUse == false in the above situation
//arg == nil implies the flag was specified without an argument
if
optDef
.
Type
()
==
cmds
.
Bool
{
if
mustUse
{
return
false
,
fmt
.
Errorf
(
"Option '%s' takes no arguments, but was passed '%s'"
,
name
,
*
arg
)
if
arg
==
nil
||
!
mustUse
{
opts
[
name
]
=
true
return
false
,
nil
}
argVal
:=
strings
.
ToLower
(
*
arg
)
switch
argVal
{
case
"true"
:
opts
[
name
]
=
true
return
true
,
nil
case
"false"
:
opts
[
name
]
=
false
return
true
,
nil
default
:
return
true
,
fmt
.
Errorf
(
"Option '%s' takes true/false arguments, but was passed '%s'"
,
name
,
argVal
)
}
opts
[
name
]
=
""
return
false
,
nil
}
else
{
if
arg
==
nil
{
return
true
,
fmt
.
Errorf
(
"Missing argument for option '%s'"
,
name
)
...
...
commands/cli/parse_test.go
浏览文件 @
107409ce
...
...
@@ -106,17 +106,25 @@ func TestOptionParsing(t *testing.T) {
test
(
"-s foo"
,
kvs
{
"s"
:
"foo"
},
words
{})
test
(
"-sfoo"
,
kvs
{
"s"
:
"foo"
},
words
{})
test
(
"-s=foo"
,
kvs
{
"s"
:
"foo"
},
words
{})
test
(
"-b"
,
kvs
{
"b"
:
""
},
words
{})
test
(
"-bs foo"
,
kvs
{
"b"
:
""
,
"s"
:
"foo"
},
words
{})
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"
:
""
},
words
{
"foo"
})
test
(
"--bool foo"
,
kvs
{
"bool"
:
""
},
words
{
"foo"
})
test
(
"-b foo"
,
kvs
{
"b"
:
true
},
words
{
"foo"
})
test
(
"--bool 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"
:
""
},
words
{
"foo"
})
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"
})
}
func
TestArgumentParsing
(
t
*
testing
.
T
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论