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
bf328181
Commit
bf328181
authored
Oct 13, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Oct 20, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands/cli: Added CLI option parsing
parent
43670971
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
92 additions
and
0 deletions
+92
-0
commands/cli/parse.go
commands/cli/parse.go
+92
-0
No files found.
commands/cli/parse.go
0 → 100644
View file @
bf328181
package
cli
import
(
"strings"
"fmt"
"github.com/jbenet/go-ipfs/commands"
)
func
Parse
(
input
[]
string
,
root
*
commands
.
Command
)
([]
string
,
[]
string
,
map
[
string
]
string
,
error
)
{
opts
,
input
,
err
:=
options
(
input
,
root
)
if
err
!=
nil
{
return
nil
,
nil
,
nil
,
err
}
return
nil
,
nil
,
opts
,
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
options
(
input
[]
string
,
root
*
commands
.
Command
)
(
map
[
string
]
string
,
[]
string
,
error
)
{
opts
:=
make
(
map
[
string
]
string
)
cleanInput
:=
make
([]
string
,
0
)
// TODO: error if one option is defined multiple times
for
i
:=
0
;
i
<
len
(
input
);
i
++
{
blob
:=
input
[
i
]
if
strings
.
HasPrefix
(
blob
,
"--"
)
{
name
:=
blob
[
2
:
]
value
:=
""
if
strings
.
Contains
(
name
,
"="
)
{
split
:=
strings
.
SplitN
(
name
,
"="
,
2
)
name
=
split
[
0
]
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
,
"-"
)
{
blob
=
blob
[
1
:
]
if
strings
.
ContainsAny
(
blob
,
"-=
\"
"
)
{
return
nil
,
nil
,
fmt
.
Errorf
(
"Invalid option blob: '%s'"
,
input
[
i
])
}
for
_
,
name
:=
range
blob
{
opts
[
string
(
name
)]
=
""
}
// TODO: interpret next blob as value if the last option isn't a bool
}
else
{
cleanInput
=
append
(
cleanInput
,
blob
)
}
}
return
opts
,
cleanInput
,
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