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
5b18844c
Commit
5b18844c
authored
Oct 09, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Oct 20, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands: Check for option name collisions
parent
dd682963
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
1 deletion
+27
-1
commands/command.go
commands/command.go
+27
-1
No files found.
commands/command.go
View file @
5b18844c
...
...
@@ -19,7 +19,13 @@ func (c *Command) Register(id string, sub *Command) error {
c
.
subcommands
=
make
(
map
[
string
]
*
Command
)
}
// TODO: check for duplicate option names
// check for duplicate option names (only checks downwards)
names
:=
make
(
map
[
string
]
bool
)
c
.
checkOptions
(
names
)
err
:=
sub
.
checkOptions
(
names
)
if
err
!=
nil
{
return
err
}
if
_
,
ok
:=
c
.
subcommands
[
id
];
ok
{
return
fmt
.
Errorf
(
"There is already a subcommand registered with id '%s'"
,
id
)
...
...
@@ -83,3 +89,23 @@ func (c *Command) Call(path []string, req *Request) (interface{}, error) {
func
(
c
*
Command
)
Sub
(
id
string
)
*
Command
{
return
c
.
subcommands
[
id
]
}
func
(
c
*
Command
)
checkOptions
(
names
map
[
string
]
bool
)
error
{
for
_
,
opt
:=
range
c
.
Options
{
for
_
,
name
:=
range
opt
.
Names
{
if
_
,
ok
:=
names
[
name
];
ok
{
return
fmt
.
Errorf
(
"Multiple options are using the same name ('%s')"
,
name
)
}
names
[
name
]
=
true
}
}
for
_
,
cmd
:=
range
c
.
subcommands
{
err
:=
cmd
.
checkOptions
(
names
)
if
err
!=
nil
{
return
err
}
}
return
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