Skip to content

Commit

Permalink
up: update logic for flags format, fix words style
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 13, 2022
1 parent 690c080 commit 80e07fd
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 133 deletions.
9 changes: 5 additions & 4 deletions .github/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ title: '## Change Log'
style: gh-release
# group names
names: [Refactor, Fixed, Feature, Update, Other]
#repo_url: https://github.com/gookit/gcli
# if empty will auto fetch by git remote
#repo_url: https://github.com/gookit/goutil

filters:
# message length should >= 12
Expand All @@ -30,7 +31,7 @@ rules:
contains: ['fix:']
- name: Feature
start_withs: [feat, new]
contains: [feature]
contains: [feature, 'feat:']
- name: Update
start_withs: [update, 'up:']
contains: []
start_withs: [up]
contains: ['update:', 'up:']
11 changes: 2 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,13 @@ jobs:
timeout-minutes: 10
strategy:
fail-fast: true
matrix:
go_version: [1.17]

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Go Faster
uses: WillAbides/setup-go-faster@v1.7.0
timeout-minutes: 3
with:
go-version: ${{ matrix.go_version }}

- name: Setup ENV
# https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
run: |
Expand All @@ -35,7 +27,8 @@ jobs:
- name: Generate changelog
run: |
go install github.com/gookit/gitw/cmd/chlog@latest
curl https://github.com/gookit/gitw/releases/latest/download/chlog-linux-amd64 -L -o /usr/local/bin/chlog
chmod a+x /usr/local/bin/chlog
chlog -c .github/changelog.yml -o changelog.md prev last
# https://github.com/softprops/action-gh-release
Expand Down
11 changes: 7 additions & 4 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ func (md *mapData) ClearData() {
// HookFunc definition.
//
// func arguments:
// in app, like: func(app *App, data ...interface{})
// in cmd, like: func(cmd *Command, data ...interface{})
//
// in app, like: func(app *App, data ...interface{})
// in cmd, like: func(cmd *Command, data ...interface{})
//
// type HookFunc func(obj interface{}, data interface{})
//
// return:
Expand Down Expand Up @@ -230,7 +232,7 @@ func newCmdLine() *cmdLine {
binFile := os.Args[0]
workDir, _ := os.Getwd()

// binName will contains work dir path on windows
// binName will contain work dir path on Windows
// if envutil.IsWin() {
// binFile = strings.Replace(CLI.binName, workDir+"\\", "", 1)
// }
Expand Down Expand Up @@ -307,7 +309,8 @@ func (c *cmdLine) hasHelpKeywords() bool {
// HelpVarFormat allow var replace on render help info.
//
// Default support:
// "{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}"
//
// "{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}"
const HelpVarFormat = "{$%s}"

// HelpVars struct. provide string var function for render help template.
Expand Down
31 changes: 16 additions & 15 deletions gargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,18 @@ func (ags *Arguments) ParseArgs(args []string) (err error) {
* command arguments
*************************************************************/

// AddArg binding an named argument for the command.
// AddArg binding a named argument for the command.
//
// Notice:
// - Required argument cannot be defined after optional argument
// - Only one array parameter is allowed
// - The (array) argument of multiple values can only be defined at the end
// - Required argument cannot be defined after optional argument
// - Only one array parameter is allowed
// - The (array) argument of multiple values can only be defined at the end
//
// Usage:
// cmd.AddArg("name", "description")
// cmd.AddArg("name", "description", true) // required
// cmd.AddArg("names", "description", true, true) // required and is arrayed
//
// cmd.AddArg("name", "description")
// cmd.AddArg("name", "description", true) // required
// cmd.AddArg("names", "description", true, true) // required and is arrayed
func (ags *Arguments) AddArg(name, desc string, requiredAndArrayed ...bool) *Argument {
newArg := NewArgument(name, desc, requiredAndArrayed...)
return ags.AddArgument(newArg)
Expand Down Expand Up @@ -127,10 +128,9 @@ func (ags *Arguments) BindArg(arg *Argument) *Argument {
// AddArgument binding a named argument for the command.
//
// Notice:
// - Required argument cannot be defined after optional argument
// - Only one array parameter is allowed
// - The (array) argument of multiple values can only be defined at the end
//
// - Required argument cannot be defined after optional argument
// - Only one array parameter is allowed
// - The (array) argument of multiple values can only be defined at the end
func (ags *Arguments) AddArgument(arg *Argument) *Argument {
if ags.argsIndexes == nil {
ags.argsIndexes = make(map[string]int)
Expand Down Expand Up @@ -191,9 +191,10 @@ func (ags *Arguments) HasArguments() bool {
// Arg get arg by defined name.
//
// Usage:
// intVal := ags.Arg("name").Int()
// strVal := ags.Arg("name").String()
// arrVal := ags.Arg("names").Array()
//
// intVal := ags.Arg("name").Int()
// strVal := ags.Arg("name").String()
// arrVal := ags.Arg("names").Array()
func (ags *Arguments) Arg(name string) *Argument {
i, ok := ags.argsIndexes[name]
if !ok {
Expand Down Expand Up @@ -228,7 +229,7 @@ type Argument struct {
ShowName string
// Required arg is required
Required bool
// Arrayed if is array, can allow accept multi values, and must in last.
// Arrayed if is array, can allow to accept multi values, and must in last.
Arrayed bool

// Handler custom argument value handler on call GetValue()
Expand Down
66 changes: 1 addition & 65 deletions gcli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package gcli is a simple to use command line application and tool library.
// Package gcli is a simple-to-use command line application and tool library.
//
// Contains: cli app, flags parse, interact, progress, data show tools.
//
Expand All @@ -10,10 +10,7 @@
package gcli

import (
"fmt"
"os"
"strconv"
"strings"
)

const (
Expand Down Expand Up @@ -233,64 +230,3 @@ func (g *GOptions) bindingFlags(fs *Flags) {
fs.BoolOpt(&g.noProgress, "no-progress", "np", g.noProgress, "Disable display progress message")
fs.BoolOpt(&g.noInteractive, "no-interactive", "ni", g.noInteractive, "Disable interactive confirmation operation")
}

/*************************************************************************
* verbose level
*************************************************************************/

// VerbLevel type.
type VerbLevel uint

// Int verbose level to int.
func (vl VerbLevel) Int() int {
return int(vl)
}

// String verbose level to string.
func (vl VerbLevel) String() string {
return fmt.Sprintf("%d=%s", vl, vl.Name())
}

// Upper verbose level to string.
func (vl VerbLevel) Upper() string {
return strings.ToUpper(vl.Name())
}

// Name verbose level to string.
func (vl VerbLevel) Name() string {
switch vl {
case VerbQuiet:
return "quiet"
case VerbError:
return "error"
case VerbWarn:
return "warn"
case VerbInfo:
return "info"
case VerbDebug:
return "debug"
case VerbCrazy:
return "crazy"
}
return "unknown"
}

// Set value from option binding.
func (vl *VerbLevel) Set(value string) error {
// int: level value.
if iv, err := strconv.Atoi(value); err == nil {
if iv > VerbCrazy.Int() {
*vl = VerbCrazy
} else if iv < 0 { // fallback to default level.
*vl = DefaultVerb
} else { // 0 - 5
*vl = VerbLevel(iv)
}

return nil
}

// string: level name.
*vl = name2verbLevel(value)
return nil
}
41 changes: 41 additions & 0 deletions gcli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,44 @@ func TestCmdLine(t *testing.T) {
is.Empty(gcli.CLI.ArgLine())
}
}

func TestSetStrictMode(t *testing.T) {
stm := gcli.StrictMode()
defer gcli.SetStrictMode(stm)

opts := struct {
name string
ok, bl bool
}{}

// gcli.SetVerbose(gcli.VerbDebug)
app := gcli.NewApp(gcli.NotExitOnEnd())
app.Add(&gcli.Command{
Name: "test",
Config: func(c *gcli.Command) {
c.StrOpt(&opts.name, "name", "n", "", "1")
c.BoolOpt(&opts.ok, "ok", "o", true, "2")
c.BoolOpt(&opts.bl, "bl", "b", false, "3")
},
Func: func(c *gcli.Command, _ []string) error {
return nil
},
})

app.Run([]string{"test", "-o", "-n", "inhere"})
assert.Eq(t, "inhere", opts.name)
assert.True(t, opts.ok)

app.Run([]string{"test", "-o=false", "-n=inhere"})
assert.Eq(t, "inhere", opts.name)
assert.False(t, opts.ok)

app.Run([]string{"test", "-ob"})
// assert.StrContains(t, errMsg, "ddd")

gcli.SetStrictMode(true)
app.Run([]string{"test", "-ob"})
assert.True(t, opts.ok)
assert.True(t, opts.bl)

}
77 changes: 70 additions & 7 deletions type_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gcli
import (
"fmt"
"strconv"
"strings"

"github.com/gookit/goutil/strutil"
)
Expand Down Expand Up @@ -94,9 +95,10 @@ func (s *EnumString) Set(value string) error {
return nil
}

// String type, an special string
// String type, a special string
//
// Usage:
//
// // case 1:
// var names gcli.String
// c.VarOpt(&names, "names", "", "multi name by comma split")
Expand All @@ -119,16 +121,77 @@ func (s *String) Set(val string) error {
}

// String to string
func (s String) String() string {
return string(s)
func (s *String) String() string {
return string(*s)
}

// Split value to []string
func (s String) Split(sep string) []string {
return strutil.ToStrings(string(s), sep)
func (s *String) Split(sep string) []string {
return strutil.ToStrings(string(*s), sep)
}

// Ints value to []int
func (s String) Ints(sep string) []int {
return strutil.Ints(string(s), sep)
func (s *String) Ints(sep string) []int {
return strutil.Ints(string(*s), sep)
}

/*************************************************************************
* verbose level
*************************************************************************/

// VerbLevel type.
type VerbLevel uint

// Int verbose level to int.
func (vl *VerbLevel) Int() int {
return int(*vl)
}

// String verbose level to string.
func (vl *VerbLevel) String() string {
return fmt.Sprintf("%d=%s", *vl, vl.Name())
}

// Upper verbose level to string.
func (vl *VerbLevel) Upper() string {
return strings.ToUpper(vl.Name())
}

// Name verbose level to string.
func (vl *VerbLevel) Name() string {
switch *vl {
case VerbQuiet:
return "quiet"
case VerbError:
return "error"
case VerbWarn:
return "warn"
case VerbInfo:
return "info"
case VerbDebug:
return "debug"
case VerbCrazy:
return "crazy"
}
return "unknown"
}

// Set value from option binding.
func (vl *VerbLevel) Set(value string) error {
// int: level value.
if iv, err := strconv.Atoi(value); err == nil {
if iv > int(VerbCrazy) {
*vl = VerbCrazy
} else if iv < 0 { // fallback to default level.
*vl = DefaultVerb
} else { // 0 - 5
*vl = VerbLevel(iv)
}

return nil
}

// string: level name.
*vl = name2verbLevel(value)
return nil
}

0 comments on commit 80e07fd

Please sign in to comment.