cli

package
v0.0.0-...-36f19b9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2014 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseArgs

func ParseArgs(cxt cookoo.Context, params *cookoo.Params) (interface{}, cookoo.Interrupt)

Parse arguments for a "subcommand"

The cookoo.cli.RequestResolver allows you to specify global level flags. This command allows you to augment those with subcommand flags. Example:

$ myprog -foo=yes subcommand -bar=no

In the above example, `-foo` is a global flag (set before the subcommand), while `-bar` is a local flag. It is specific to `subcommand`. This command lets you parse an arguments list given a pointer to a `flag.FlagSet`.

Like the cookoo.cli.RequestResolver, this will place the parsed params directly into the context. For this reason, you ought not use the same flag names at both global and local flag levels. (The local will overwrite the global.)

Params:

  • args: (required) A slice of arguments. Typically, this is `cxt:args` as set by cookoo.cli.RequestResolver.
  • flagset: (required) A set if flags (see flag.FlagSet) to parse.

A slice of all non-flag arguments remaining after the parse are returned into the context.

For example, if ['-foo', 'bar', 'some', 'other', 'data'] is passed in, '-foo' and 'bar' will be parsed out, while ['some', 'other', 'data'] will be returned into the context. (Assuming, of course, that the flag definition for -foo exists, and is a type that accepts a value).

Thus, you will have `cxt:foo` available (with value `bar`) and everything else will be available in the slice under this command's context entry.

func RunSubcommand

func RunSubcommand(cxt cookoo.Context, params *cookoo.Params) (interface{}, cookoo.Interrupt)

Run a subcommand.

Params:

  • args: a string[] of arguments, like you get from os.Args. This will assume the first arg is a subcommand. If you have options, you should parse those out first with ParseArgs.
  • default: The default subcommand to run if none is found.
  • offset: By default, this assumes an os.Args, and looks up the item in os.Args[1]. You can override this behavior by setting offset to something else.
  • ignoreRoutes: A []string of routes that should not be executed.

func ShiftArgs

func ShiftArgs(c cookoo.Context, params *cookoo.Params) (interface{}, cookoo.Interrupt)

Shift the args N (default 1) times, returning the last shifted value.

Params: n: The number of times to shift. Only the last value is returned. args: The name of the context slice/array to modify. This value will be retrieved

from the context. Default: "os.Args"

func ShowHelp

func ShowHelp(cxt cookoo.Context, params *cookoo.Params) (interface{}, cookoo.Interrupt)

Show help. This command is useful for placing at the front of a CLI "subcommand" to have it output help information. It will only trigger when "show" is set to true, so another command can, for example, check for a "-h" or "-help" flag and set "show" based on that.

Params: - show (bool): If `true`, show help. - summary (string): A one-line summary of the command. - description (string): A short description of what the command does. - usage (string): usage information. - flags (FlagSet): Flags that are supported. The FlagSet will be converted to help text. - writer (Writer): The location that this will write to. Default is os.Stdout

Types

type RequestResolver

type RequestResolver struct {
	// contains filtered or unexported fields
}

A CLI-centered request resolver with support for command line flags.

When this resolver finds "globalFlags" in the base context, it attempts to parse out the flags from the `path` string.

You must specify arguments as a `flag.FlagSet` (see the Go documentation).

Parsed commanline arguments are put directly into the context (though later we may put them in a datasource instead). Currently, all parameter values -- even booleans -- are stored as strings. If it is important to you to store them as more complex types, you may need to use `FlagSet.*Var()` functions.

Splitting the path string into arguments is done naively by splitting the string on the space (%20) character.

Arguments are parsed when the request resolver is asked to resolve (no earlier). Thus the FlagSet may be placed into the context at any time prior to cookoo.Router.HandleRequest().

CONTEXT VALUES - globalFlags: A `flag.FlagSet`. If this is present and not empty, the path will be parsed.

func (*RequestResolver) Init

func (r *RequestResolver) Init(registry *cookoo.Registry)

func (*RequestResolver) Resolve

func (r *RequestResolver) Resolve(path string, cxt cookoo.Context) (string, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL