-
Notifications
You must be signed in to change notification settings - Fork 0
/
name_command.go
101 lines (75 loc) · 3.27 KB
/
name_command.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package common_types
import (
"errors"
"fmt"
"regexp"
"github.com/terryhay/dolly/tools/size"
)
// NameCommand - name type of command
type NameCommand string
const (
// NameCommandUndefined - default NameCommand value, nameless command has this name
NameCommandUndefined NameCommand = ""
// NameCommandHelpHException - exception name for 'h' command name
NameCommandHelpHException NameCommand = "h"
// NameCommandHelpDashHException - exception name for '-h' command name
NameCommandHelpDashHException NameCommand = "-h"
// NameCommandHelpDashHelpException - exception name for '--help' command name
NameCommandHelpDashHelpException NameCommand = "--help"
)
const (
// LenNameCommandMin - min len of NameCommand
LenNameCommandMin size.Width = 3
// LenNameCommandMax - max len of NameCommand
LenNameCommandMax size.Width = 12
)
// String implements Stringer interface
func (n NameCommand) String() string {
return string(n)
}
var (
// ErrNameCommandDashInFront - unexpected dash in front
ErrNameCommandDashInFront = fmt.Errorf("NameCommand: must not have dash in front; exceptions are '%s' and '%s' for help command", NameCommandHelpDashHException, NameCommandHelpDashHelpException)
// ErrNameCommandEmpty - empty string
ErrNameCommandEmpty = errors.New("NameCommand: is empty")
// ErrNameCommandHelpCommandTooShort - len is less than LenNameCommandMin
ErrNameCommandHelpCommandTooShort = errors.New("NameCommand: help command is too short; exception '-h'")
// ErrNameCommandTooLong - len is more than LenNameCommandMax
ErrNameCommandTooLong = errors.New("NameCommand: is too long")
// ErrNameCommandTooShort - len is less than LenNameCommandMin
ErrNameCommandTooShort = errors.New("NameCommand: is too short")
// ErrNameCommandUnexpectedChar - NameCommand has unexpected char
ErrNameCommandUnexpectedChar = errors.New("NameCommand: unexpected character")
)
var patternBlackListCommandChar = regexp.MustCompile(`[^a-zA-Z-_]`)
// IsValid checks if NameCommand doesn't have dash in front, isn't too short/long
func (n NameCommand) IsValid(thisIsHelpCommand bool) error {
const dash = "-"
switch {
case len(n) == 0:
return ErrNameCommandEmpty
case len(n) < LenNameCommandMin.Int():
if thisIsHelpCommand {
if n != NameCommandHelpDashHException && n != NameCommandHelpHException {
return fmt.Errorf("%w: '%s' has len='%d'; permissible len inteval is [%d; %d]",
ErrNameCommandHelpCommandTooShort, n, len(n), LenNameCommandMin, LenNameCommandMax)
}
return nil
}
return fmt.Errorf("%w: '%s' has len='%d'; permissible len inteval is [%d; %d]",
ErrNameCommandTooShort, n, len(n), LenNameCommandMin, LenNameCommandMax)
case len(n) > LenNameCommandMax.Int():
return fmt.Errorf("%w: '%s' has len='%d'; permissible len inteval is [%d; %d]",
ErrNameCommandTooLong, n, len(n), LenNameCommandMin, LenNameCommandMax)
case len(patternBlackListCommandChar.FindString(n.String())) > 0:
return fmt.Errorf("%w: '%s' has unexpected character '%s'",
ErrNameCommandUnexpectedChar, n, patternBlackListFlagChar.FindString(n.String()))
case string(n[0]) == dash:
if thisIsHelpCommand && (n == NameCommandHelpDashHException || n == NameCommandHelpDashHelpException) {
return nil
}
return fmt.Errorf("%w: '%s'", ErrNameCommandDashInFront, n)
default:
return nil
}
}