-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_check_options.go
76 lines (67 loc) · 2.49 KB
/
pre_check_options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package cliz
import (
"github.com/hakadoriya/z.go/errorz"
"github.com/hakadoriya/z.go/mapz"
)
func (c *Command) preCheckOptions() error {
// NOTE: duplicate check
if err := c.preCheckDuplicateOptions(make(map[string]duplicateMetadata)); err != nil {
return errorz.Errorf("%s: %w", c.Name, err)
}
return nil
}
type duplicateMetadata struct {
cmdName string
optionFullName string
}
//nolint:cyclop
func (c *Command) preCheckDuplicateOptions(duplicateChecker map[string]duplicateMetadata) error {
for _, opt := range c.Options {
if _, ok := opt.(*HelpOption); ok {
// HelpOption is not a target of duplicate check
continue
}
if name := opt.GetName(); name != "" {
if key, alreadyExists := duplicateChecker[name]; alreadyExists {
return errorz.Errorf("option: %s(%s) and %s(%s%s): %w", key.cmdName, key.optionFullName, c.Name, longOptionPrefix, name, ErrDuplicateOption)
}
duplicateChecker[name] = duplicateMetadata{
cmdName: c.Name,
optionFullName: longOptionPrefix + name,
}
}
for _, alias := range opt.GetAliases() {
if alias != "" {
if key, alreadyExists := duplicateChecker[alias]; alreadyExists {
return errorz.Errorf("option: %s(%s) and %s(%s%s): %w", key.cmdName, key.optionFullName, c.Name, shortOptionPrefix, alias, ErrDuplicateOption)
}
duplicateChecker[alias] = duplicateMetadata{
cmdName: c.Name,
optionFullName: shortOptionPrefix + alias,
}
}
}
if env := opt.GetEnv(); env != "" {
const envPrefix = "env:"
if key, alreadyExists := duplicateChecker[env]; alreadyExists {
return errorz.Errorf("option: %s(%s) and %s(%s%s): %w", key.cmdName, key.optionFullName, c.Name, envPrefix, env, ErrDuplicateOption)
}
duplicateChecker[env] = duplicateMetadata{
cmdName: c.Name,
optionFullName: envPrefix + env,
}
}
}
for _, subcmd := range c.SubCommands {
// IMPORTANT: `copiedDuplicateChecker` is copied to avoid affecting the sibling subcommands.
// Ancestor command options and descendant command options must not be duplicated,
// but the sibling subcommands' options can be duplicated,
// because the sibling subcommands are not called at the same time,
// so they can coexist because sibling subcommands' options do not mix.
copiedDuplicateChecker := mapz.Copy(duplicateChecker)
if err := subcmd.preCheckDuplicateOptions(copiedDuplicateChecker); err != nil {
return errorz.Errorf("%s: %w", subcmd.Name, err)
}
}
return nil
}