Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
xflags
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Go
Application
xflags
Commits
f398af16
Verified
Commit
f398af16
authored
2 years ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix assign the correct value to the proxy
parent
af461a4e
No related branches found
No related tags found
No related merge requests found
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
README.md
+15
-13
15 additions, 13 deletions
README.md
command.go
+6
-0
6 additions, 0 deletions
command.go
execute_test.go
+3
-3
3 additions, 3 deletions
execute_test.go
mapping.go
+4
-0
4 additions, 0 deletions
mapping.go
mapping_test.go
+4
-3
4 additions, 3 deletions
mapping_test.go
tags.go
+1
-0
1 addition, 0 deletions
tags.go
with
33 additions
and
19 deletions
README.md
+
15
−
13
View file @
f398af16
...
...
@@ -57,16 +57,17 @@ type Definition struct {
The following tags are supported:
| Tag | Context | Description |
|---------------|---------|--------------------------------------------|
|
`short`
| Value | Short name of the flag. |
|
`long`
| Value | Long name of the flag. |
|
`description`
| Value | Description of the flag. |
|
`required`
| Value | Flag is required. |
|
`shadow`
| Value | Copy the value to the shadow structure. |
|
`command`
| Command | Flag is a command. |
|
`call`
| Command | Function to call when the command is used. |
|
`ignore`
| -/- | Property is ignored. |
| Tag | Context | Description |
|---------------|----------|------------------------------------------------|
|
`short`
| Value | Short name of the flag. |
|
`long`
| Value | Long name of the flag. |
|
`description`
| Value | Description of the flag. |
|
`required`
| Value | Flag is required. |
|
`proxy`
| Value | Copy the value to the proxy structure. |
|
`command`
| Command | Flag is a command. |
|
`call`
| Command | Function to call when the command is used. |
|
`ignore`
| -/- | Property is ignored. |
### Callbacks
...
...
@@ -148,9 +149,8 @@ setting.Execute()
### Proxy
The proxy structure is used to copy the values of the flags to the
proxy structure. The proxy structure is used to access the values of the
flags.
The proxy structure is used to copy the values of the flags to a other
structure.
The proxy structure must implement the
`Proxy`
interface.
...
...
@@ -174,6 +174,8 @@ func main() {
}
```
The path in the structure is defined by the tag
`proxy`
.
### Arguments
the free arguments can be fetched with the method
`Args()`
.
...
...
This diff is collapsed.
Click to expand it.
command.go
+
6
−
0
View file @
f398af16
...
...
@@ -12,6 +12,7 @@ type cmd[C any] struct {
name
string
flagSet
*
flag
.
FlagSet
tagMapping
map
[
string
]
string
proxyMapping
map
[
string
]
string
commands
[]
*
cmd
[
C
]
settings
*
Settings
[
C
]
valuePath
[]
string
...
...
@@ -60,6 +61,7 @@ func buildCommandStruct[C any](s *Settings[C], name, fkt string, errorHandling f
commands
:
[]
*
cmd
[
C
]{},
settings
:
s
,
tagMapping
:
map
[
string
]
string
{},
proxyMapping
:
map
[
string
]
string
{},
valuePath
:
path
,
functionName
:
fkt
,
}
...
...
@@ -148,6 +150,10 @@ func (c *cmd[C]) parseStruct(dta any) {
continue
}
if
m
[
tagProxy
]
!=
""
{
c
.
proxyMapping
[
v
.
Type
()
.
Field
(
i
)
.
Name
]
=
m
[
tagProxy
]
}
if
m
[
tagShort
]
!=
""
{
c
.
tagMapping
[
m
[
tagShort
]]
=
v
.
Type
()
.
Field
(
i
)
.
Name
}
...
...
This diff is collapsed.
Click to expand it.
execute_test.go
+
3
−
3
View file @
f398af16
...
...
@@ -12,12 +12,12 @@ import (
type
testExecutionStruct
struct
{
callbackCounter
int
`ignore:"true"`
// for tag
shadow
see TestFlagCopyTo
Shadow
Global1
bool
`short:"a" long:"global1" description:"Global 1"
shadow
:"ValGlobal1"`
// for tag
proxy
see TestFlagCopyTo
Proxy
Global1
bool
`short:"a" long:"global1" description:"Global 1"
proxy
:"ValGlobal1"`
Global2
bool
`short:"b" long:"global2" description:"Global 2"`
Command1
struct
{
Command1Flag1
bool
`short:"c" long:"command1flag1" description:"Command 1 Flag 1"`
Command1Flag2
bool
`short:"d" long:"command1flag2" description:"Command 1 Flag 2"
shadow
:"ValCommand1Flag2"`
Command1Flag2
bool
`short:"d" long:"command1flag2" description:"Command 1 Flag 2"
proxy
:"ValCommand1Flag2"`
Command2
struct
{
Command2Flag1
bool
`short:"e" long:"command2flag1" description:"Command 2 Flag 1"`
Command2Flag2
bool
`short:"f" long:"command2flag2" description:"Command 2 Flag 2"`
...
...
This diff is collapsed.
Click to expand it.
mapping.go
+
4
−
0
View file @
f398af16
...
...
@@ -53,6 +53,10 @@ func (s *Settings[C]) assignValues(c cmd[C]) {
s
.
errors
=
append
(
s
.
errors
,
err
)
}
if
c
.
proxyMapping
[
k
]
!=
""
{
p
=
c
.
proxyMapping
[
k
]
}
s
.
mapping
[
p
]
=
value
return
...
...
This diff is collapsed.
Click to expand it.
mapping_test.go
+
4
−
3
View file @
f398af16
...
...
@@ -21,7 +21,7 @@ type ConfigStruct6 struct {
ValSub
ConfigStruct6Sub1
}
func
TestFlagCopyTo
Shadow
(
t
*
testing
.
T
)
{
func
TestFlagCopyTo
Proxy
(
t
*
testing
.
T
)
{
c
:=
ConfigStruct6
{}
c
.
ValSub
.
Command3Flag1
=
true
...
...
@@ -40,8 +40,9 @@ func TestFlagCopyToShadow(t *testing.T) {
}
func
(
s
*
ConfigStruct6
)
Copy
(
m
map
[
string
]
any
)
{
pathfinder
.
SetValue
(
s
,
"ValGlobal1"
,
(
m
[
"Global1"
]))
pathfinder
.
SetValue
(
s
,
"ValCommand1Flag2"
,
(
m
[
"Command1.Command1Flag2"
]))
for
k
,
v
:=
range
m
{
pathfinder
.
SetValue
(
s
,
k
,
v
)
}
}
func
TestCopyable
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
tags.go
+
1
−
0
View file @
f398af16
...
...
@@ -15,6 +15,7 @@ const (
tagShort
=
"short"
tagLong
=
"long"
tagDescription
=
"description"
tagProxy
=
"proxy"
)
func
getTagMap
(
field
reflect
.
StructField
)
(
value
map
[
string
]
string
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment