Example #1
0
func TestUsageEmpty(t *testing.T) {
	var args struct {
	}
	usage := positional.Usage(&args)
	if g, e := usage, ""; g != e {
		t.Errorf("unexpected usage: %q != %q", g, e)
	}
}
Example #2
0
func TestUsageMandatoryPlural(t *testing.T) {
	var args struct {
		Foo []string
	}
	usage := positional.Usage(&args)
	if g, e := usage, "FOO.."; g != e {
		t.Errorf("unexpected usage: %q != %q", g, e)
	}
}
Example #3
0
func TestUsageOptionalPlural(t *testing.T) {
	var args struct {
		positional.Optional
		Foo []string
	}
	usage := positional.Usage(&args)
	if g, e := usage, "[FOO..]"; g != e {
		t.Errorf("unexpected usage: %q != %q", g, e)
	}
}
Example #4
0
func TestUsageMandatoryTwo(t *testing.T) {
	var args struct {
		Foo string
		Bar string
	}
	usage := positional.Usage(&args)
	if g, e := usage, "FOO BAR"; g != e {
		t.Errorf("unexpected usage: %q != %q", g, e)
	}
}
Example #5
0
func ExampleUsage() {
	var args struct {
		Op string `positional:",metavar=ACTION"`
		positional.Optional
		Path []string
	}
	usage := positional.Usage(&args)
	fmt.Printf("Usage: doit [-v] %s\n", usage)
	// Output:
	// Usage: doit [-v] ACTION [PATH..]
}
Example #6
0
func TestUsageOptionalTwo(t *testing.T) {
	var args struct {
		positional.Optional
		Foo string
		Bar string
	}
	usage := positional.Usage(&args)
	if g, e := usage, "[FOO [BAR]]"; g != e {
		t.Errorf("unexpected usage: %q != %q", g, e)
	}
}
Example #7
0
// UsageTo writes a usage message for the active subcommand to the
// given Writer.
func (r *Result) UsageTo(w io.Writer) {
	fmt.Fprintf(w, "Usage:\n")
	cmd := r.last().cmd

	var synopses []string
	if s, ok := cmd.(SynopsesGetter); ok {
		synopses = s.GetSynopses()
	} else {
		syn := []string{}

		if v, ok := cmd.(VisiterAll); ok {
			var opts bool
			v.VisitAll(func(flag *flag.Flag) { opts = true })
			if opts {
				syn = append(syn, "[OPT..]")
			}
		}

		var didArgs bool
		if cmd != nil {
			argsField := reflect.ValueOf(cmd).Elem().FieldByName("Arguments")
			if argsField.IsValid() {
				argsI := argsField.Addr().Interface()
				syn = append(syn, positional.Usage(argsI))
				didArgs = true
			}
		}

		if !didArgs {
			var hasSub bool
			pkg := r.last().pkg
			for _, subcmd := range r.parser.listSubcommands(pkg) {
				if strings.HasPrefix(subcmd.pkg, pkg+"/") {
					hasSub = true
				}
			}
			if hasSub {
				syn = append(syn, "COMMAND..")
			}
		}

		synopses = []string{
			strings.Join(syn, " "),
		}
	}
	for _, s := range synopses {
		fmt.Fprintf(w, "  %s %s\n", r.name, s)
	}

	if v, ok := cmd.(VisiterAll); ok {
		var header bool
		v.VisitAll(func(flag *flag.Flag) {
			if !header {
				fmt.Fprintf(w, "\nOptions:\n")
				header = true
			}

			fmt.Fprintf(w, "  -%s=%s: %s\n", flag.Name, flag.DefValue, flag.Usage)
		})
	}

	subs := r.parser.listSubcommands(r.last().pkg)
	if len(subs) > 0 {
		fmt.Fprintf(w, "\nCommands:\n")

		// TODO if there's a real direct child, hide the deeper
		// subcommands that are under it

		// +1 for the slash
		dropPrefix := len(r.last().pkg) + 1

		wTab := tabwriter.NewWriter(w, 0, 0, 4, ' ', 0)
		for _, c := range subs {
			desc := ""
			if d, ok := c.cmd.(DescriptionGetter); ok {
				desc = d.GetDescription()
			}
			subcommand := strings.Replace(c.pkg[dropPrefix:], "/", " ", -1)
			if desc != "" {
				desc = "\t" + desc
			}
			fmt.Fprintf(wTab, "  %s%s\n", subcommand, desc)
		}
		wTab.Flush()
	}
}