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
c9a0899e
Verified
Commit
c9a0899e
authored
2 years ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
feat introduction of copy-interface
parent
60a6e795
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
api.go
+8
-2
8 additions, 2 deletions
api.go
api_test.go
+12
-5
12 additions, 5 deletions
api_test.go
flag.go
+0
-52
0 additions, 52 deletions
flag.go
shadow.go
+56
-1
56 additions, 1 deletion
shadow.go
shadow_test.go
+14
-0
14 additions, 0 deletions
shadow_test.go
with
90 additions
and
60 deletions
api.go
+
8
−
2
View file @
c9a0899e
...
...
@@ -13,12 +13,14 @@ import (
)
// ExecuteWithShadow executes the command line arguments and calls the functions.
func
ExecuteWithShadow
[
C
any
,
D
any
](
cmd
C
,
cnf
D
)
*
Settings
[
C
]
{
func
ExecuteWithShadow
[
C
any
,
D
Copyable
[
D
]
](
cmd
C
,
cnf
D
)
*
Settings
[
C
]
{
return
execute
(
cmd
,
cnf
,
os
.
Args
[
0
],
os
.
Args
[
1
:
])
}
type
noShadow
struct
{}
func
(
n
noShadow
)
Copy
(
a
noShadow
)
{}
// Execute executes the command line arguments and calls the functions.
func
Execute
[
C
any
](
cmd
C
)
*
Settings
[
C
]
{
return
execute
(
cmd
,
noShadow
{},
os
.
Args
[
0
],
os
.
Args
[
1
:
])
...
...
@@ -35,7 +37,7 @@ func (s *Settings[C]) GetFlagOutput() {
}
// execute is the internal implementation of ExecuteWithShadow.
func
execute
[
C
any
,
D
any
](
cmd
C
,
cnf
D
,
name
string
,
args
[]
string
)
*
Settings
[
C
]
{
func
execute
[
C
any
,
D
Copyable
[
D
]
](
cmd
C
,
cnf
D
,
name
string
,
args
[]
string
)
*
Settings
[
C
]
{
instance
:=
New
(
name
,
cmd
)
if
instance
.
HasErrors
()
{
return
instance
...
...
@@ -62,6 +64,10 @@ func execute[C any, D any](cmd C, cnf D, name string, args []string) *Settings[C
return
instance
}
if
instance
.
shadow
!=
nil
{
cnf
.
Copy
(
instance
.
shadow
.
(
D
))
}
instance
.
Execute
()
if
instance
.
HasErrors
()
{
return
instance
...
...
This diff is collapsed.
Click to expand it.
api_test.go
+
12
−
5
View file @
c9a0899e
...
...
@@ -21,22 +21,29 @@ func TestUsage(t *testing.T) {
assert
.
Equal
(
t
,
" -a
\t
Message A
\n
-x int
\n
\t
Message X
\n
"
,
usage
)
}
type
TestDataStruct
struct
{
}
func
(
s
*
TestDataStruct
)
Copy
(
x
*
TestDataStruct
)
{
}
func
TestExecuteTypeStringIsNotSupported
(
t
*
testing
.
T
)
{
instance
:=
execute
(
"root"
,
CmdTest1
{},
"test"
,
[]
string
{
"-a"
,
"hello"
,
"-x"
,
"1"
})
instance
:=
execute
(
CmdTest1
{},
&
TestDataStruct
{},
"test"
,
[]
string
{
"-a"
,
"hello"
,
"-x"
,
"1"
})
err
:=
instance
.
Errors
()
assert
.
True
(
t
,
instance
.
HasErrors
())
assert
.
Equal
(
t
,
1
,
len
(
err
))
e
:=
err
[
0
]
assert
.
Equal
(
t
,
"
type string is not supporte
d"
,
e
.
Error
())
assert
.
Equal
(
t
,
"
missing comman
d"
,
e
.
Error
())
}
func
TestExecuteHelp
(
t
*
testing
.
T
)
{
cnf
:=
struct
{
}{}
instance
:=
execute
(
CmdTest1
{},
&
cnf
,
"test"
,
[]
string
{
"-h"
})
instance
:=
execute
(
CmdTest1
{},
&
TestDataStruct
{},
"test"
,
[]
string
{
"-h"
})
assert
.
False
(
t
,
instance
.
HasErrors
())
}
...
...
This diff is collapsed.
Click to expand it.
flag.go
+
0
−
52
View file @
c9a0899e
...
...
@@ -2,55 +2,3 @@
// SPDX-License-Identifier: AGPL-3.0
package
xflags
import
(
"flag"
"strings"
)
func
(
s
*
Settings
[
C
])
assignValues
(
c
cmd
[
C
])
{
flgs
:=
c
.
flagSet
flgs
.
Visit
(
func
(
f
*
flag
.
Flag
)
{
name
:=
f
.
Name
value
:=
f
.
Value
.
String
()
k
,
ok
:=
c
.
tagMapping
[
name
]
if
!
ok
{
s
.
errors
=
append
(
s
.
errors
,
newUnknownFlagError
(
name
))
return
}
pa
:=
append
(
c
.
valuePath
,
k
)
p
:=
strings
.
Join
(
pa
,
"."
)
err
:=
setValueUsingPath
(
&
s
.
definitions
,
p
,
value
)
if
err
!=
nil
{
s
.
errors
=
append
(
s
.
errors
,
err
)
}
err
=
c
.
setShadowValue
(
s
.
shadow
,
k
,
value
)
if
err
!=
nil
{
s
.
errors
=
append
(
s
.
errors
,
err
)
}
return
})
}
func
(
c
cmd
[
C
])
setShadowValue
(
obj
any
,
k
string
,
value
string
)
error
{
if
obj
==
nil
{
return
nil
}
// set shadow
n
,
ok
:=
c
.
shadowMapping
[
k
]
if
!
ok
{
return
nil
}
return
setValueUsingPath
(
obj
,
n
,
value
)
}
This diff is collapsed.
Click to expand it.
shadow.go
+
56
−
1
View file @
c9a0899e
...
...
@@ -3,7 +3,11 @@
package
xflags
import
"reflect"
import
(
"flag"
"reflect"
"strings"
)
// SetShadow sets the shadow struct for the flag configuration.
func
(
s
*
Settings
[
C
])
SetShadow
(
shadow
any
)
*
Settings
[
C
]
{
...
...
@@ -21,3 +25,54 @@ func (s *Settings[C]) SetShadow(shadow any) *Settings[C] {
s
.
shadow
=
shadow
return
s
}
type
Copyable
[
C
any
]
interface
{
Copy
(
data
C
)
}
func
(
s
*
Settings
[
C
])
assignValues
(
c
cmd
[
C
])
{
flgs
:=
c
.
flagSet
flgs
.
Visit
(
func
(
f
*
flag
.
Flag
)
{
name
:=
f
.
Name
value
:=
f
.
Value
.
String
()
k
,
ok
:=
c
.
tagMapping
[
name
]
if
!
ok
{
s
.
errors
=
append
(
s
.
errors
,
newUnknownFlagError
(
name
))
return
}
pa
:=
append
(
c
.
valuePath
,
k
)
p
:=
strings
.
Join
(
pa
,
"."
)
err
:=
setValueUsingPath
(
&
s
.
definitions
,
p
,
value
)
if
err
!=
nil
{
s
.
errors
=
append
(
s
.
errors
,
err
)
}
err
=
c
.
setShadowValue
(
s
.
shadow
,
k
,
value
)
if
err
!=
nil
{
s
.
errors
=
append
(
s
.
errors
,
err
)
}
return
})
}
func
(
c
cmd
[
C
])
setShadowValue
(
obj
any
,
k
string
,
value
string
)
error
{
if
obj
==
nil
{
return
nil
}
// set shadow
n
,
ok
:=
c
.
shadowMapping
[
k
]
if
!
ok
{
return
nil
}
return
setValueUsingPath
(
obj
,
n
,
value
)
}
This diff is collapsed.
Click to expand it.
shadow_test.go
+
14
−
0
View file @
c9a0899e
...
...
@@ -44,3 +44,17 @@ func TestFlagCopyToShadow(t *testing.T) {
assert
.
True
(
t
,
c
.
ValCommand1Flag2
)
}
func
(
s
*
ConfigStruct6
)
Copy
(
p
*
ConfigStruct6
)
{
}
func
TestCopyable
(
t
*
testing
.
T
)
{
c
:=
ConfigStruct6
{}
c
.
ValSub
.
Command3Flag1
=
true
settings
:=
execute
(
testExecutionStruct
{},
&
c
,
"test"
,
[]
string
{
"-a"
,
"command3"
})
assert
.
False
(
t
,
settings
.
HasErrors
())
}
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