-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefinition.go
More file actions
111 lines (88 loc) · 3.09 KB
/
definition.go
File metadata and controls
111 lines (88 loc) · 3.09 KB
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
107
108
109
110
111
package console
import (
"fmt"
"github.com/eidolon/console/parameters"
"github.com/eidolon/console/specification"
)
// Definition represents the collected and configured input parameters of an application.
type Definition struct {
// Defined arguments for the current application run.
arguments map[string]parameters.Argument
argumentKeys []string
// Defined options for the current application run.
options map[string]parameters.Option
optionSet []parameters.Option
}
// NewDefinition creates a new Definition with sensible defaults.
func NewDefinition() *Definition {
definition := Definition{}
definition.arguments = make(map[string]parameters.Argument)
definition.options = make(map[string]parameters.Option)
return &definition
}
// ArgumentDefinition is a struct that represents the entire configuration of a CLI argument.
type ArgumentDefinition struct {
// The value to reference.
Value parameters.Value
// The specification of the argument.
Spec string
// The description of the argument.
Desc string
}
// OptionDefinition is a struct that represents the entire configuration of a CLI option.
type OptionDefinition struct {
// The value to reference.
Value parameters.Value
// The specification of the option.
Spec string
// The description of the option.
Desc string
// The name of an environment variable to read an option value from.
EnvVar string
}
// Arguments gets all of the arguments in this Definition.
func (d *Definition) Arguments() []parameters.Argument {
var arguments []parameters.Argument
for _, key := range d.argumentKeys {
arguments = append(arguments, d.arguments[key])
}
return arguments
}
// Options gets all of the options in this Definition.
func (d *Definition) Options() []parameters.Option {
return d.optionSet
}
// AddArgument creates a parameters.Argument and adds it to the Definition. Duplicate argument names
// will result in an error.
func (d *Definition) AddArgument(definition ArgumentDefinition) {
arg, err := specification.ParseArgumentSpecification(definition.Spec)
if err != nil {
panic(fmt.Errorf("console: Error parsing argument specification: '%s'", err.Error()))
}
arg.Description = definition.Desc
arg.Value = definition.Value
if _, ok := d.arguments[arg.Name]; ok {
panic(fmt.Errorf("console: Cannot redeclare argument with name '%s'", arg.Name))
}
d.arguments[arg.Name] = arg
d.argumentKeys = append(d.argumentKeys, arg.Name)
}
// AddOption creates a parameters.Option and adds it to the Definition. Duplicate option names will
// result in an error.
func (d *Definition) AddOption(definition OptionDefinition) {
opt, err := specification.ParseOptionSpecification(definition.Spec)
if err != nil {
panic(fmt.Errorf("console: Error parsing option specification: '%s'", err.Error()))
}
opt.Description = definition.Desc
opt.EnvVar = definition.EnvVar
opt.Value = definition.Value
for _, name := range opt.Names {
if _, ok := d.options[name]; ok {
fmt.Println(definition)
panic(fmt.Errorf("console: Cannot redeclare option with name '%s'", name))
}
d.options[name] = opt
}
d.optionSet = append(d.optionSet, opt)
}