Skip to content

Commit 48904eb

Browse files
committed
fix: resolve lint errors in ScanArgs callers and related code
1 parent 8eb60cb commit 48904eb

5 files changed

Lines changed: 73 additions & 51 deletions

File tree

discover_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func TestDiscover_UnreadableDirectory_WithWarnFunc(t *testing.T) {
373373
readable := filepath.Join(dir, "readable")
374374
unreadable := filepath.Join(dir, "noperm")
375375

376-
require.NoError(t, os.Mkdir(readable, 0o755))
376+
require.NoError(t, os.Mkdir(readable, 0o750))
377377
require.NoError(t, os.Mkdir(unreadable, 0o000))
378378
t.Cleanup(func() {
379379
_ = os.Chmod(unreadable, 0o750) //nolint:errcheck,gosec // best-effort cleanup

execute.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,12 @@ func findSubcommand(subs []Commander, name string, prefixMatch, caseInsensitive
298298
}
299299
}
300300

301+
var match Commander
301302
if !prefixMatch {
302-
return nil, nil
303+
return match, nil
303304
}
304305

305306
// Unique prefix match.
306-
var match Commander
307307
var matches []string
308308
for _, s := range subs {
309309
info := resolveInfo(s)
@@ -370,7 +370,10 @@ func scanBranchingLevel(args []string, fi flagIndex, cmd Commander) ([]string, [
370370
// expects. Only required args are consumed during routing; optional args are
371371
// handled after subcommand matching to avoid consuming subcommand names.
372372
func countBranchArgs(cmd Commander) int {
373-
defs, _ := ScanArgs(cmd) // errors caught during populateLeafArgs
373+
defs, err := ScanArgs(cmd)
374+
if err != nil {
375+
return 0
376+
}
374377
count := 0
375378
for i := range defs {
376379
if defs[i].IsSlice {
@@ -388,7 +391,8 @@ func countBranchArgs(cmd Commander) int {
388391
// Returns the matched subcommand, any non-flag args that appeared before it
389392
// (potential optional args for the parent), remaining args after it, and any error.
390393
func findSubcommandInArgs(args []string, subs []Commander, prefixMatch, caseInsensitive bool) (*subMatch, error) {
391-
var before []string
394+
var result *subMatch
395+
before := make([]string, 0, len(args))
392396
for i, arg := range args {
393397
if strings.HasPrefix(arg, "-") {
394398
continue
@@ -398,11 +402,12 @@ func findSubcommandInArgs(args []string, subs []Commander, prefixMatch, caseInse
398402
return nil, err
399403
}
400404
if sub != nil {
401-
return &subMatch{sub: sub, before: before, remaining: args[i+1:]}, nil
405+
result = &subMatch{sub: sub, before: before, remaining: args[i+1:]}
406+
return result, nil
402407
}
403408
before = append(before, arg)
404409
}
405-
return nil, nil
410+
return result, nil
406411
}
407412

408413
// filterNonFlags returns only the non-flag args from the input slice.
@@ -826,7 +831,10 @@ func buildUsageLine(cmd Commander, path string) string {
826831
}
827832

828833
// Add positional args.
829-
args, _ := ScanArgs(cmd) // errors caught during execution
834+
args, err := ScanArgs(cmd)
835+
if err != nil {
836+
return usage.String()
837+
}
830838
for i := range args {
831839
switch {
832840
case args[i].Required:
@@ -899,7 +907,10 @@ func renderHelp(cmd Commander, chain []Commander, opts *options) error {
899907
sortFlags(globalFlags)
900908
}
901909

902-
args, _ := ScanArgs(cmd) // errors caught during execution
910+
args, err := ScanArgs(cmd)
911+
if err != nil {
912+
return err
913+
}
903914

904915
var text string
905916
if renderer != nil {
@@ -908,7 +919,7 @@ func renderHelp(cmd Commander, chain []Commander, opts *options) error {
908919
text = defaultRenderHelp(cmd, chain, flags, globalFlags, opts.sortedHelp)
909920
}
910921

911-
_, err := fmt.Fprint(opts.stdout, text)
922+
_, err = fmt.Fprint(opts.stdout, text)
912923
return err
913924
}
914925

@@ -973,7 +984,10 @@ func checkSubcommandRequired(cmd Commander) error {
973984
return nil
974985
}
975986
subs, err := allSubcommands(cmd)
976-
if err != nil || len(subs) == 0 {
987+
if err != nil {
988+
return err
989+
}
990+
if len(subs) == 0 {
977991
return nil // no subcommands defined, nothing to require
978992
}
979993
return fmt.Errorf("%w: %s", ErrMissingSubcommand, resolveInfo(cmd).name)

flags.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,10 +1232,7 @@ func validateTypeConstraints(f reflect.StructField) error {
12321232
if f.Tag.Get("sep") != "" && kind != reflect.Slice {
12331233
return fmt.Errorf("%w: field %s: sep requires slice type", ErrInvalidTag, f.Name)
12341234
}
1235-
if err := validateEnumValues(f); err != nil {
1236-
return err
1237-
}
1238-
return nil
1235+
return validateEnumValues(f)
12391236
}
12401237

12411238
// validateEnumValues checks that all enum values can be parsed as the field type.

help.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ func defaultRenderHelp(cmd Commander, chain []Commander, flags, globalFlags []Fl
4141
if len(allSubs) > 0 {
4242
fmt.Fprintf(&b, " %s [command]\n", chainNames)
4343
}
44-
argDefs, _ := ScanArgs(cmd) // errors caught during execution
44+
argDefs, err := ScanArgs(cmd)
45+
if err != nil {
46+
argDefs = nil
47+
}
4548
argUsage := buildArgUsage(argDefs)
4649

4750
if hasVisibleFlags(flags) || hasVisibleFlags(globalFlags) {

0 commit comments

Comments
 (0)