Documentation ¶
Index ¶
- func HasFlags(fs *flag.FlagSet) bool
- func JoinArguments(args []string) string
- func LicenseFormatString(licenses interface{}, format string, a ...interface{}) (string, error)
- func LicenseString(licenses interface{}) (string, error)
- func OptionsHelp(fs *flag.FlagSet) string
- func SplitArguments(argstr string) []string
- func SplitCommand(cmdstr string) []string
- func VersionString(opts Options) string
- type Command
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JoinArguments ¶ added in v0.10.0
JoinArguments is the counter operation for SplitArguments. See that for more details.
func LicenseFormatString ¶
LicenseString returns a string of the licenses printed out in the given format.
The licenses is expected to be of the following kind of type:
map[string]struct { Name string Text string }
The map key is the package name, Name is the license name, and Text is the license text.
The format string is similar to fmt's but with the following additional verbs: %Lp Package name %Ln License name %Lt License text
The licenses are printed in the alphabetical order of the package name.
As an implementation detail, https://github.com/kopoli/licrep generates a compatible map.
func LicenseString ¶
func OptionsHelp ¶ added in v0.12.0
OptionsHelp is similar to flag.PrintDefaults, with the following differences: - Returns a string instead of printing to flagset.Output() - If one character flag has the same Usage as a longer one, combine them. - Use tabwriter
func SplitArguments ¶ added in v0.10.0
SplitArguments splits the arguments from the option "cmdline-args". See Parse for details.
func SplitCommand ¶ added in v0.10.0
SplitCommand splits a command string to a command and its synonyms. See NewCommand for more information.
func VersionString ¶
VersionString returns a version string that should be printed with the -v or the --version flag. It gets the components from the following keys from the options: program-name program-version program-timestamp
Types ¶
type Command ¶ added in v0.10.0
type Command struct { Cmd []string // Help that describes command Help string // The sub-command portion of the Usage line in the help SubCommandHelp string // The argument portion of the Usage line in the help ArgumentHelp string Flags *flag.FlagSet // contains filtered or unexported fields }
func NewCommand ¶ added in v0.10.0
NewCommand creates a recursive command line argument with flags.
Parent of the top-level command should be nil. The cmd string can contain multiple space-separated commands that are regarded as synonyms of the command. The help string is displayed if help option is given.
If parent == nil, then the Usage function prints out all sub-commands with helps. This can be overridden by re-defining the Flags.Usage function.
Example:
opts := appkit.NewOptions() base := appkit.NewCommand(nil, "", "") optVersion := base.Flags.Bool("version", false, "Display version") add := appkit.NewCommand(base, "add a", "Adding stuff") _ = appkit.NewCommand(add, "package p", "Add package") _ = appkit.NewCommand(add, "dependency d", "Add dependency") del := appkit.NewCommand(base, "delete del d", "Deleting stuff") _ = appkit.NewCommand(del, "package p", "Delete package") _ = appkit.NewCommand(del, "dependency d", "Delete dependency") optRecurse := del.Flags.Bool("recurse", false, "Delete recursively") err = base.Parse(os.Args[1:], opts) if err == flag.ErrHelp { os.Exit(0) } if *optVersion { fmt.Println(appkit.VersionString(opts)) os.Exit(0) } cmd := opts.Get("cmdline-command", "") switch cmd { case "add package": ... case "delete package": ... }
func (*Command) CommandList ¶ added in v0.10.0
CommandList prints out a recursive tree of sub-commands to the given io.Writer.
func (*Command) FullCommandName ¶ added in v0.11.0
FullCommandName returns the full (sub-)command as a string
func (*Command) HasSubcommands ¶ added in v0.11.0
HasSubcommands returns true if a command has any sub-commands defined.
func (*Command) IsTopLevel ¶ added in v0.11.0
IsTopLevel returns true if the command has no parents
func (*Command) Parse ¶ added in v0.10.0
Parse the command line arguments according to the recursive command structure.
The actual command will be set to the option "cmdline-command" inside the opts structure. Additional positional arguments after the command are in the "cmdline-args" option.
Recursive commands in "cmdline-command" are space separated. If there are multiple synonyms defined for a command, the first one is listed.
The positional arguments in "cmdline-args" are NUL separated inside the string. They can be split to an array using SplitArguments.