Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Juicedata
JuiceFS
提交
9281087d
JuiceFS
项目概览
Juicedata
/
JuiceFS
通知
10
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
JuiceFS
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
9281087d
编写于
3月 25, 2021
作者:
D
Davies Liu
提交者:
GitHub
3月 25, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
re-order the arguments (#274)
上级
b65998e4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
130 addition
and
1 deletion
+130
-1
cmd/main.go
cmd/main.go
+69
-1
cmd/main_test.go
cmd/main_test.go
+61
-0
未找到文件。
cmd/main.go
浏览文件 @
9281087d
...
...
@@ -79,7 +79,7 @@ func main() {
}
}
err
:=
app
.
Run
(
os
.
Args
)
err
:=
app
.
Run
(
reorderOptions
(
app
,
os
.
Args
)
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
...
...
@@ -160,6 +160,74 @@ func stringContains(s []string, e string) bool {
return
false
}
func
isFlag
(
flags
[]
cli
.
Flag
,
option
string
)
(
bool
,
bool
)
{
if
!
strings
.
HasPrefix
(
option
,
"-"
)
{
return
false
,
false
}
// --V or -v work the same
option
=
strings
.
TrimLeft
(
option
,
"-"
)
for
_
,
flag
:=
range
flags
{
_
,
isBool
:=
flag
.
(
*
cli
.
BoolFlag
)
for
_
,
name
:=
range
flag
.
Names
()
{
if
option
==
name
||
strings
.
HasPrefix
(
option
,
name
+
"="
)
{
return
true
,
!
isBool
&&
!
strings
.
Contains
(
option
,
"="
)
}
}
}
return
false
,
false
}
func
reorderOptions
(
app
*
cli
.
App
,
args
[]
string
)
[]
string
{
var
newArgs
=
[]
string
{
args
[
0
]}
var
others
[]
string
globalFlags
:=
append
(
app
.
Flags
,
cli
.
VersionFlag
)
for
i
:=
1
;
i
<
len
(
args
);
i
++
{
option
:=
args
[
i
]
if
ok
,
hasValue
:=
isFlag
(
globalFlags
,
option
);
ok
{
newArgs
=
append
(
newArgs
,
option
)
if
hasValue
{
i
++
newArgs
=
append
(
newArgs
,
args
[
i
])
}
}
else
{
others
=
append
(
others
,
option
)
}
}
// no command
if
len
(
others
)
==
0
{
return
newArgs
}
cmdName
:=
others
[
0
]
var
cmd
*
cli
.
Command
for
_
,
c
:=
range
app
.
Commands
{
if
c
.
Name
==
cmdName
{
cmd
=
c
}
}
if
cmd
==
nil
{
// can't recognize the command, skip it
return
append
(
newArgs
,
others
...
)
}
newArgs
=
append
(
newArgs
,
cmdName
)
args
,
others
=
others
[
1
:
],
nil
// -h is valid for all the commands
cmdFlags
:=
append
(
cmd
.
Flags
,
cli
.
HelpFlag
)
for
i
:=
0
;
i
<
len
(
args
);
i
++
{
option
:=
args
[
i
]
if
ok
,
hasValue
:=
isFlag
(
cmdFlags
,
option
);
ok
{
newArgs
=
append
(
newArgs
,
option
)
if
hasValue
{
i
++
newArgs
=
append
(
newArgs
,
args
[
i
])
}
}
else
{
others
=
append
(
others
,
option
)
}
}
return
append
(
newArgs
,
others
...
)
}
func
setLoggerLevel
(
c
*
cli
.
Context
)
{
if
c
.
Bool
(
"trace"
)
{
utils
.
SetLogLevel
(
logrus
.
TraceLevel
)
...
...
cmd/main_test.go
0 → 100644
浏览文件 @
9281087d
/*
* JuiceFS, Copyright (C) 2020 Juicedata, Inc.
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package
main
import
(
"reflect"
"testing"
"github.com/urfave/cli/v2"
)
func
TestArgsOrder
(
t
*
testing
.
T
)
{
var
app
=
&
cli
.
App
{
Flags
:
[]
cli
.
Flag
{
&
cli
.
BoolFlag
{
Name
:
"verbose"
,
Aliases
:
[]
string
{
"v"
},
},
&
cli
.
Int64Flag
{
Name
:
"key"
,
Aliases
:
[]
string
{
"k"
},
},
},
Commands
:
[]
*
cli
.
Command
{
{
Name
:
"cmd"
,
Flags
:
[]
cli
.
Flag
{
&
cli
.
Int64Flag
{
Name
:
"k2"
,
},
},
},
},
}
var
cases
=
[][]
string
{
{
"test"
,
"cmd"
,
"a"
,
"-k2"
,
"v2"
,
"b"
,
"--v"
},
{
"test"
,
"--v"
,
"cmd"
,
"-k2"
,
"v2"
,
"a"
,
"b"
},
{
"test"
,
"cmd"
,
"a"
,
"-k2=v"
,
"--h"
},
{
"test"
,
"cmd"
,
"-k2=v"
,
"--h"
,
"a"
},
}
for
i
:=
0
;
i
<
len
(
cases
);
i
+=
2
{
oreded
:=
reorderOptions
(
app
,
cases
[
i
])
if
!
reflect
.
DeepEqual
(
cases
[
i
+
1
],
oreded
)
{
t
.
Fatalf
(
"expecte %v, but got %v"
,
cases
[
i
+
1
],
oreded
)
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录