Esempio n. 1
0
func TestParseEmpty(t *testing.T) {
	_, err := subcommands.Parse(&calc.Calc, "calc", []string{})
	if err == nil {
		t.Fatalf("expected an error")
	}
	if _, ok := err.(subcommands.ErrMissingCommand); !ok {
		t.Errorf("unexpected error type: %T", err)
	}
	if err.Error() != "missing mandatory subcommand" {
		t.Errorf("unexpected error message: %q", err.Error())
	}
}
Esempio n. 2
0
func TestParseHelp(t *testing.T) {
	result, err := subcommands.Parse(&calc.Calc, "calc", []string{"-help"})
	if err == nil {
		t.Fatalf("expected an error")
	}
	if err != flag.ErrHelp {
		t.Errorf("unexpected error message: %q", err.Error())
	}
	commands := result.ListCommands()
	if g, e := commands[len(commands)-1], &calc.Calc; g != e {
		t.Fatalf("unexpected dispatch: %#v != %#v", g, e)
	}
}
Esempio n. 3
0
func TestParseFlags(t *testing.T) {
	result, err := subcommands.Parse(&calc.Calc, "calc", []string{"sum", "-frobnicate", "1", "2"})
	if err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}
	commands := result.ListCommands()
	if g, e := commands[len(commands)-1], &sum.Sum; g != e {
		t.Fatalf("Unexpected dispatch: %#v != %#v", g, e)
	}
	if g, e := sum.Sum.Config.Frob, true; g != e {
		t.Errorf("Unexpected flag value: %#v != %#v", g, e)
	}
}
Esempio n. 4
0
func TestUsageSub(t *testing.T) {
	result, err := subcommands.Parse(&calc.Calc, "calc", []string{"sum", "-help"})
	if err != flag.ErrHelp {
		t.Errorf("unexpected error message: %q", err.Error())
	}
	var buf bytes.Buffer
	result.UsageTo(&buf)
	if g, e := string(buf.Bytes()), `Usage:
  calc sum [OPT..] A B

Options:
  -frobnicate=false: frobnicate the qubbitz
`; g != e {
		t.Errorf("unexpected usage:\n%s", g)
		t.Logf("got: %q", g)
		t.Logf("exp: %q", e)
	}
}
Esempio n. 5
0
func TestParseSimple(t *testing.T) {
	result, err := subcommands.Parse(&calc.Calc, "calc", []string{"sum", "1", "3"})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if g, e := result.Name(), "calc sum"; g != e {
		t.Fatalf("unexpected command name: %q != %q", g, e)
	}
	commands := result.ListCommands()
	if g, e := commands[len(commands)-1], &sum.Sum; g != e {
		t.Fatalf("unexpected dispatch: %#v != %#v", g, e)
	}
	if g, e := sum.Sum.Arguments.A, 1; g != e {
		t.Errorf("unexpected arg A: %#v != %#v", g, e)
	}
	if g, e := sum.Sum.Arguments.B, 3; g != e {
		t.Errorf("unexpected arg B: %#v != %#v", g, e)
	}
}
Esempio n. 6
0
func ExampleSynopsis() {
	type frobCommand struct {
		subcommands.Synopsis
	}

	var frob = frobCommand{
		Synopsis: "POLARITY PARTICLE <FILE",
	}

	result, err := subcommands.Parse(&frob, "frob", []string{"reverse", "neutron"})
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}
	result.UsageTo(os.Stdout)
	// Output:
	// Usage:
	//   frob POLARITY PARTICLE <FILE
}
Esempio n. 7
0
func TestUsage(t *testing.T) {
	result, err := subcommands.Parse(&calc.Calc, "calc", []string{"-help"})
	if err != flag.ErrHelp {
		t.Errorf("unexpected error message: %q", err.Error())
	}
	var buf bytes.Buffer
	result.UsageTo(&buf)
	if g, e := string(buf.Bytes()), `Usage:
  calc [OPT..] COMMAND..

Options:
  -v=false: verbose output

Commands:
  sum    sum two numbers
`; g != e {
		t.Errorf("unexpected usage:\n%s", g)
		t.Logf("got: %q", g)
		t.Logf("exp: %q", e)
	}
}
Esempio n. 8
0
// Main is primary entry point into the bazil command line
// application.
func Main() (exitstatus int) {
	progName := filepath.Base(os.Args[0])
	log.SetFlags(0)
	log.SetPrefix(progName + ": ")

	result, err := subcommands.Parse(&Bazil, progName, os.Args[1:])
	if err == flag.ErrHelp {
		result.Usage()
		return 0
	}
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", result.Name(), err)
		result.Usage()
		return 2
	}

	ok := run(result)
	if !ok {
		return 1
	}
	return 0
}
Esempio n. 9
0
func TestParseMissingArgs(t *testing.T) {
	result, err := subcommands.Parse(&calc.Calc, "calc", []string{"sum", "1"})
	if err == nil {
		t.Fatalf("expected an error")
	}
	if _, ok := err.(positional.ErrMissingMandatoryArg); !ok {
		t.Errorf("unexpected error type: %T", err)
	}
	if err.Error() != "missing mandatory argument: B" {
		t.Errorf("unexpected error message: %q", err.Error())
	}
	commands := result.ListCommands()
	if g, e := commands[len(commands)-1], &sum.Sum; g != e {
		t.Fatalf("unexpected dispatch: %#v != %#v", g, e)
	}
	if g, e := sum.Sum.Arguments.A, 1; g != e {
		t.Errorf("unexpected arg A: %#v != %#v", g, e)
	}
	// did not get set
	if g, e := sum.Sum.Arguments.B, 0; g != e {
		t.Errorf("unexpected arg B: %#v != %#v", g, e)
	}
}
Esempio n. 10
0
func ExampleSynopses() {
	type compressCommand struct {
		subcommands.Synopses
	}

	var compress = compressCommand{
		Synopses: []string{
			// compress refuses to output compressed data to a tty
			">FILE",
			"-o FILE",
		},
	}

	result, err := subcommands.Parse(&compress, "compress", []string{})
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}
	result.UsageTo(os.Stdout)
	// Output:
	// Usage:
	//   compress >FILE
	//   compress -o FILE
}