Agent-friendly argument normalization that auto-corrects common CLI syntax mistakes before cobra parses them: - Single-dash long flags: -zip -> --zip - Bare key=value: zip=33101 -> --zip=33101 - Typos via Levenshtein distance (max 2): --ziip -> --zip - Command typos: categoriess -> categories - Flag aliases: --zipcode, --dept, --search -> canonical names Corrections emit a "note:" line to stderr showing what was rewritten. Positional arguments for completion/help subcommands are preserved (e.g., "completion zsh" is not rewritten). Integration tests verify end-to-end behavior including tolerance notes, double-dash boundaries, and help output for rewritten args.
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestRunCLI_CompletionZsh(t *testing.T) {
|
|
var stdout bytes.Buffer
|
|
var stderr bytes.Buffer
|
|
|
|
code := runCLI([]string{"completion", "zsh"}, &stdout, &stderr)
|
|
|
|
assert.Equal(t, 0, code)
|
|
assert.Contains(t, stdout.String(), "#compdef pubcli")
|
|
assert.Empty(t, stderr.String())
|
|
}
|
|
|
|
func TestRunCLI_HelpStores(t *testing.T) {
|
|
var stdout bytes.Buffer
|
|
var stderr bytes.Buffer
|
|
|
|
code := runCLI([]string{"help", "stores"}, &stdout, &stderr)
|
|
|
|
assert.Equal(t, 0, code)
|
|
assert.Contains(t, stdout.String(), "pubcli stores [flags]")
|
|
assert.Empty(t, stderr.String())
|
|
}
|
|
|
|
func TestRunCLI_TolerantRewriteWithoutNetworkCall(t *testing.T) {
|
|
var stdout bytes.Buffer
|
|
var stderr bytes.Buffer
|
|
|
|
code := runCLI([]string{"stores", "-zip", "33101", "--help"}, &stdout, &stderr)
|
|
|
|
assert.Equal(t, 0, code)
|
|
assert.Contains(t, stdout.String(), "pubcli stores [flags]")
|
|
assert.Contains(t, stderr.String(), "interpreted `-zip` as `--zip`")
|
|
}
|
|
|
|
func TestRunCLI_DoubleDashBoundary(t *testing.T) {
|
|
var stdout bytes.Buffer
|
|
var stderr bytes.Buffer
|
|
|
|
code := runCLI([]string{"stores", "--", "zip", "33101", "--help"}, &stdout, &stderr)
|
|
|
|
assert.Equal(t, 0, code)
|
|
assert.Contains(t, stdout.String(), "pubcli stores [flags]")
|
|
assert.False(t, strings.Contains(stderr.String(), "interpreted `zip` as `--zip`"))
|
|
}
|