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
1b356158
Commit
1b356158
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: Made Command#GetOption method, for getting all options for a given command path
parent
f437230d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
24 deletions
+35
-24
commands/command.go
commands/command.go
+35
-24
No files found.
commands/command.go
View file @
1b356158
...
...
@@ -38,35 +38,17 @@ func (c *Command) Register(id string, sub *Command) error {
// Call invokes the command at the given subcommand path
func
(
c
*
Command
)
Call
(
path
[]
string
,
req
*
Request
)
*
Response
{
options
:=
make
([]
Option
,
len
(
c
.
Options
))
copy
(
options
,
c
.
Options
)
options
=
append
(
options
,
globalOptions
...
)
cmd
:=
c
res
:=
&
Response
{
req
:
req
}
if
path
!=
nil
{
for
i
,
id
:=
range
path
{
cmd
=
c
.
Sub
(
id
)
if
cmd
==
nil
{
pathS
:=
strings
.
Join
(
path
[
0
:
i
],
"/"
)
res
.
SetError
(
fmt
.
Errorf
(
"Undefined command: '%s'"
,
pathS
),
Client
)
return
res
}
options
=
append
(
options
,
cmd
.
Options
...
)
}
}
optionsMap
:=
make
(
map
[
string
]
Option
)
for
_
,
opt
:=
range
options
{
for
_
,
name
:=
range
opt
.
Names
{
optionsMap
[
name
]
=
opt
}
}
options
,
err
:=
cmd
.
GetOptions
(
path
)
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
return
res
}
for
k
,
v
:=
range
req
.
options
{
opt
,
ok
:=
options
Map
[
k
]
opt
,
ok
:=
options
[
k
]
if
!
ok
{
res
.
SetError
(
fmt
.
Errorf
(
"Unrecognized command option: '%s'"
,
k
),
Client
)
...
...
@@ -94,6 +76,35 @@ func (c *Command) Call(path []string, req *Request) *Response {
return
res
}
// GetOptions gets the options in the given path of commands
func
(
c
*
Command
)
GetOptions
(
path
[]
string
)
(
map
[
string
]
Option
,
error
)
{
options
:=
make
([]
Option
,
len
(
c
.
Options
))
copy
(
options
,
c
.
Options
)
options
=
append
(
options
,
globalOptions
...
)
if
path
!=
nil
{
for
i
,
id
:=
range
path
{
cmd
:=
c
.
Sub
(
id
)
if
cmd
==
nil
{
pathS
:=
strings
.
Join
(
path
[
0
:
i
],
"/"
)
return
nil
,
fmt
.
Errorf
(
"Undefined command: '%s'"
,
pathS
)
}
options
=
append
(
options
,
cmd
.
Options
...
)
}
}
optionsMap
:=
make
(
map
[
string
]
Option
)
for
_
,
opt
:=
range
options
{
for
_
,
name
:=
range
opt
.
Names
{
optionsMap
[
name
]
=
opt
}
}
return
optionsMap
,
nil
}
// Sub returns the subcommand with the given id
func
(
c
*
Command
)
Sub
(
id
string
)
*
Command
{
return
c
.
subcommands
[
id
]
...
...
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