Codegangsta/cli is a Go package library for building CLI applications. It provides a framework for easily parsing command-line arguments and options, and generating help text for your command-line application. The package includes a variety of useful tools, such as support for subcommands, flags, and app-generated completions.
Context and Duration are two types that are commonly used with Codegangsta/cli applications. Context represents the context of a particular command execution, including the input arguments, flags, and any subcommand that was invoked. Duration represents a duration of time, and is often used for controlling timeouts or other time-based operations in CLI apps.
Here are some example code snippets that demonstrate the use of Context and Duration with Codegangsta/cli:
1. Using Context:
// Import the cli and context packages import ( "github.com/codegangsta/cli" "golang.org/x/net/context" )
// Define a command that uses the context.Context type app := cli.NewApp() app.Commands = []cli.Command{ { Name: "mycommand", Usage: "Run mycommand", Flags: []cli.Flag{ cli.IntFlag{Name: "count, c", Value: 1, Usage: "Number of times to run"}, }, Action: func(c *cli.Context) error { ctx := context.Background() count := c.Int("count") for i := 0; i < count; i++ { // Do something with the context } return nil }, }, }
In this example, we create a command called "mycommand" that takes an optional "--count" flag. Inside the command's Action function, we create a context.Background() which represents the context of the command execution. We then use this context to perform some action based on the input arguments, in this case running the command a specified number of times.
2. Using Duration:
// Import the cli and time packages import ( "github.com/codegangsta/cli" "time" )
// Define a command that uses the time.Duration type app := cli.NewApp() app.Commands = []cli.Command{ { Name: "timeout", Usage: "Run timeout", Flags: []cli.Flag{ cli.DurationFlag{Name: "t, timeout", Value: 5 * time.Second, Usage: "Timeout duration"}, }, Action: func(c *cli.Context) error { timeout := c.Duration("timeout") // Do something with the timeout duration return nil }, }, }
In this example, we create a command called "timeout" that takes an optional "--timeout" flag. Inside the command's Action function, we retrieve the value of the flag as a time.Duration type using c.Duration("timeout"). We can then use this duration to control a timeout or other time-based operation.
Golang Context.Duration - 30 examples found. These are the top rated real world Golang examples of github.com/codegangsta/cli.Context.Duration extracted from open source projects. You can rate examples to help us improve the quality of examples.