Debian Codegraph @

unknown_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package flags
2
3 import (
4 "github.com/jessevdk/go-flags"
5 "testing"
6 )
7
8 func TestUnknownFlags(t *testing.T) {
9 var opts = struct {
10 Verbose []bool `short:"v" long:"verbose" description:"Verbose output"`
11 }{}
12
13 args := []string{
14 "-f",
15 }
16
17 p := flags.NewParser(&opts, 0)
18 args, err := p.ParseArgs(args)
19
20 if err == nil {
21 t.Fatal("Expected error for unknown argument")
22 }
23 }
24
25 func TestIgnoreUnknownFlags(t *testing.T) {
26 var opts = struct {
27 Verbose []bool `short:"v" long:"verbose" description:"Verbose output"`
28 }{}
29
30 args := []string{
31 "hello",
32 "world",
33 "-v",
34 "--foo=bar",
35 "--verbose",
36 "-f",
37 }
38
39 p := flags.NewParser(&opts, flags.IgnoreUnknown)
40 args, err := p.ParseArgs(args)
41
42 if err != nil {
43 t.Fatal(err)
44 }
45
46 exargs := []string{
47 "hello",
48 "world",
49 "--foo=bar",
50 "-f",
51 }
52
53 issame := (len(args) == len(exargs))
54
55 if issame {
56 for i := 0; i < len(args); i++ {
57 if args[i] != exargs[i] {
58 issame = false
59 break
60 }
61 }
62 }
63
64 if !issame {
65 t.Fatalf("Expected %v but got %v", exargs, args)
66 }
67 }