Remove unused parser resource plumbing
This commit is contained in:
@@ -4,7 +4,6 @@ package cobra
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
@@ -35,8 +34,7 @@ func Prepare(root *commandmeta.Command, options commandmeta.ParseOptions) *Sessi
|
||||
session.metadata = root
|
||||
session.flagAliases = append([]commandmeta.FlagAlias(nil), options.FlagAliases...)
|
||||
session.flagAliasesForPath = options.FlagAliasesForPath
|
||||
options = normalizeOptions(options)
|
||||
session.root, session.declarations = buildTree(root, options)
|
||||
session.root, session.declarations = buildTree(root)
|
||||
return session
|
||||
}
|
||||
|
||||
@@ -289,23 +287,7 @@ func Parse(root *commandmeta.Command, argv []string, options commandmeta.ParseOp
|
||||
return Prepare(root, options).Parse(argv)
|
||||
}
|
||||
|
||||
func normalizeOptions(options commandmeta.ParseOptions) commandmeta.ParseOptions {
|
||||
if options.Context == nil {
|
||||
options.Context = context.Background()
|
||||
}
|
||||
if options.In == nil {
|
||||
options.In = strings.NewReader("")
|
||||
}
|
||||
if options.Out == nil {
|
||||
options.Out = io.Discard
|
||||
}
|
||||
if options.Err == nil {
|
||||
options.Err = io.Discard
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
func buildTree(root *commandmeta.Command, options commandmeta.ParseOptions) (*spfcobra.Command, map[*spfcobra.Command]*commandmeta.Command) {
|
||||
func buildTree(root *commandmeta.Command) (*spfcobra.Command, map[*spfcobra.Command]*commandmeta.Command) {
|
||||
nodes := make(map[*spfcobra.Command]*commandmeta.Command)
|
||||
var build func(*commandmeta.Command) *spfcobra.Command
|
||||
build = func(declaration *commandmeta.Command) *spfcobra.Command {
|
||||
@@ -321,10 +303,8 @@ func buildTree(root *commandmeta.Command, options commandmeta.ParseOptions) (*sp
|
||||
if declaration.Runnable {
|
||||
backend.Run = func(*spfcobra.Command, []string) {}
|
||||
}
|
||||
backend.SetContext(options.Context)
|
||||
backend.SetIn(options.In)
|
||||
backend.SetOut(options.Out)
|
||||
backend.SetErr(options.Err)
|
||||
backend.SetOut(io.Discard)
|
||||
backend.SetErr(io.Discard)
|
||||
backend.SetHelpFunc(func(*spfcobra.Command, []string) {})
|
||||
backend.SetFlagErrorFunc(func(_ *spfcobra.Command, err error) error { return err })
|
||||
backend.SetUsageTemplate(usageTemplate)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package cobra
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
@@ -182,7 +180,7 @@ func TestParseReturnsNeutralFailuresAndFreshState(t *testing.T) {
|
||||
tree := &Command{Name: "app", Children: []*Command{
|
||||
{Name: "run", Runnable: true, Flags: []Flag{{Name: "value"}}},
|
||||
}}
|
||||
options := ParseOptions{Context: context.Background(), In: strings.NewReader("unused"), Out: io.Discard, Err: io.Discard}
|
||||
options := ParseOptions{}
|
||||
first := Parse(tree, []string{"run", "--value=old"}, options)
|
||||
second := Parse(tree, []string{"run"}, options)
|
||||
if first.Failure != nil || second.Failure != nil {
|
||||
@@ -247,8 +245,7 @@ func TestSelectedPathsResolveThroughBackendParents(t *testing.T) {
|
||||
|
||||
func TestSessionReusesTreeForParseHelpAndUsageWithFreshState(t *testing.T) {
|
||||
tree := &Command{Name: "app", Children: []*Command{{Name: "run", Runnable: true, Flags: []Flag{{Name: "value"}}}}}
|
||||
var output, errors bytes.Buffer
|
||||
session := Prepare(tree, ParseOptions{Context: context.Background(), Out: &output, Err: &errors})
|
||||
session := Prepare(tree, ParseOptions{})
|
||||
first := session.Parse([]string{"run", "--value=old"})
|
||||
if first.Failure != nil || !reflect.DeepEqual(first.FlagValues, [][]string{{"old"}}) {
|
||||
t.Fatalf("first session parse = %#v", first)
|
||||
@@ -261,19 +258,15 @@ func TestSessionReusesTreeForParseHelpAndUsageWithFreshState(t *testing.T) {
|
||||
if failure != nil || !strings.Contains(line, "run") {
|
||||
t.Fatalf("session usage = %q, %v", line, failure)
|
||||
}
|
||||
if output.Len() != 0 || errors.Len() != 0 {
|
||||
t.Fatalf("help escaped session delivery control: out=%q err=%q", output.String(), errors.String())
|
||||
if session.root.OutOrStdout() != io.Discard || session.root.ErrOrStderr() != io.Discard {
|
||||
t.Fatal("backend destinations are not discarded")
|
||||
}
|
||||
|
||||
var secondOutput, secondErrors bytes.Buffer
|
||||
second := Prepare(tree, ParseOptions{Context: context.Background(), Out: &secondOutput, Err: &secondErrors})
|
||||
second := Prepare(tree, ParseOptions{})
|
||||
secondResult := second.Parse([]string{"run"})
|
||||
if secondResult.Failure != nil || !reflect.DeepEqual(secondResult.FlagValues, [][]string{nil}) {
|
||||
t.Fatalf("fresh session state = %#v", secondResult)
|
||||
}
|
||||
if output.Len() != 0 || errors.Len() != 0 || secondOutput.Len() != 0 || secondErrors.Len() != 0 {
|
||||
t.Fatal("prepared session streams were unexpectedly used")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingSessionTreeReturnsNeutralFailures(t *testing.T) {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package cobra
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -11,7 +9,7 @@ func BenchmarkPerformanceParse(b *testing.B) {
|
||||
wide := performanceFlatTree(128)
|
||||
wideArgs := []string{"wide127"}
|
||||
deep, deepArgs := performanceNestedTree(24)
|
||||
options := ParseOptions{Context: context.Background(), Out: io.Discard, Err: io.Discard}
|
||||
options := ParseOptions{}
|
||||
|
||||
for _, benchmark := range []struct {
|
||||
name string
|
||||
@@ -41,7 +39,7 @@ func BenchmarkPerformanceParse(b *testing.B) {
|
||||
func BenchmarkPerformanceSessionParseAndHelp(b *testing.B) {
|
||||
root := performanceFlatTree(8)
|
||||
args := []string{"wide007"}
|
||||
session := Prepare(root, ParseOptions{Context: context.Background(), Out: io.Discard, Err: io.Discard})
|
||||
session := Prepare(root, ParseOptions{})
|
||||
result := session.Parse(args)
|
||||
if result.Failure != nil {
|
||||
b.Fatalf("fixture parse failed: %s", result.Failure.Message)
|
||||
|
||||
@@ -2,11 +2,6 @@
|
||||
// its command-line backend.
|
||||
package commandmeta
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Key identifies one command inside one routing snapshot.
|
||||
type Key uint64
|
||||
|
||||
@@ -181,14 +176,10 @@ type Result struct {
|
||||
Failure *Failure
|
||||
}
|
||||
|
||||
// ParseOptions makes parser context and streams explicit.
|
||||
// ParseOptions selects flag aliases for parser contexts.
|
||||
type ParseOptions struct {
|
||||
// FlagAliasesForPath selects aliases for the actual parser context when
|
||||
// routing has not already selected one. It overrides FlagAliases when set.
|
||||
FlagAliasesForPath func([]string) []FlagAlias
|
||||
Context context.Context
|
||||
In io.Reader
|
||||
Out io.Writer
|
||||
Err io.Writer
|
||||
FlagAliases []FlagAlias
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user