Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
  • v1.0.0
  • v1.1.0
  • v1.1.1
  • v1.10.0
  • v1.10.1
  • v1.10.2
  • v1.11.0
  • v1.12.0
  • v1.13.0
  • v1.13.1
  • v1.13.2
  • v1.14.0
  • v1.15.0
  • v1.16.0
  • v1.16.1
  • v1.16.2
  • v1.16.3
  • v1.16.4
  • v1.16.5
  • v1.2.0
  • v1.2.1
  • v1.2.2
  • v1.2.3
  • v1.3.0
  • v1.3.1
  • v1.4.0
  • v1.5.0
  • v1.6.0
  • v1.7.0
  • v1.8.0
  • v1.8.1
  • v1.8.2
  • v1.8.3
  • v1.9.0
35 results

Target

Select target project
  • oss/libraries/go/application/xflags
1 result
Select Git revision
  • master
  • v1.0.0
  • v1.1.0
  • v1.1.1
  • v1.10.0
  • v1.10.1
  • v1.10.2
  • v1.11.0
  • v1.12.0
  • v1.13.0
  • v1.13.1
  • v1.13.2
  • v1.14.0
  • v1.15.0
  • v1.16.0
  • v1.16.1
  • v1.16.2
  • v1.16.3
  • v1.16.4
  • v1.16.5
  • v1.2.0
  • v1.2.1
  • v1.2.2
  • v1.2.3
  • v1.3.0
  • v1.3.1
  • v1.4.0
  • v1.5.0
  • v1.6.0
  • v1.7.0
  • v1.8.0
  • v1.8.1
  • v1.8.2
  • v1.8.3
  • v1.9.0
