forked from symfony-cli/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.go
106 lines (90 loc) · 2.28 KB
/
args.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
102
103
104
105
106
/*
* Copyright (c) 2021-present Fabien Potencier <[email protected]>
*
* This file is part of Symfony CLI project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package console
type Args interface {
// Get returns the named argument, or else a blank string
Get(name string) string
// first returns the first argument, or else a blank string
first() string
// Tail returns the rest of the arguments (the last "array" one)
// or else an empty string slice
Tail() []string
// Len returns the length of the wrapped slice
Len() int
// Present checks if there are any arguments present
Present() bool
// Slice returns a copy of the internal slice
Slice() []string
}
type args struct {
values []string
command *Command
}
func (a *args) Get(name string) string {
if a.command == nil {
return ""
}
for i, arg := range a.command.Args {
if arg.Name != name || arg.Slice {
continue
}
if len(a.values) >= i+1 {
return a.values[i]
}
return arg.Default
}
return ""
}
func (a *args) first() string {
if len(a.values) > 0 {
return (a.values)[0]
}
return ""
}
func (a *args) Tail() []string {
if a.command != nil {
for i, arg := range a.command.Args {
if !arg.Slice {
continue
}
if len(a.values) >= i+1 {
ret := make([]string, len(a.values)-i)
copy(ret, a.values[i:])
return ret
}
break
}
} else if a.Len() > 1 {
ret := make([]string, a.Len()-1)
copy(ret, a.values[1:])
return ret
}
return []string{}
}
func (a *args) Len() int {
return len(a.values)
}
func (a *args) Present() bool {
return a.Len() != 0
}
func (a *args) Slice() []string {
ret := make([]string, len(a.values))
copy(ret, a.values)
return ret
}