alecthomas / alecthomas/kingpin

Incorrect handling of commands with arguments (v3 unstable)

Đang mở
#204 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Go
Star
3.6k
Fork
279
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

When using kingpin, I was expecting to be able to write the following code to parse a command:

```go
args := os.Args[1:]
switch kingpin.MustParse(app.Parse(args)) {
case initCmd.FullCommand():
initAction(*initCmdArg)
// ...
default:
app.Usage(args)
}
```

Given the CLI args `myapp init file.txt`, I got the usage message displayed, when I wanted initAction to be called.

Currently in kingpin (v3 unstable) I am working around this issue using:

```go
args := os.Args[1:]
switch kingpin.MustParse(app.Parse(args)) {
case "init":
initData(*initCmdArg)
// ...
default:
app.Usage(args)
}
```

## Expected Behavior

As stated above, I am expecting the output of `kingpin.MustParse(app.Parse(args))` to equal the result of `.FullCommand()` function of a command, rather than to have to define my own strings in the switch statement.

Below is an example block of test code that I would expect to passed.

```go
package main

import (
"testing"

kingpin "gopkg.in/alecthomas/kingpin.v3-unstable"
)

func TestParse(t *testing.T) {
app := kingpin.New("myApp", "Lorem ipsum.")
initCmd := app.Command("init", "Lorem init.")
_ = initCmd.Arg("file", "Lorem file.").String()

importCmd := app.Command("import", "Lorem import")

testCases := []struct {
testName string
args []string
want *kingpin.CmdClause
}{
{"SimpleCommand", []string{"import"}, importCmd},
{"CommandWithArg", []string{"init", "file.txt"}, initCmd},
}

for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
parsed := kingpin.MustParse(app.Parse(tc.args))

if parsed != tc.want.FullCommand() {
t.Errorf("Expected %q to equal %q", parsed, tc.want.FullCommand())
}
})
}
}
```

## Current Behavior

Test Output:
```bash
Running tool: /usr/bin/go test -timeout 30s -tags -run ^TestParse$

--- FAIL: TestParse (0.00s)
--- FAIL: TestParse/CommandWithArg (0.00s)
/home/matthewdunsdon/go/src/github.com/matthewdunsdon/explore-kingpin-3/main_test.go:38: Expected "init" to equal "init []"
FAIL
exit status 1
FAIL github.com/matthewdunsdon/explore-kingpin-3 0.005s
Error: Tests failed.
```

I do not mind whether the parsed output is changed to `"init []"` or whether `importCmd.FullCommand()` returns `"init"`, however I would like the test to pass.

## Context

My reasons for using (v3 unstable) are that:

1. I want to be able to write apps where I can use the `app.UsageContext` to include examples in the usage context. I have written `github.com/matthewdunsdon/egcmd` [![GoDoc](https://godoc.org/github.com/matthewdunsdon/egcmd?status.svg)](https://godoc.org/github.com/matthewdunsdon/egcmd) and `github.com/matthewdunsdon/egcmd/egkingpin` [![GoDoc](https://godoc.org/github.com/matthewdunsdon/egcmd/egkingpin?status.svg)](https://godoc.org/github.com/matthewdunsdon/egcmd/egkingpin) to make this possible.
1. I have no intention to take the apps I write with (v3 unstable) into any production environment. I am using them for my own learning and own use.

## Environment
* Package source: `"gopkg.in/alecthomas/kingpin.v3-unstable"`
* Go version used: `1.8`
* Operating System and version (desktop or mobile): Solus (Linux x64)
```bash
> lsb_release -a
LSB Version: 1.4
Distributor ID: Solus
Description: Solus
Release: 2017.04.18.0
Codename: shannon
```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.