35 results
Show changes
Commits on Source (7)
<a name="v1.13.1"></a>
## [v1.13.1] - 2022-12-23
<a name="v1.13.0"></a> <a name="v1.13.0"></a>
## [v1.13.0] - 2022-10-23 ## [v1.13.0] - 2022-10-23
### Add Features ### Add Features
...@@ -150,6 +153,7 @@ ...@@ -150,6 +153,7 @@
<a name="v1.0.0"></a> <a name="v1.0.0"></a>
## v1.0.0 - 2022-10-04 ## v1.0.0 - 2022-10-04
[v1.13.1]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.13.0...v1.13.1
[v1.13.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.12.0...v1.13.0 [v1.13.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.12.0...v1.13.0
[v1.12.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.11.0...v1.12.0 [v1.12.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.11.0...v1.12.0
[v1.11.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.10.2...v1.11.0 [v1.11.0]: https://gitlab.schukai.com/oss/libraries/go/application/xflags/compare/v1.10.2...v1.11.0
......
...@@ -15,7 +15,7 @@ It supports: ...@@ -15,7 +15,7 @@ It supports:
## Installation ## Installation
```shell ```shell
go get gitlab.schukai.com/oss/libraries/go/network/xflags go get gitlab.schukai.com/oss/libraries/go/application/xflags
``` ```
**Note:** This library uses [Go Modules](https://github.com/golang/go/wiki/Modules) to manage dependencies. **Note:** This library uses [Go Modules](https://github.com/golang/go/wiki/Modules) to manage dependencies.
...@@ -33,7 +33,7 @@ package main ...@@ -33,7 +33,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"gitlab.schukai.com/oss/libraries/go/network/xflags" "gitlab.schukai.com/oss/libraries/go/application/xflags"
) )
``` ```
......
...@@ -122,6 +122,21 @@ func (c *cmd[C]) initFlags(x reflect.Value, m map[string]string) { ...@@ -122,6 +122,21 @@ func (c *cmd[C]) initFlags(x reflect.Value, m map[string]string) {
if m[tagLong] != "" { if m[tagLong] != "" {
c.flagSet.String(m[tagLong], x.String(), m[tagDescription]) c.flagSet.String(m[tagLong], x.String(), m[tagDescription])
} }
case reflect.Slice:
if x.Type() == reflect.TypeOf(StringFlags{}) {
if m[tagShort] != "" {
xx := x.Interface().(StringFlags)
c.flagSet.Var(&xx, m[tagShort], m[tagDescription])
}
if m[tagLong] != "" {
xx := x.Interface().(StringFlags)
c.flagSet.Var(&xx, m[tagLong], m[tagDescription])
}
}
default: default:
c.settings.errors = append(c.settings.errors, newUnsupportedFlagTypeError(x.Type())) c.settings.errors = append(c.settings.errors, newUnsupportedFlagTypeError(x.Type()))
...@@ -163,7 +178,6 @@ func (c *cmd[C]) parseStruct(dta any) { ...@@ -163,7 +178,6 @@ func (c *cmd[C]) parseStruct(dta any) {
} }
c.initFlags(x, m) c.initFlags(x, m)
} else if m[tagCommand] != "" { } else if m[tagCommand] != "" {
//c.tagMapping["cmd"+m[tagCommand]] = v.Type().Field(i).Name
c.initCommands(x, m, v.Type().Field(i).Name) c.initCommands(x, m, v.Type().Field(i).Name)
} else if m[tagIgnore] != "" { } else if m[tagIgnore] != "" {
......
...@@ -20,19 +20,16 @@ func (s *Settings[C]) HasErrors() bool { ...@@ -20,19 +20,16 @@ func (s *Settings[C]) HasErrors() bool {
return len(s.errors) > 0 return len(s.errors) > 0
} }
func (s *Settings[C]) HasHint() bool {
return s.hint != ""
}
func (s *Settings[C]) GetHint() string {
return s.hint
}
// Get all errors // Get all errors
func (s *Settings[C]) Errors() []error { func (s *Settings[C]) Errors() []error {
return s.errors return s.errors
} }
func (s *Settings[C]) AddError(err error) *Settings[C] {
s.errors = append(s.errors, err)
return s
}
var WatchListNotInitializedError = errors.New("watch list not initialized") var WatchListNotInitializedError = errors.New("watch list not initialized")
var MissingCommandError = errors.New("missing command") var MissingCommandError = errors.New("missing command")
var NotParsedError = errors.New("flag set not parsed") var NotParsedError = errors.New("flag set not parsed")
......
...@@ -6,10 +6,8 @@ package xflags ...@@ -6,10 +6,8 @@ package xflags
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/stretchr/testify/assert"
"reflect" "reflect"
"strings" "strings"
"testing"
) )
func (s *Settings[C]) Execute() *Settings[C] { func (s *Settings[C]) Execute() *Settings[C] {
...@@ -118,33 +116,3 @@ func (s *Settings[C]) MissingCommand() bool { ...@@ -118,33 +116,3 @@ func (s *Settings[C]) MissingCommand() bool {
func (s *Settings[C]) WasExecuted() bool { func (s *Settings[C]) WasExecuted() bool {
return s.wasExecuted return s.wasExecuted
} }
func TestWrongDefinitionType(t *testing.T) {
c := New("root", 2)
c.Parse([]string{"test"})
c.Execute()
assert.True(t, c.HasErrors())
}
type testExecuteCommandStruct struct {
Command1 struct {
} `command:"command1" description:"Command 1" callback:"command1Callback" `
Command2 struct {
Command3 struct {
} `command:"command3" description:"Command 3" callback:"command3Callback" call:"DoCmd3"`
} `command:"command2" description:"Command 2" callback:"command2Callback" `
}
func (c *testExecuteCommandStruct) DoCmd3(s *Settings[testExecuteCommandStruct]) {
}
func (c *testExecuteCommandStruct) command1Callback(args []string) {
fmt.Println("command1Callback", args)
}
func TestExecute1(t *testing.T) {
c := New("root", testExecuteCommandStruct{})
c.Parse([]string{"command2", "command3", "commandX"})
c.Execute()
assert.False(t, c.HasErrors())
}
...@@ -4,11 +4,42 @@ ...@@ -4,11 +4,42 @@
package xflags package xflags
import ( import (
"fmt"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"strconv" "strconv"
"testing" "testing"
) )
func TestWrongDefinitionType(t *testing.T) {
c := New("root", 2)
c.Parse([]string{"test"})
c.Execute()
assert.True(t, c.HasErrors())
}
type testExecuteCommandStruct struct {
Command1 struct {
} `command:"command1" description:"Command 1" callback:"command1Callback" `
Command2 struct {
Command3 struct {
} `command:"command3" description:"Command 3" callback:"command3Callback" call:"DoCmd3"`
} `command:"command2" description:"Command 2" callback:"command2Callback" `
}
func (c *testExecuteCommandStruct) DoCmd3(s *Settings[testExecuteCommandStruct]) {
}
func (c *testExecuteCommandStruct) command1Callback(args []string) {
fmt.Println("command1Callback", args)
}
func TestExecute1(t *testing.T) {
c := New("root", testExecuteCommandStruct{})
c.Parse([]string{"command2", "command3", "commandX"})
c.Execute()
assert.False(t, c.HasErrors())
}
type testExecutionStruct struct { type testExecutionStruct struct {
callbackCounter int `ignore:"true"` callbackCounter int `ignore:"true"`
......
...@@ -96,7 +96,7 @@ func getFlagTable(f *flag.FlagSet) []string { ...@@ -96,7 +96,7 @@ func getFlagTable(f *flag.FlagSet) []string {
// isZeroValue determines whether the string represents the zero // isZeroValue determines whether the string represents the zero
// value for a flag. // value for a flag.
func isZeroValue(f *flag.Flag, value string) (ok bool, err error) { func isZeroValue(f *flag.Flag, value string) (ok bool, err error) {
// Build a zero value of the flag's Value type, and see if the // Sync a zero value of the flag's Value type, and see if the
// result of calling its String method equals the value passed in. // result of calling its String method equals the value passed in.
// This works unless the Value type is itself an interface type. // This works unless the Value type is itself an interface type.
typ := reflect.TypeOf(f.Value) typ := reflect.TypeOf(f.Value)
......
...@@ -27,10 +27,11 @@ func (c *cmd[C]) getCommandLevel() (*cmd[C], []string) { ...@@ -27,10 +27,11 @@ func (c *cmd[C]) getCommandLevel() (*cmd[C], []string) {
return result, path return result, path
} }
// Help returns the help text for the command
func (s *Settings[C]) Help() string { func (s *Settings[C]) Help() string {
return s.createHelp(s.command, []string{})
}
cmd, path := s.command.getCommandLevel() func (s *Settings[C]) createHelp(cmd *cmd[C], path []string) string {
h := strings.Join(path, " ") h := strings.Join(path, " ")
var help string var help string
...@@ -67,6 +68,12 @@ func (s *Settings[C]) Help() string { ...@@ -67,6 +68,12 @@ func (s *Settings[C]) Help() string {
} }
} }
return help return help
} }
// Help returns the help text for the command
func (s *Settings[C]) ContextHelp() string {
cmd, path := s.command.getCommandLevel()
return s.createHelp(cmd, path)
}
...@@ -32,9 +32,9 @@ func TestGetHelp(t *testing.T) { ...@@ -32,9 +32,9 @@ func TestGetHelp(t *testing.T) {
args []string args []string
substring string substring string
}{ }{
{"test1", []string{"sub1", "--help"}, "Usage: test1 sub1 [global options] [command] [arguments]"}, {"test1", []string{"sub1", "--help"}, "Usage: [global options] [command] [arguments]\n\nGlobal Options:\n -a\tMessage A\n -x int\n \tMessage X\n\nCommand: sub1\n\nOptions:\n -b\tMessage B\n"},
{"test1", []string{"xsd", "help"}, "Usage: test1 [global options] [command] [arguments]"}, {"test1", []string{"xsd", "help"}, "Usage: [global options] [command] [arguments]\n\nGlobal Options:\n -a\tMessage A\n -x int\n \tMessage X\n\nCommand: sub1\n\nOptions:\n -b\tMessage B\n"},
{"test2", []string{"sub1", "sub2"}, "Usage: test2 sub1 sub2 [global options] [command] [arguments]"}, {"test2", []string{"sub1", "sub2"}, "Usage: [global options] [command] [arguments]\n\nGlobal Options:\n -a\tMessage A\n -x int\n \tMessage X\n\nCommand: sub1\n\nOptions:\n -b\tMessage B\n"},
} }
for _, table := range tables { for _, table := range tables {
......
package xflags
func (s *Settings[C]) SetHint(hint string) *Settings[C] {
s.hint = hint
return s
}
func (s *Settings[C]) HasHint() bool {
return s.hint != ""
}
func (s *Settings[C]) GetHint() string {
return s.hint
}
...@@ -48,7 +48,18 @@ func (s *Settings[C]) assignValues(c cmd[C]) { ...@@ -48,7 +48,18 @@ func (s *Settings[C]) assignValues(c cmd[C]) {
pa := append(c.valuePath, k) pa := append(c.valuePath, k)
p := strings.Join(pa, ".") p := strings.Join(pa, ".")
err := pathfinder.SetValue(&s.definitions, p, value) q, err := pathfinder.GetValue(&s.definitions, p)
if err != nil {
s.errors = append(s.errors, err)
return
}
if q == nil {
q = reflect.New(reflect.TypeOf(q).Elem()).Interface()
}
err = pathfinder.SetValue(&s.definitions, p, value)
if err != nil { if err != nil {
s.errors = append(s.errors, err) s.errors = append(s.errors, err)
} }
......
{"version":"1.13.0"} {"version":"1.13.1"}
package xflags
import (
"fmt"
"strings"
)
type StringFlags []string
func (i *StringFlags) String() string {
return strings.Join(*i, ",")
}
func (i *StringFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
type IntFlags []int
func (i *IntFlags) String() string {
r := make([]string, len(*i))
for k, v := range *i {
r[k] = fmt.Sprintf("%c", v)
}
return strings.Join(r, ",")
}
func (i *IntFlags) Set(value int) error {
*i = append(*i, value)
return nil
}