Updated for go1.7
This commit is contained in:
parent
64910d97de
commit
24ee649f83
|
@ -1,6 +1,7 @@
|
|||
{
|
||||
"ImportPath": "github.com/starkandwayne/esuf",
|
||||
"GoVersion": "go1.5.3",
|
||||
"GoVersion": "go1.7",
|
||||
"GodepVersion": "v74",
|
||||
"Deps": [
|
||||
{
|
||||
"ImportPath": "github.com/voxelbrain/goptions",
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
/pkg
|
||||
/bin
|
29
Godeps/_workspace/src/github.com/voxelbrain/goptions/examples/readme_example.go
generated
vendored
29
Godeps/_workspace/src/github.com/voxelbrain/goptions/examples/readme_example.go
generated
vendored
|
@ -1,29 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/voxelbrain/goptions"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
options := struct {
|
||||
Server string `goptions:"-s, --server, obligatory, description='Server to connect to'"`
|
||||
Password string `goptions:"-p, --password, description='Don\\'t prompt for password'"`
|
||||
Timeout time.Duration `goptions:"-t, --timeout, description='Connection timeout in seconds'"`
|
||||
Help goptions.Help `goptions:"-h, --help, description='Show this help'"`
|
||||
|
||||
goptions.Verbs
|
||||
Execute struct {
|
||||
Command string `goptions:"--command, mutexgroup='input', description='Command to exectute', obligatory"`
|
||||
Script *os.File `goptions:"--script, mutexgroup='input', description='Script to exectute', rdonly"`
|
||||
} `goptions:"execute"`
|
||||
Delete struct {
|
||||
Path string `goptions:"-n, --name, obligatory, description='Name of the entity to be deleted'"`
|
||||
Force bool `goptions:"-f, --force, description='Force removal'"`
|
||||
} `goptions:"delete"`
|
||||
}{ // Default values goes here
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
goptions.ParseAndFail(&options)
|
||||
}
|
|
@ -1,101 +0,0 @@
|
|||
package goptions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ExampleFlagSet_PrintHelp() {
|
||||
options := struct {
|
||||
Server string `goptions:"-s, --server, obligatory, description='Server to connect to'"`
|
||||
Password string `goptions:"-p, --password, description='Don\\'t prompt for password'"`
|
||||
Timeout time.Duration `goptions:"-t, --timeout, description='Connection timeout in seconds'"`
|
||||
Help Help `goptions:"-h, --help, description='Show this help'"`
|
||||
|
||||
Verbs
|
||||
Execute struct {
|
||||
Command string `goptions:"--command, mutexgroup='input', description='Command to exectute', obligatory"`
|
||||
Script *os.File `goptions:"--script, mutexgroup='input', description='Script to exectute', rdonly"`
|
||||
} `goptions:"execute"`
|
||||
Delete struct {
|
||||
Path string `goptions:"-n, --name, obligatory, description='Name of the entity to be deleted'"`
|
||||
Force bool `goptions:"-f, --force, description='Force removal'"`
|
||||
} `goptions:"delete"`
|
||||
}{ // Default values goes here
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
args := []string{"--help"}
|
||||
fs := NewFlagSet("goptions", &options)
|
||||
err := fs.Parse(args)
|
||||
if err == ErrHelpRequest {
|
||||
fs.PrintHelp(os.Stdout)
|
||||
return
|
||||
} else if err != nil {
|
||||
fmt.Printf("Failure: %s", err)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Usage: goptions [global options] <verb> [verb options]
|
||||
//
|
||||
// Global options:
|
||||
// -s, --server Server to connect to (*)
|
||||
// -p, --password Don't prompt for password
|
||||
// -t, --timeout Connection timeout in seconds (default: 10s)
|
||||
// -h, --help Show this help
|
||||
//
|
||||
// Verbs:
|
||||
// delete:
|
||||
// -n, --name Name of the entity to be deleted (*)
|
||||
// -f, --force Force removal
|
||||
// execute:
|
||||
// --command Command to exectute (*)
|
||||
// --script Script to exectute
|
||||
}
|
||||
|
||||
func ExampleVerbs() {
|
||||
options := struct {
|
||||
ImportantFlag string `goptions:"-f, --flag, description='Important flag, obligatory'"`
|
||||
Password string `goptions:"-p, --password, description='Don\\'t prompt for password'"`
|
||||
Timeout time.Duration `goptions:"-t, --timeout, description='Connection timeout in seconds'"`
|
||||
Help Help `goptions:"-h, --help, description='Show this help'"`
|
||||
|
||||
Verb Verbs
|
||||
Execute struct {
|
||||
Command string `goptions:"--command, mutexgroup='input', description='Command to exectute', obligatory"`
|
||||
Script *os.File `goptions:"--script, mutexgroup='input', description='Script to exectute', rdonly"`
|
||||
} `goptions:"execute"`
|
||||
Delete struct {
|
||||
Path string `goptions:"-n, --name, obligatory, description='Name of the entity to be deleted'"`
|
||||
Force bool `goptions:"-f, --force, description='Force removal'"`
|
||||
} `goptions:"delete"`
|
||||
}{ // Default values goes here
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
args := []string{"delete", "-n", "/usr/bin"}
|
||||
fs := NewFlagSet("goptions", &options)
|
||||
_ = fs.Parse(args)
|
||||
// Error handling omitted
|
||||
fmt.Printf("Selected verb: %s", options.Verb)
|
||||
|
||||
// Output:
|
||||
// Selected verb: delete
|
||||
}
|
||||
|
||||
func ExampleRemainder() {
|
||||
options := struct {
|
||||
Username string `goptions:"-u, --user, obligatory, description='Name of the user'"`
|
||||
Remainder Remainder
|
||||
}{}
|
||||
|
||||
args := []string{"-u", "surma", "some", "more", "args"}
|
||||
fs := NewFlagSet("goptions", &options)
|
||||
_ = fs.Parse(args)
|
||||
// Error handling omitted
|
||||
fmt.Printf("Remainder: %#v", options.Remainder)
|
||||
|
||||
// Output:
|
||||
// Remainder: goptions.Remainder{"some", "more", "args"}
|
||||
}
|
|
@ -1,73 +0,0 @@
|
|||
package goptions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Name struct {
|
||||
FirstName string
|
||||
LastName string
|
||||
}
|
||||
|
||||
func (n *Name) MarshalGoption(val string) error {
|
||||
f := strings.SplitN(val, " ", 2)
|
||||
if len(f) != 2 {
|
||||
return fmt.Errorf("Incomplete name")
|
||||
}
|
||||
n.FirstName = f[0]
|
||||
n.LastName = f[1]
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestMarshaler(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Name *Name `goptions:"--name"`
|
||||
}
|
||||
args = []string{"--name", "Alexander Surma"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
expected := &Name{
|
||||
FirstName: "Alexander",
|
||||
LastName: "Surma",
|
||||
}
|
||||
if !reflect.DeepEqual(options.Name, expected) {
|
||||
t.Fatalf("Unexpected value: %#v", options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArrayOfMarshaler(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Names []*Name `goptions:"--name"`
|
||||
}
|
||||
args = []string{"--name", "Alexander Surma", "--name", "Yo Mama"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
expected := []*Name{
|
||||
&Name{
|
||||
FirstName: "Alexander",
|
||||
LastName: "Surma",
|
||||
},
|
||||
&Name{
|
||||
FirstName: "Yo",
|
||||
LastName: "Mama",
|
||||
},
|
||||
}
|
||||
if !reflect.DeepEqual(options.Names, expected) {
|
||||
t.Fatalf("Unexpected value: %#v", options)
|
||||
}
|
||||
}
|
|
@ -1,452 +0,0 @@
|
|||
package goptions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParse_StringValue(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Name string `goptions:"--name, -n"`
|
||||
}
|
||||
expected := "SomeName"
|
||||
|
||||
args = []string{"--name", "SomeName"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Flag parsing failed: %s", err)
|
||||
}
|
||||
if options.Name != expected {
|
||||
t.Fatalf("Expected %s for options.Name, got %s", expected, options.Name)
|
||||
}
|
||||
|
||||
options.Name = ""
|
||||
|
||||
args = []string{"-n", "SomeName"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Flag parsing failed: %s", err)
|
||||
}
|
||||
if options.Name != expected {
|
||||
t.Fatalf("Expected %s for options.Name, got %s", expected, options.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_ObligatoryStringValue(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Name string `goptions:"-n, obligatory"`
|
||||
}
|
||||
args = []string{}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err == nil {
|
||||
t.Fatalf("Parsing should have failed.")
|
||||
}
|
||||
|
||||
args = []string{"-n", "SomeName"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
|
||||
expected := "SomeName"
|
||||
if options.Name != expected {
|
||||
t.Fatalf("Expected %s for options.Name, got %s", expected, options.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_UnknownFlag(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Name string `goptions:"--name, -n"`
|
||||
}
|
||||
args = []string{"-k", "4"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err == nil {
|
||||
t.Fatalf("Parsing should have failed.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_FlagCluster(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Fast bool `goptions:"-f"`
|
||||
Silent bool `goptions:"-q"`
|
||||
Serious bool `goptions:"-s"`
|
||||
Crazy bool `goptions:"-c"`
|
||||
Verbose bool `goptions:"-v"`
|
||||
}
|
||||
args = []string{"-fqcv"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
|
||||
if !(options.Fast &&
|
||||
options.Silent &&
|
||||
!options.Serious &&
|
||||
options.Crazy &&
|
||||
options.Verbose) {
|
||||
t.Fatalf("Unexpected value: %v", options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_MutexGroup(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Create bool `goptions:"--create, mutexgroup='action'"`
|
||||
Delete bool `goptions:"--delete, mutexgroup='action'"`
|
||||
}
|
||||
args = []string{"--create", "--delete"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err == nil {
|
||||
t.Fatalf("Parsing should have failed.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_HelpFlag(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Name string `goptions:"--name, -n"`
|
||||
Help `goptions:"--help, -h"`
|
||||
}
|
||||
args = []string{"-n", "SomeNone", "-h"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != ErrHelpRequest {
|
||||
t.Fatalf("Expected ErrHelpRequest, got: %s", err)
|
||||
}
|
||||
|
||||
args = []string{"-n", "SomeNone"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error returned: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_Verbs(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Server string `goptions:"--server, -s"`
|
||||
|
||||
Verbs
|
||||
Create struct {
|
||||
Name string `goptions:"--name, -n"`
|
||||
} `goptions:"create"`
|
||||
}
|
||||
|
||||
args = []string{"-s", "127.0.0.1", "create", "-n", "SomeDocument"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
|
||||
if !(options.Server == "127.0.0.1" &&
|
||||
options.Create.Name == "SomeDocument" &&
|
||||
options.Verbs == "create") {
|
||||
t.Fatalf("Unexpected value: %v", options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_IntValue(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Limit int `goptions:"-l"`
|
||||
}
|
||||
|
||||
args = []string{"-l", "123"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
|
||||
if !(options.Limit == 123) {
|
||||
t.Fatalf("Unexpected value: %v", options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_Remainder(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Limit int `goptions:"-l"`
|
||||
Remainder
|
||||
}
|
||||
|
||||
args = []string{"-l", "123", "Something", "SomethingElse"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
|
||||
if !(len(options.Remainder) == 2 &&
|
||||
options.Remainder[0] == "Something" &&
|
||||
options.Remainder[1] == "SomethingElse") {
|
||||
t.Fatalf("Unexpected value: %v", options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_VerbRemainder(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Limit int `goptions:"-l"`
|
||||
Remainder
|
||||
|
||||
Verbs
|
||||
Create struct {
|
||||
Fast bool `goptions:"-f"`
|
||||
Remainder
|
||||
} `goptions:"create"`
|
||||
}
|
||||
|
||||
args = []string{"create", "-f", "Something", "SomethingElse"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
|
||||
if !(len(options.Remainder) == 2 &&
|
||||
options.Remainder[0] == "Something" &&
|
||||
options.Remainder[1] == "SomethingElse" &&
|
||||
options.Verbs == "create") {
|
||||
t.Fatalf("Unexpected value: %v", options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_NoRemainder(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Fast bool `goptions:"-f"`
|
||||
}
|
||||
|
||||
args = []string{"-f", "Something", "SomethingElse"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err == nil {
|
||||
t.Fatalf("Parsing should have failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_MissingValue(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Name string `goptions:"-n, --name"`
|
||||
}
|
||||
|
||||
args = []string{"-n"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err == nil {
|
||||
t.Fatalf("Parsing should have failed")
|
||||
}
|
||||
|
||||
args = []string{"--name"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err == nil {
|
||||
t.Fatalf("Parsing should have failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_ObligatoryMutexGroup(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Create bool `goptions:"-c, mutexgroup='action', obligatory"`
|
||||
Delete bool `goptions:"-d, mutexgroup='action'"`
|
||||
}
|
||||
|
||||
args = []string{}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err == nil {
|
||||
t.Fatalf("Parsing should have failed")
|
||||
}
|
||||
|
||||
args = []string{"-c", "-d"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err == nil {
|
||||
t.Fatalf("Parsing should have failed")
|
||||
}
|
||||
|
||||
args = []string{"-d"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_StringArray_Short(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Servers []string `goptions:"-s"`
|
||||
}
|
||||
|
||||
args = []string{}
|
||||
for i := 1; i < 10; i++ {
|
||||
options.Servers = []string{}
|
||||
args = append(args, []string{"-s", fmt.Sprintf("server%d", i)}...)
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed at %d: %s", i, err)
|
||||
}
|
||||
if len(options.Servers) != i {
|
||||
t.Fatalf("Unexpected number of values. Expected %d, got %d (%#v)", i, len(options.Servers), options.Servers)
|
||||
}
|
||||
for j := 0; j < i; j++ {
|
||||
expected := fmt.Sprintf("server%d", j+1)
|
||||
if options.Servers[j] != expected {
|
||||
t.Fatalf("Unexpected value. %#v", options.Servers)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_BoolArray_Cluster(t *testing.T) {
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Verbosity []bool `goptions:"-v"`
|
||||
}
|
||||
|
||||
args := "-v"
|
||||
for i := 1; i < 10; i++ {
|
||||
options.Verbosity = []bool{}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse([]string{args})
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed at %d: %s", i, err)
|
||||
}
|
||||
if len(options.Verbosity) != i {
|
||||
t.Fatalf("Unexpected number of values. Expected %d, got %d (%#v)", i, len(options.Verbosity), options.Verbosity)
|
||||
}
|
||||
args += "v"
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_BoolArray_Short(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Verbosity []bool `goptions:"-v"`
|
||||
}
|
||||
|
||||
args = []string{}
|
||||
for i := 1; i < 10; i++ {
|
||||
options.Verbosity = []bool{}
|
||||
args = append(args, "-v")
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed at %d: %s", i, err)
|
||||
}
|
||||
if len(options.Verbosity) != i {
|
||||
t.Fatalf("Unexpected number of values. Expected %d, got %d (%#v)", i, len(options.Verbosity), options.Verbosity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_BoolArray_Long(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Verbosity []bool `goptions:"--verbose"`
|
||||
}
|
||||
|
||||
args = []string{}
|
||||
for i := 1; i < 10; i++ {
|
||||
options.Verbosity = []bool{}
|
||||
args = append(args, "--verbose")
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed at %d: %s", i, err)
|
||||
}
|
||||
if len(options.Verbosity) != i {
|
||||
t.Fatalf("Unexpected number of values. Expected %d, got %d (%#v)", i, len(options.Verbosity), options.Verbosity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_UnexportedVerbs(t *testing.T) {
|
||||
var options struct {
|
||||
Verbs
|
||||
A struct {
|
||||
A1 string `goptions:"--a1"`
|
||||
a2 string `goptions:"--a2"`
|
||||
} `goptions:"A"`
|
||||
}
|
||||
args := []string{"A", "--a1", "x"}
|
||||
fs := NewFlagSet("goptions", &options)
|
||||
err := fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
if options.A.A1 != "x" || options.A.a2 != "" {
|
||||
t.Fatalf("Unexpected values in struct: %#v", options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_DashAsRemainder(t *testing.T) {
|
||||
var options struct {
|
||||
SomeFlag bool `goptions:"-b"`
|
||||
Remainder
|
||||
}
|
||||
args := []string{"-b", "-"}
|
||||
fs := NewFlagSet("goptions", &options)
|
||||
err := fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
if len(options.Remainder) != 1 {
|
||||
t.Fatalf("Unexpected size of remainder: %d (%#v)", len(options.Remainder), options.Remainder)
|
||||
}
|
||||
if options.Remainder[0] != "-" {
|
||||
t.Fatalf("Unexpected remainder: %#v", options.Remainder)
|
||||
}
|
||||
|
||||
}
|
|
@ -1,67 +0,0 @@
|
|||
package goptions
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseTag_Minimal(t *testing.T) {
|
||||
var tag string
|
||||
tag = `--name, -n, description='Some name'`
|
||||
f, e := parseStructField(reflect.ValueOf(string("")), tag)
|
||||
if e != nil {
|
||||
t.Fatalf("Tag parsing failed: %s", e)
|
||||
}
|
||||
expected := &Flag{
|
||||
Long: "name",
|
||||
Short: "n",
|
||||
Description: "Some name",
|
||||
}
|
||||
if !flagequal(f, expected) {
|
||||
t.Fatalf("Expected %#v, got %#v", expected, f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTag_More(t *testing.T) {
|
||||
var tag string
|
||||
tag = `--name, -n, description='Some name', mutexgroup='selector', obligatory`
|
||||
f, e := parseStructField(reflect.ValueOf(string("")), tag)
|
||||
if e != nil {
|
||||
t.Fatalf("Tag parsing failed: %s", e)
|
||||
}
|
||||
expected := &Flag{
|
||||
Long: "name",
|
||||
Short: "n",
|
||||
Description: "Some name",
|
||||
MutexGroups: []string{"selector"},
|
||||
Obligatory: true,
|
||||
}
|
||||
if !flagequal(f, expected) {
|
||||
t.Fatalf("Expected %#v, got %#v", expected, f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTag_MultipleFlags(t *testing.T) {
|
||||
var tag string
|
||||
var e error
|
||||
tag = `--name1, --name2`
|
||||
_, e = parseStructField(reflect.ValueOf(string("")), tag)
|
||||
if e == nil {
|
||||
t.Fatalf("Parsing should have failed")
|
||||
}
|
||||
|
||||
tag = `-n, -v`
|
||||
_, e = parseStructField(reflect.ValueOf(string("")), tag)
|
||||
if e == nil {
|
||||
t.Fatalf("Parsing should have failed")
|
||||
}
|
||||
}
|
||||
|
||||
func flagequal(f1, f2 *Flag) bool {
|
||||
return f1.Short == f2.Short &&
|
||||
f1.Long == f2.Long &&
|
||||
reflect.DeepEqual(f1.MutexGroups, f2.MutexGroups) &&
|
||||
f1.Description == f2.Description &&
|
||||
f1.Obligatory == f2.Obligatory &&
|
||||
f1.WasSpecified == f2.WasSpecified
|
||||
}
|
|
@ -1,89 +0,0 @@
|
|||
package goptions
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestParse_File(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Output *os.File `goptions:"-o, create, trunc, wronly"`
|
||||
}
|
||||
|
||||
args = []string{"-o", "testfile"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
if !(options.Output != nil) {
|
||||
t.Fatalf("Unexpected value: %#v", options)
|
||||
}
|
||||
options.Output.Close()
|
||||
os.Remove("testfile")
|
||||
}
|
||||
|
||||
func TestParse_TCPAddr(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Server *net.TCPAddr `goptions:"-a"`
|
||||
}
|
||||
|
||||
args = []string{"-a", "192.168.0.100:8080"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
if !(options.Server.IP.String() == "192.168.0.100" &&
|
||||
options.Server.Port == 8080) {
|
||||
t.Fatalf("Unexpected value: %#v", options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_URL(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Server *url.URL `goptions:"-a"`
|
||||
}
|
||||
|
||||
args = []string{"-a", "http://www.google.com"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
if !(options.Server.Scheme == "http" &&
|
||||
options.Server.Host == "www.google.com") {
|
||||
t.Fatalf("Unexpected value: %#v", options.Server)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_Duration(t *testing.T) {
|
||||
var args []string
|
||||
var err error
|
||||
var fs *FlagSet
|
||||
var options struct {
|
||||
Cache time.Duration `goptions:"-d"`
|
||||
}
|
||||
|
||||
args = []string{"-d", "1h45m"}
|
||||
fs = NewFlagSet("goptions", &options)
|
||||
err = fs.Parse(args)
|
||||
if err != nil {
|
||||
t.Fatalf("Parsing failed: %s", err)
|
||||
}
|
||||
if !(int64(options.Cache) != (1*60+45)*60*1e12) {
|
||||
t.Fatalf("Unexpected value: %#v", options.Cache)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue