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
Commits
bb326331
Commit
bb326331
authored
Oct 14, 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 parsing to always get the command path at the beginning of the CLI input
parent
66b0727d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
25 deletions
+34
-25
commands/cli/parse.go
commands/cli/parse.go
+34
-25
No files found.
commands/cli/parse.go
View file @
bb326331
...
...
@@ -8,12 +8,12 @@ import (
)
func
Parse
(
input
[]
string
,
root
*
commands
.
Command
)
([]
string
,
[]
string
,
map
[
string
]
string
,
error
)
{
opts
,
input
,
err
:=
parse
Options
(
input
,
root
)
path
,
input
,
err
:=
parse
Path
(
input
,
root
)
if
err
!=
nil
{
return
nil
,
nil
,
nil
,
err
}
path
,
args
,
err
:=
parse
Path
(
input
,
root
)
opts
,
args
,
err
:=
parse
Options
(
input
,
path
,
root
)
if
err
!=
nil
{
return
nil
,
nil
,
nil
,
err
}
...
...
@@ -21,11 +21,38 @@ func Parse(input []string, root *commands.Command) ([]string, []string, map[stri
return
path
,
args
,
opts
,
nil
}
// path gets the command path from the command line input
func
parsePath
(
input
[]
string
,
root
*
commands
.
Command
)
([]
string
,
[]
string
,
error
)
{
cmd
:=
root
i
:=
0
for
_
,
blob
:=
range
input
{
if
strings
.
HasPrefix
(
blob
,
"-"
)
{
break
}
cmd
:=
cmd
.
Sub
(
blob
)
if
cmd
==
nil
{
break
}
i
++
}
return
input
[
:
i
],
input
[
i
:
],
nil
}
// options parses the raw string values of the given options
// returns the parsed options as strings, along with the CLI input minus option blobs
func
parseOptions
(
input
[]
string
,
root
*
commands
.
Command
)
(
map
[
string
]
string
,
[]
string
,
error
)
{
// returns the parsed options as strings, along with the CLI args
func
parseOptions
(
input
,
path
[]
string
,
root
*
commands
.
Command
)
(
map
[
string
]
string
,
[]
string
,
error
)
{
_
,
err
:=
root
.
GetOptions
(
path
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
opts
:=
make
(
map
[
string
]
string
)
cleanInput
:=
make
([]
string
,
0
)
args
:=
make
([]
string
,
0
)
// TODO: error if one option is defined multiple times
...
...
@@ -89,27 +116,9 @@ func parseOptions(input []string, root *commands.Command) (map[string]string, []
// TODO: interpret next blob as value if the last option isn't a bool
}
else
{
cleanInput
=
append
(
cleanInput
,
blob
)
}
}
return
opts
,
cleanInput
,
nil
}
// path takes the command line (without options) and splits it into the command path and arguments
func
parsePath
(
input
[]
string
,
root
*
commands
.
Command
)
([]
string
,
[]
string
,
error
)
{
cmd
:=
root
i
:=
0
for
_
,
blob
:=
range
input
{
cmd
:=
cmd
.
Sub
(
blob
)
if
cmd
==
nil
{
break
args
=
append
(
args
,
blob
)
}
i
++
}
return
input
[
:
i
],
input
[
i
:
]
,
nil
return
opts
,
args
,
nil
}
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