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
f059748d
Commit
f059748d
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: Removed parser string handling since the go runtime handles it for us automatically
parent
e3307cce
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
3 additions
and
35 deletions
+3
-35
cli/parse.go
cli/parse.go
+2
-34
cli/parse_test.go
cli/parse_test.go
+1
-1
No files found.
cli/parse.go
View file @
f059748d
...
...
@@ -21,8 +21,7 @@ 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
// parsePath gets the command path from the command line input
func
parsePath
(
input
[]
string
,
root
*
commands
.
Command
)
([]
string
,
[]
string
,
error
)
{
cmd
:=
root
i
:=
0
...
...
@@ -43,7 +42,7 @@ func parsePath(input []string, root *commands.Command) ([]string, []string, erro
return
input
[
:
i
],
input
[
i
:
],
nil
}
//
o
ptions parses the raw string values of the given options
//
parseO
ptions parses the raw string values of the given options
// returns the parsed options as strings, along with the CLI args
func
parseOptions
(
input
,
path
[]
string
,
root
*
commands
.
Command
)
(
map
[
string
]
string
,
[]
string
,
error
)
{
options
,
err
:=
root
.
GetOptions
(
path
)
...
...
@@ -69,37 +68,6 @@ func parseOptions(input, path []string, root *commands.Command) (map[string]stri
value
=
split
[
1
]
}
if
strings
.
Contains
(
name
,
"-"
)
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Invalid option blob: '%s' (Shouldn't contain '-')"
,
input
[
i
])
}
if
value
!=
""
&&
strings
.
Contains
(
value
,
"
\"
"
)
{
// TODO: ignore escaped quotations (--foo="\"")
if
!
strings
.
HasPrefix
(
value
,
"
\"
"
)
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Invalid option blob: '%s' (Quotation wasn't at the start of value)"
,
input
[
i
])
}
value
=
value
[
1
:
]
for
{
if
strings
.
HasSuffix
(
value
,
"
\"
"
)
{
value
=
value
[
:
len
(
value
)
-
1
]
break
}
i
++
if
i
>=
len
(
input
)
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Unterminated string: '%s'"
,
value
)
}
value
+=
" "
+
input
[
i
]
}
if
strings
.
Contains
(
value
,
"
\"
"
)
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Invalid option blob: '%s' (Value contains unescaped quotation)"
,
value
)
}
}
opts
[
name
]
=
value
}
else
if
strings
.
HasPrefix
(
blob
,
"-"
)
{
...
...
cli/parse_test.go
View file @
f059748d
...
...
@@ -15,7 +15,7 @@ func TestOptionParsing(t *testing.T) {
}
cmd
.
Register
(
"test"
,
&
commands
.
Command
{})
opts
,
input
,
err
:=
parseOptions
([]
string
{
"--beep"
,
"--boop=
\"
5"
,
"
lol
\"
"
,
"test2"
,
"-cVb"
,
"beep"
},
opts
,
input
,
err
:=
parseOptions
([]
string
{
"--beep"
,
"--boop=
5
lol"
,
"test2"
,
"-cVb"
,
"beep"
},
[]
string
{
"test"
},
cmd
)
/*for k, v := range opts {
fmt.Printf("%s: %s\n", k, v)
...
...
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