Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
9705b56b
Commit
9705b56b
authored
Oct 17, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Oct 20, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands/cli: Refactored CLI parsing to match go tooling conventions
parent
a0f96f48
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
33 deletions
+9
-33
cli/parse.go
cli/parse.go
+9
-33
No files found.
cli/parse.go
View file @
9705b56b
...
...
@@ -2,7 +2,6 @@ package cli
import
(
"strings"
"fmt"
"github.com/jbenet/go-ipfs/commands"
)
...
...
@@ -13,12 +12,7 @@ func Parse(input []string, root *commands.Command) (*commands.Request, error) {
return
nil
,
err
}
options
,
err
:=
root
.
GetOptions
(
path
)
if
err
!=
nil
{
return
nil
,
err
}
opts
,
args
,
err
:=
parseOptions
(
input
,
options
)
opts
,
args
,
err
:=
parseOptions
(
input
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -49,7 +43,7 @@ func parsePath(input []string, root *commands.Command) ([]string, []string, erro
// parseOptions parses the raw string values of the given options
// returns the parsed options as strings, along with the CLI args
func
parseOptions
(
input
[]
string
,
options
map
[
string
]
commands
.
Option
)
(
map
[
string
]
interface
{},
[]
string
,
error
)
{
func
parseOptions
(
input
[]
string
)
(
map
[
string
]
interface
{},
[]
string
,
error
)
{
opts
:=
make
(
map
[
string
]
interface
{})
args
:=
make
([]
string
,
0
)
...
...
@@ -58,10 +52,15 @@ func parseOptions(input []string, options map[string]commands.Option) (map[strin
for
i
:=
0
;
i
<
len
(
input
);
i
++
{
blob
:=
input
[
i
]
if
strings
.
HasPrefix
(
blob
,
"-
-
"
)
{
name
:=
blob
[
2
:
]
if
strings
.
HasPrefix
(
blob
,
"-"
)
{
name
:=
blob
[
1
:
]
value
:=
""
// support single and double dash
if
strings
.
HasPrefix
(
name
,
"-"
)
{
name
=
name
[
1
:
]
}
if
strings
.
Contains
(
name
,
"="
)
{
split
:=
strings
.
SplitN
(
name
,
"="
,
2
)
name
=
split
[
0
]
...
...
@@ -70,29 +69,6 @@ func parseOptions(input []string, options map[string]commands.Option) (map[strin
opts
[
name
]
=
value
}
else
if
strings
.
HasPrefix
(
blob
,
"-"
)
{
blob
=
blob
[
1
:
]
if
strings
.
ContainsAny
(
blob
,
"-=
\"
"
)
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Invalid option blob: '%s'"
,
input
[
i
])
}
nameS
:=
""
for
_
,
name
:=
range
blob
{
nameS
=
string
(
name
)
opts
[
nameS
]
=
""
}
if
nameS
!=
""
{
opt
,
ok
:=
options
[
nameS
]
if
ok
&&
opt
.
Type
!=
commands
.
Bool
{
i
++
if
i
<=
len
(
input
)
{
opts
[
nameS
]
=
input
[
i
]
}
}
}
}
else
{
args
=
append
(
args
,
blob
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment