-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapparg.go
More file actions
161 lines (150 loc) · 3.92 KB
/
apparg.go
File metadata and controls
161 lines (150 loc) · 3.92 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2025, Peter Ohler, All rights reserved.
package slip
import (
"flag"
"fmt"
)
// AppArg are used to specify then variables that will be set from the command
// line arguments.
type AppArg struct {
// Flag is the command line flag such as 'v' which matches '-v' on the
// command line.
Flag string
// Doc is the documentation or description of the flag as it appears in
// the help display that is triggered a '-h' on the command line.
Doc string
// Default is the default SLIP object that will be assigned to the
// variable associated with the flag unless over-ridden by a command line
// option.
Default Object
// Type is the SLIP type to coerce the command line option value into.
Type string
// Var is the name of the variable to that is bound to the flag value.
Var string
strValue string
intValue int64
floatValue float64
boolValue bool
}
// SetFlag sets the command line flag option for the argument.
func (aa *AppArg) SetFlag(fs *flag.FlagSet, scope *Scope) {
switch aa.Type {
case "fixnum", "integer":
var dv int64
if num, ok := aa.Default.(Integer); ok {
dv = num.Int64()
}
fs.Int64Var(&aa.intValue, aa.Flag, dv, aa.Doc)
case "float", "short-float", "double-float", "single-float":
var dv float64
if num, ok := aa.Default.(Real); ok {
dv = num.RealValue()
}
fs.Float64Var(&aa.floatValue, aa.Flag, dv, aa.Doc)
case "boolean":
if aa.Default == nil {
fs.BoolVar(&aa.boolValue, aa.Flag, false, aa.Doc)
} else {
fs.BoolVar(&aa.boolValue, aa.Flag, true, aa.Doc)
}
case "eval-before":
fs.Func(aa.Flag, aa.Doc, func(str string) error {
code := ReadString(str, scope)
var value Object
for _, obj := range code {
value = obj.Eval(scope, 0)
}
if 0 < len(aa.Var) {
scope.Let(Symbol(aa.Var), value)
}
return nil
})
case "eval-after":
fs.Func(aa.Flag, aa.Doc, func(str string) error {
aa.strValue = string(append(append([]byte(aa.strValue), str...), '\n'))
return nil
})
default: // any or blank
switch td := aa.Default.(type) {
case nil:
fs.StringVar(&aa.strValue, aa.Flag, "", aa.Doc)
case String:
fs.StringVar(&aa.strValue, aa.Flag, string(td), aa.Doc)
case Octets:
fs.StringVar(&aa.strValue, aa.Flag, string(td), aa.Doc)
default:
fs.StringVar(&aa.strValue, aa.Flag, ObjectString(aa.Default), aa.Doc)
}
}
}
// UpdateScope updates the scope with the values in the app arg.
func (aa *AppArg) UpdateScope(scope *Scope) {
var (
value Object
skip bool
)
switch aa.Type {
case "fixnum", "integer":
value = Fixnum(aa.intValue)
case "float", "short-float", "double-float", "single-float":
value = DoubleFloat(aa.floatValue)
case "string":
if 0 < len(aa.strValue) {
value = String(aa.strValue)
}
case "symbol":
if 0 < len(aa.strValue) {
value = Symbol(aa.strValue)
}
case "octets":
if 0 < len(aa.strValue) {
value = Octets(aa.strValue)
}
case "boolean":
if aa.boolValue {
value = True
}
skip = true
case "eval-before":
// already handled
return
case "eval-after":
if 0 < len(aa.strValue) {
code := ReadString(aa.strValue, scope)
for _, obj := range code {
value = obj.Eval(scope, 0)
}
}
skip = true
default: // any or blank
if code := ReadString(aa.strValue, scope); 0 < len(code) {
value = code[0]
}
}
if !skip && 0 < len(aa.Type) && value != nil {
value = Coerce(value, Symbol(aa.Type))
}
if 0 < len(aa.Var) {
scope.Let(Symbol(aa.Var), value)
}
}
// DefaultReadable returns the readable string that reads into the default value.
func (aa *AppArg) DefaultReadable() (str string) {
switch td := aa.Default.(type) {
case nil:
str = "nil"
case Symbol:
str = fmt.Sprintf("slip.Symbol(%q)", string(td))
case String:
str = fmt.Sprintf("slip.String(%q)", string(td))
case Octets:
str = fmt.Sprintf("slip.Octets(%q)", string(td))
case boolean:
if td {
str = "t"
}
default:
str = fmt.Sprintf("%T(%s)", aa.Default, ObjectString(aa.Default))
}
return
}