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
4d3fd0f0
Verified
Commit
4d3fd0f0
authored
2 years ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
feat new Execute() Method for the automatic execution of command
parent
65f72a7c
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
api.go
+42
-0
42 additions, 0 deletions
api.go
api_test.go
+30
-21
30 additions, 21 deletions
api_test.go
parse.go
+15
-1
15 additions, 1 deletion
parse.go
with
87 additions
and
22 deletions
api.go
+
42
−
0
View file @
4d3fd0f0
...
...
@@ -6,10 +6,52 @@ package xflags
import
(
"bytes"
"flag"
"fmt"
"io"
"os"
"reflect"
)
// Execute executes the command line arguments and calls the functions.
func
Execute
[
C
any
,
D
any
](
cmd
C
,
cnf
D
)
*
Settings
[
C
]
{
return
execute
(
cmd
,
cnf
,
os
.
Args
[
0
],
os
.
Args
[
1
:
])
}
func
(
s
*
Settings
[
C
])
PrintFlagOutput
()
{
fmt
.
Println
(
s
.
command
.
flagSet
.
Output
())
}
// execute is the internal implementation of Execute.
func
execute
[
C
any
,
D
any
](
cmd
C
,
cnf
D
,
name
string
,
args
[]
string
)
*
Settings
[
C
]
{
instance
:=
New
(
name
,
cmd
)
if
instance
.
HasErrors
()
{
return
instance
}
instance
.
SetShadow
(
cnf
)
if
instance
.
HasErrors
()
{
return
instance
}
instance
.
Parse
(
args
)
if
instance
.
HelpRequested
()
{
for
i
,
err
:=
range
instance
.
errors
{
if
err
==
flag
.
ErrHelp
{
instance
.
errors
=
append
(
instance
.
errors
[
:
i
],
instance
.
errors
[
i
+
1
:
]
...
)
}
}
instance
.
PrintFlagOutput
()
return
instance
}
if
instance
.
HasErrors
()
{
return
instance
}
instance
.
Execute
()
return
instance
}
// New creates a new instance of the settings.
// name should be the name of the command and comes from the first argument of the command line.
// os.Args[0] is a good choice.
...
...
This diff is collapsed.
Click to expand it.
api_test.go
+
30
−
21
View file @
4d3fd0f0
...
...
@@ -8,22 +8,31 @@ import (
"testing"
)
//func TestUsage(t *testing.T) {
//
// commands := New("root", CmdTest1{})
// args := []string{"-h"}
//
// commands.Parse(args)
//
// assert.False(t, commands.HasErrors())
// if commands.HasErrors() {
// t.Log(commands.Errors())
// }
//
// usage := commands.Usage()
// assert.NotEmpty(t, usage)
//
//}
func
TestUsage
(
t
*
testing
.
T
)
{
commands
:=
New
(
"root"
,
CmdTest1
{})
args
:=
[]
string
{
"-h"
}
commands
.
Parse
(
args
)
assert
.
True
(
t
,
commands
.
HelpRequested
())
usage
:=
commands
.
GetDefaults
()
assert
.
Equal
(
t
,
" -a
\t
Message A
\n
-x int
\n
\t
Message X
\n
"
,
usage
)
}
func
TestExecute
(
t
*
testing
.
T
)
{
instance
:=
execute
(
"root"
,
CmdTest1
{},
"test"
,
[]
string
{
"-a"
,
"hello"
,
"-x"
,
"1"
})
assert
.
NotNil
(
t
,
instance
)
}
func
TestExecuteHelp
(
t
*
testing
.
T
)
{
cnf
:=
struct
{
}{}
instance
:=
execute
(
CmdTest1
{},
&
cnf
,
"test"
,
[]
string
{
"-h"
})
assert
.
False
(
t
,
instance
.
HasErrors
())
}
func
TestNewIntWithError
(
t
*
testing
.
T
)
{
...
...
@@ -47,17 +56,17 @@ func TestNew(t *testing.T) {
}
type
CmdTest1
struct
{
A
bool
`short:"a"`
A
bool
`short:"a"
description:"Message A"
`
Sub1
struct
{
B
bool
`short:"b"`
B
bool
`short:"b"
description:"Message B"
`
Sub2
struct
{
C
bool
`short:"c"`
C
bool
`short:"c"
description:"Message C"
`
Sub3
struct
{
D
bool
`short:"d"`
D
bool
`short:"d"
description:"Message D"
`
}
`command:"sub3"`
}
`command:"sub2"`
}
`command:"sub1"`
aa
int
`short:"x"`
aa
int
`short:"x"
description:"Message X"
`
}
func
TestCommand2
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
parse.go
+
15
−
1
View file @
4d3fd0f0
...
...
@@ -3,7 +3,10 @@
package
xflags
import
"os"
import
(
"flag"
"os"
)
// ParseOsArgs parses the os.Args.
func
(
s
*
Settings
[
C
])
ParseOsArgs
()
*
Settings
[
C
]
{
...
...
@@ -38,3 +41,14 @@ func (s *Settings[C]) Parse(args []string) *Settings[C] {
return
s
}
func
(
s
*
Settings
[
C
])
HelpRequested
()
bool
{
for
_
,
err
:=
range
s
.
errors
{
if
err
==
flag
.
ErrHelp
{
return
true
}
}
return
false
}
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