Exemplo n.º 1
0
func TestNewOptions_dupe(t *testing.T) {
	spec := `
--
a,bbb,ccc an option
d,bbb,eee an option with dupe`
	tu.ExpectDie(t, func() { NewOptions(spec) })
}
Exemplo n.º 2
0
func TestParse_extra(t *testing.T) {
	s := NewOptions("TestParse_extra\n--\nccc= doc [def]")
	s.Exit = exitToPanic
	opt := s.Parse([]string{"extra1", "--ccc", "myval", "extra2", "extra3=foo"})
	tu.ExpectEqual(t, opt.Get("ccc"), "myval", "Get")
	tu.ExpectEqual(t, opt.Flags, [][]string{[]string{"--ccc", "myval"}}, "flags specified")
	tu.ExpectEqual(t, opt.Extra, []string{"extra1", "extra2", "extra3=foo"}, "extra args given")

	s.SetUnknownValuesFatal(true)
	tu.ExpectDie(t, func() {
		s.Parse([]string{"extra1", "--ccc", "myval", "extra2"})
	}, "dies on extras when asked to")
}
Exemplo n.º 3
0
func TestParse_unknownFlags(t *testing.T) {
	s := NewOptions("TestParse_unknownFlags\n--\nccc= doc [def]")
	s.Exit = exitToPanic
	s.ErrorWriter = devNull{}

	tu.ExpectDie(t, func() {
		s.Parse([]string{"--ccc", "myval", "--unk"})
	}, "dies on unknown options unless asked not to")

	s.SetUnknownOptionsFatal(false)
	opt := s.Parse([]string{"--unk1", "--ccc", "myval", "--unk2", "val2", "--unk3"})
	tu.ExpectEqual(t, opt.Get("ccc"), "myval", "Get")
	tu.ExpectEqual(t, opt.Flags, [][]string{
		[]string{"--unk1"},
		[]string{"--ccc", "myval"},
		[]string{"--unk2", "val2"},
		[]string{"--unk3"}},
		"flags specified")
	tu.ExpectEqual(t, opt.Extra, []string{}, "no extra args given")
}
Exemplo n.º 4
0
func TestParse_missingArgument(t *testing.T) {
	s := NewOptions("TestParse_missingArgument\n--\na,bbb,ccc= doc [def]")
	s.Exit = exitToPanic
	s.ErrorWriter = devNull{}
	tu.ExpectDie(t, func() { s.Parse([]string{"--ccc"}) }, "missing required param")
}
Exemplo n.º 5
0
func TestClustering_missingArg(t *testing.T) {
	s := NewOptions("TestClustering_missingArg\n--\na,bbb doc\nb,ccc= doc")
	s.Exit = exitToPanic
	s.ErrorWriter = devNull{}
	tu.ExpectDie(t, func() { s.Parse([]string{"-aab"}) })
}