Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome committed Jul 19, 2024
1 parent e1833f2 commit db6ad1b
Show file tree
Hide file tree
Showing 26 changed files with 397 additions and 252 deletions.
50 changes: 20 additions & 30 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,32 @@ import (

var (
ErrCommandNotSpecified = fmt.Errorf("command not specified")
)

var (
envarTransformRegexp = regexp.MustCompile(`[^a-zA-Z0-9_]+`)
envarTransformRegexp = regexp.MustCompile(`[^a-zA-Z0-9_]+`)
)

type ApplicationValidator func(*Application) error

// An Application contains the definitions of flags, arguments and commands
// for an application.
type Application struct {
errorWriter io.Writer
usageWriter io.Writer
usageFuncs template.FuncMap
VersionFlag *FlagClause
HelpCommand *CmdClause
HelpFlag *FlagClause
terminate func(status int)
validator ApplicationValidator
usageTemplate string
version string
author string
Help string
Name string
cmdMixin
initialized bool

Name string
Help string

author string
version string
errorWriter io.Writer // Destination for errors.
usageWriter io.Writer // Destination for usage
usageTemplate string
usageFuncs template.FuncMap
validator ApplicationValidator
terminate func(status int) // See Terminate()
noInterspersed bool // can flags be interspersed with args (or must they come first)
noInterspersed bool
defaultEnvars bool
completion bool

// Help flag. Exposed for user customisation.
HelpFlag *FlagClause
// Help command. Exposed for user customisation. May be nil.
HelpCommand *CmdClause
// Version flag. Exposed for user customisation. May be nil.
VersionFlag *FlagClause
initialized bool
}

// New creates a new Kingpin application instance.
Expand Down Expand Up @@ -129,7 +120,7 @@ func (a *Application) Terminate(terminate func(int)) *Application {
}

// Writer specifies the writer to use for usage and errors. Defaults to os.Stderr.
// DEPRECATED: See ErrorWriter and UsageWriter.
// Deprecated: See ErrorWriter and UsageWriter.
func (a *Application) Writer(w io.Writer) *Application {
a.errorWriter = w
a.usageWriter = w
Expand Down Expand Up @@ -189,9 +180,8 @@ func (a *Application) parseContext(ignoreDefault bool, args []string) (*ParseCon
// This will populate all flag and argument values, call all callbacks, and so
// on.
func (a *Application) Parse(args []string) (command string, err error) {

context, parseErr := a.ParseContext(args)
selected := []string{}
var selected []string
var setValuesErr error

if context == nil {
Expand Down Expand Up @@ -239,8 +229,8 @@ func (a *Application) writeUsage(context *ParseContext, err error) {
if err != nil {
a.Errorf("%s", err)
}
if err := a.UsageForContext(context); err != nil {
panic(err)
if errUsage := a.UsageForContext(context); errUsage != nil {
panic(errUsage)
}
if err != nil {
a.terminate(1)
Expand Down
34 changes: 28 additions & 6 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package kingpin

import (
"errors"
"io/ioutil"

"github.com/stretchr/testify/assert"

"io"
"sort"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func newTestApp() *Application {
return New("test", "").Terminate(nil)
}

func TestCommander(t *testing.T) {
t.Parallel()
c := newTestApp()
ping := c.Command("ping", "Ping an IP address.")
pingTTL := ping.Flag("ttl", "TTL for ICMP packets").Short('t').Default("5s").Duration()
Expand All @@ -33,6 +33,7 @@ func TestCommander(t *testing.T) {
}

func TestRequiredFlags(t *testing.T) {
t.Parallel()
c := newTestApp()
c.Flag("a", "a").String()
c.Flag("b", "b").Required().String()
Expand All @@ -44,6 +45,7 @@ func TestRequiredFlags(t *testing.T) {
}

func TestRepeatableFlags(t *testing.T) {
t.Parallel()
c := newTestApp()
c.Flag("a", "a").String()
c.Flag("b", "b").Strings()
Expand All @@ -54,13 +56,15 @@ func TestRepeatableFlags(t *testing.T) {
}

func TestInvalidDefaultFlagValueErrors(t *testing.T) {
t.Parallel()
c := newTestApp()
c.Flag("foo", "foo").Default("a").Int()
_, err := c.Parse([]string{})
assert.Error(t, err)
}

func TestInvalidDefaultArgValueErrors(t *testing.T) {
t.Parallel()
c := newTestApp()
cmd := c.Command("cmd", "cmd")
cmd.Arg("arg", "arg").Default("one").Int()
Expand All @@ -69,6 +73,7 @@ func TestInvalidDefaultArgValueErrors(t *testing.T) {
}

func TestArgsRequiredAfterNonRequiredErrors(t *testing.T) {
t.Parallel()
c := newTestApp()
cmd := c.Command("cmd", "")
cmd.Arg("a", "a").String()
Expand All @@ -78,7 +83,8 @@ func TestArgsRequiredAfterNonRequiredErrors(t *testing.T) {
}

func TestArgsMultipleRequiredThenNonRequired(t *testing.T) {
c := newTestApp().Writer(ioutil.Discard)
t.Parallel()
c := newTestApp().Writer(io.Discard)
cmd := c.Command("cmd", "")
cmd.Arg("a", "a").Required().String()
cmd.Arg("b", "b").Required().String()
Expand All @@ -91,6 +97,7 @@ func TestArgsMultipleRequiredThenNonRequired(t *testing.T) {
}

func TestDispatchCallbackIsCalled(t *testing.T) {
t.Parallel()
dispatched := false
c := newTestApp()
c.Command("cmd", "").Action(func(*ParseContext) error {
Expand All @@ -104,6 +111,7 @@ func TestDispatchCallbackIsCalled(t *testing.T) {
}

func TestTopLevelArgWorks(t *testing.T) {
t.Parallel()
c := newTestApp()
s := c.Arg("arg", "help").String()
_, err := c.Parse([]string{"foo"})
Expand All @@ -112,6 +120,7 @@ func TestTopLevelArgWorks(t *testing.T) {
}

func TestTopLevelArgCantBeUsedWithCommands(t *testing.T) {
t.Parallel()
c := newTestApp()
c.Arg("arg", "help").String()
c.Command("cmd", "help")
Expand All @@ -120,13 +129,15 @@ func TestTopLevelArgCantBeUsedWithCommands(t *testing.T) {
}

func TestTooManyArgs(t *testing.T) {
t.Parallel()
a := newTestApp()
a.Arg("a", "").String()
_, err := a.Parse([]string{"a", "b"})
assert.Error(t, err)
}

func TestTooManyArgsAfterCommand(t *testing.T) {
t.Parallel()
a := newTestApp()
a.Command("a", "")
assert.NoError(t, a.init())
Expand All @@ -135,13 +146,15 @@ func TestTooManyArgsAfterCommand(t *testing.T) {
}

func TestArgsLooksLikeFlagsWithConsumeRemainder(t *testing.T) {
t.Parallel()
a := newTestApp()
a.Arg("opts", "").Required().Strings()
_, err := a.Parse([]string{"hello", "-world"})
assert.Error(t, err)
}

func TestCommandParseDoesNotResetFlagsToDefault(t *testing.T) {
t.Parallel()
app := newTestApp()
flag := app.Flag("flag", "").Default("default").String()
app.Command("cmd", "")
Expand All @@ -152,6 +165,7 @@ func TestCommandParseDoesNotResetFlagsToDefault(t *testing.T) {
}

func TestCommandParseDoesNotFailRequired(t *testing.T) {
t.Parallel()
app := newTestApp()
flag := app.Flag("flag", "").Required().String()
app.Command("cmd", "")
Expand All @@ -162,6 +176,7 @@ func TestCommandParseDoesNotFailRequired(t *testing.T) {
}

func TestSelectedCommand(t *testing.T) {
t.Parallel()
app := newTestApp()
c0 := app.Command("c0", "")
c0.Command("c1", "")
Expand All @@ -171,6 +186,7 @@ func TestSelectedCommand(t *testing.T) {
}

func TestSubCommandRequired(t *testing.T) {
t.Parallel()
app := newTestApp()
c0 := app.Command("c0", "")
c0.Command("c1", "")
Expand All @@ -179,6 +195,7 @@ func TestSubCommandRequired(t *testing.T) {
}

func TestInterspersedFalse(t *testing.T) {
t.Parallel()
app := newTestApp().Interspersed(false)
a1 := app.Arg("a1", "").String()
a2 := app.Arg("a2", "").String()
Expand All @@ -192,6 +209,7 @@ func TestInterspersedFalse(t *testing.T) {
}

func TestInterspersedTrue(t *testing.T) {
t.Parallel()
// test once with the default value and once with explicit true
for i := 0; i < 2; i++ {
app := newTestApp()
Expand All @@ -214,6 +232,7 @@ func TestInterspersedTrue(t *testing.T) {
}

func TestDefaultEnvars(t *testing.T) {
t.Parallel()
a := New("some-app", "").Terminate(nil).DefaultEnvars()
f0 := a.Flag("some-flag", "")
f0.Bool()
Expand All @@ -229,6 +248,7 @@ func TestDefaultEnvars(t *testing.T) {
}

func TestBashCompletionOptionsWithEmptyApp(t *testing.T) {
t.Parallel()
a := newTestApp()
context, err := a.ParseContext([]string{"--completion-bash"})
if err != nil {
Expand All @@ -239,6 +259,7 @@ func TestBashCompletionOptionsWithEmptyApp(t *testing.T) {
}

func TestBashCompletionOptions(t *testing.T) {
t.Parallel()
a := newTestApp()
a.Command("one", "")
a.Flag("flag-0", "").String()
Expand Down Expand Up @@ -411,10 +432,10 @@ func TestBashCompletionOptions(t *testing.T) {

assert.Equal(t, c.ExpectedOptions, args, "Expected != Actual: [%v] != [%v]. \nInput was: [%v]", c.ExpectedOptions, args, c.Args)
}

}

func TestCmdValidation(t *testing.T) {
t.Parallel()
c := newTestApp()
cmd := c.Command("cmd", "")

Expand All @@ -436,6 +457,7 @@ func TestCmdValidation(t *testing.T) {
}

func TestVersion(t *testing.T) {
t.Parallel()
c := newTestApp()
c.Flag("config", "path to config file").Default("config.yaml").ExistingFile()
c.Version("1.0.0")
Expand Down
14 changes: 7 additions & 7 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func (a *argGroup) init() error {
}

type ArgClause struct {
actionMixin
parserMixin
name string
help string
placeholder string
actionMixin
completionsMixin
envarMixin
name string
help string
defaultValues []string
placeholder string
hidden bool
required bool
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func (a *ArgClause) Hidden() *ArgClause {
}

// PlaceHolder sets the place-holder string used for arg values in the help. The
// default behaviour is to use the arg name between < > brackets.
// default behavior is to use the arg name between < > brackets.
func (a *ArgClause) PlaceHolder(value string) *ArgClause {
a.placeholder = value
return a
Expand Down Expand Up @@ -174,13 +174,13 @@ func (a *ArgClause) PreAction(action Action) *ArgClause {
return a
}

// HintAction registers a HintAction (function) for the arg to provide completions
// HintAction registers a HintAction (function) for the arg to provide completions.
func (a *ArgClause) HintAction(action HintAction) *ArgClause {
a.addHintAction(action)
return a
}

// HintOptions registers any number of options for the flag to provide completions
// HintOptions registers any number of options for the flag to provide completions.
func (a *ArgClause) HintOptions(options ...string) *ArgClause {
a.addHintAction(func() []string {
return options
Expand Down
Loading

0 comments on commit db6ad1b

Please sign in to comment.