本文整理汇总了Golang中github.com/jrperritt/rack/internal/github.com/codegangsta/cli.NewApp函数的典型用法代码示例。如果您正苦于以下问题:Golang NewApp函数的具体用法?Golang NewApp怎么用?Golang NewApp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewApp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ExampleAppHelp
func ExampleAppHelp() {
// set args for examples sake
os.Args = []string{"greet", "h", "describeit"}
app := cli.NewApp()
app.Name = "greet"
app.Flags = []cli.Flag{
cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
}
app.Commands = []cli.Command{
{
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
Action: func(c *cli.Context) {
fmt.Printf("i like to describe things")
},
},
}
app.Run(os.Args)
// Output:
// NAME:
// describeit - use it to see a description
//
// USAGE:
// command describeit [arguments...]
//
// DESCRIPTION:
// This is how we describe describeit the function
}
示例2: TestApp_DefaultStdout
func TestApp_DefaultStdout(t *testing.T) {
app := cli.NewApp()
if app.Writer != os.Stdout {
t.Error("Default output writer not set.")
}
}
示例3: TestUploadHandleFlags
func TestUploadHandleFlags(t *testing.T) {
app := cli.NewApp()
flagset := flag.NewFlagSet("flags", 1)
flagset.String("name", "", "")
flagset.String("file", "", "")
flagset.String("public-key", "", "")
flagset.Set("name", "keypair1Name")
flagset.Set("public-key", "ssh public key data here")
c := cli.NewContext(app, flagset, nil)
cmd := &commandUpload{
Ctx: &handler.Context{
CLIContext: c,
},
}
expected := &handler.Resource{
Params: ¶msUpload{
opts: &osKeypairs.CreateOpts{
Name: "keypair1Name",
PublicKey: "ssh public key data here",
},
},
}
actual := &handler.Resource{}
err := cmd.HandleFlags(actual)
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, *expected.Params.(*paramsUpload).opts, *actual.Params.(*paramsUpload).opts)
}
示例4: ExampleAppBashComplete
func ExampleAppBashComplete() {
// set args for examples sake
os.Args = []string{"greet", "--generate-bash-completion"}
app := cli.NewApp()
app.Name = "greet"
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
Action: func(c *cli.Context) {
fmt.Printf("i like to describe things")
},
}, {
Name: "next",
Usage: "next example",
Description: "more stuff to see when generating bash completion",
Action: func(c *cli.Context) {
fmt.Printf("the next example")
},
},
}
app.Run(os.Args)
// Output:
// describeit
// d
// next
// help
// h
}
示例5: TestCreateHandleFlags
func TestCreateHandleFlags(t *testing.T) {
app := cli.NewApp()
flagset := flag.NewFlagSet("flags", 1)
flagset.String("metadata", "", "")
flagset.String("container-read", "", "")
flagset.String("container-write", "", "")
flagset.Set("metadata", "key=val,foo=bar")
c := cli.NewContext(app, flagset, nil)
cmd := &commandCreate{
Ctx: &handler.Context{
CLIContext: c,
},
}
expected := &handler.Resource{
Params: ¶msCreate{
opts: containers.CreateOpts{
Metadata: map[string]string{
"key": "val",
"foo": "bar",
},
},
},
}
actual := &handler.Resource{}
err := cmd.HandleFlags(actual)
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, expected.Params.(*paramsCreate).opts, actual.Params.(*paramsCreate).opts)
}
示例6: Example
func Example() {
app := cli.NewApp()
app.Name = "todo"
app.Usage = "task list on the command line"
app.Commands = []cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "add a task to the list",
Action: func(c *cli.Context) {
println("added task: ", c.Args().First())
},
},
{
Name: "complete",
Aliases: []string{"c"},
Usage: "complete a task on the list",
Action: func(c *cli.Context) {
println("completed task: ", c.Args().First())
},
},
}
app.Run(os.Args)
}
示例7: TestGenerateHandleSingle
func TestGenerateHandleSingle(t *testing.T) {
app := cli.NewApp()
flagset := flag.NewFlagSet("flags", 1)
flagset.String("name", "", "")
flagset.Set("name", "keypair1Name")
c := cli.NewContext(app, flagset, nil)
cmd := &commandGenerate{
Ctx: &handler.Context{
CLIContext: c,
},
}
expected := &handler.Resource{
Params: ¶msGenerate{
opts: &osKeypairs.CreateOpts{
Name: "keypair1Name",
},
},
}
actual := &handler.Resource{
Params: ¶msGenerate{
opts: &osKeypairs.CreateOpts{},
},
}
err := cmd.HandleSingle(actual)
th.AssertNoErr(t, err)
th.AssertEquals(t, *expected.Params.(*paramsGenerate).opts, *actual.Params.(*paramsGenerate).opts)
}
示例8: TestGetHandleSingle
func TestGetHandleSingle(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, `{"servers":[{"ID":"server1","Name":"server1Name"}]}`)
})
app := cli.NewApp()
flagset := flag.NewFlagSet("flags", 1)
flagset.String("name", "", "")
flagset.Set("name", "server1Name")
c := cli.NewContext(app, flagset, nil)
cmd := &commandGet{
Ctx: &handler.Context{
CLIContext: c,
ServiceClient: client.ServiceClient(),
},
}
expected := &handler.Resource{
Params: ¶msGet{
server: "server1",
},
}
actual := &handler.Resource{
Params: ¶msGet{},
}
err := cmd.HandleSingle(actual)
th.AssertNoErr(t, err)
th.AssertEquals(t, expected.Params.(*paramsGet).server, actual.Params.(*paramsGet).server)
}
示例9: TestListHandleFlags
func TestListHandleFlags(t *testing.T) {
app := cli.NewApp()
flagset := flag.NewFlagSet("flags", 1)
flagset.String("image", "", "")
flagset.String("flavor", "", "")
flagset.String("name", "", "")
flagset.String("status", "", "")
flagset.String("marker", "", "")
flagset.Set("image", "13ba-75c0-4483-acf9")
flagset.Set("flavor", "1234-b95f-ac5b-cd23")
flagset.Set("name", "server*")
flagset.Set("status", "AVAILABLE")
flagset.Set("marker", "1fd3-4f9f-44df-1b5c")
c := cli.NewContext(app, flagset, nil)
cmd := &commandList{
Ctx: &handler.Context{
CLIContext: c,
},
}
expected := &handler.Resource{
Params: ¶msList{
opts: &osServers.ListOpts{
Image: "13ba-75c0-4483-acf9",
Flavor: "1234-b95f-ac5b-cd23",
Name: "server*",
Status: "AVAILABLE",
Marker: "1fd3-4f9f-44df-1b5c",
},
},
}
actual := &handler.Resource{}
err := cmd.HandleFlags(actual)
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, *expected.Params.(*paramsList).opts, *actual.Params.(*paramsList).opts)
}
示例10: TestGenerateExecute
func TestGenerateExecute(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/os-keypairs", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, `{"keypair":{}}`)
})
app := cli.NewApp()
flagset := flag.NewFlagSet("flags", 1)
flagset.Bool("json", false, "")
c := cli.NewContext(app, flagset, nil)
cmd := &commandGenerate{
Ctx: &handler.Context{
CLIContext: c,
ServiceClient: client.ServiceClient(),
},
}
actual := &handler.Resource{
Params: ¶msGenerate{
opts: &osKeypairs.CreateOpts{
Name: "keypair1Name",
},
},
}
cmd.Execute(actual)
th.AssertNoErr(t, actual.Err)
}
示例11: TestApp_Run_Version
func TestApp_Run_Version(t *testing.T) {
var versionArguments = [][]string{{"boom", "--version"}, {"boom", "-v"}}
for _, args := range versionArguments {
buf := new(bytes.Buffer)
t.Logf("==> checking with arguments %v", args)
app := cli.NewApp()
app.Name = "boom"
app.Usage = "make an explosive entrance"
app.Version = "0.1.0"
app.Writer = buf
app.Action = func(c *cli.Context) {
buf.WriteString("boom I say!")
}
err := app.Run(args)
if err != nil {
t.Error(err)
}
output := buf.String()
t.Logf("output: %q\n", buf.Bytes())
if !strings.Contains(output, "0.1.0") {
t.Errorf("want version to contain %q, did not: \n%q", "0.1.0", output)
}
}
}
示例12: TestDeleteExecute
func TestDeleteExecute(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/container1", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
app := cli.NewApp()
flagset := flag.NewFlagSet("flags", 1)
flagset.String("name", "", "")
flagset.Set("name", "container1")
c := cli.NewContext(app, flagset, nil)
cmd := &commandDelete{
Ctx: &handler.Context{
ServiceClient: client.ServiceClient(),
CLIContext: c,
},
}
actual := &handler.Resource{
Params: ¶msDelete{
container: "container1",
},
}
cmd.Execute(actual)
th.AssertNoErr(t, actual.Err)
}
示例13: TestApp_ParseSliceFlagsWithMissingValue
func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
var parsedIntSlice []int
var parsedStringSlice []string
app := cli.NewApp()
command := cli.Command{
Name: "cmd",
Flags: []cli.Flag{
cli.IntSliceFlag{Name: "a", Usage: "set numbers"},
cli.StringSliceFlag{Name: "str", Usage: "set strings"},
},
Action: func(c *cli.Context) {
parsedIntSlice = c.IntSlice("a")
parsedStringSlice = c.StringSlice("str")
},
}
app.Commands = []cli.Command{command}
app.Run([]string{"", "cmd", "my-arg", "-a", "2", "-str", "A"})
var expectedIntSlice = []int{2}
var expectedStringSlice = []string{"A"}
if parsedIntSlice[0] != expectedIntSlice[0] {
t.Errorf("%v does not match %v", parsedIntSlice[0], expectedIntSlice[0])
}
if parsedStringSlice[0] != expectedStringSlice[0] {
t.Errorf("%v does not match %v", parsedIntSlice[0], expectedIntSlice[0])
}
}
示例14: TestApp_RunAsSubcommandParseFlags
func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
var context *cli.Context
a := cli.NewApp()
a.Commands = []cli.Command{
{
Name: "foo",
Action: func(c *cli.Context) {
context = c
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "lang",
Value: "english",
Usage: "language for the greeting",
},
},
Before: func(_ *cli.Context) error { return nil },
},
}
a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})
expect(t, context.Args().Get(0), "abcd")
expect(t, context.String("lang"), "spanish")
}
示例15: main
func main() {
cli.HelpPrinter = printHelp
cli.CommandHelpTemplate = `NAME: {{.Name}} - {{.Usage}}{{if .Description}}
DESCRIPTION: {{.Description}}{{end}}{{if .Flags}}
OPTIONS:
{{range .Flags}}{{flag .}}
{{end}}{{ end }}
`
app := cli.NewApp()
app.Name = "rack"
app.Usage = "An opinionated CLI for the Rackspace cloud"
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "init",
Usage: "[Linux/OS X only] Setup environment with command completion for the Bash shell.",
Action: setup.Init,
},
{
Name: "configure",
Usage: "Interactively create a config file for Rackspace authentication.",
Action: configure,
},
{
Name: "servers",
Usage: "Operations on cloud servers, both virtual and bare metal.",
Subcommands: serverscommands.Get(),
},
{
Name: "files",
Usage: "Object storage for files and media.",
Subcommands: filescommands.Get(),
},
{
Name: "networks",
Usage: "Software-defined networking.",
Subcommands: networkscommands.Get(),
},
{
Name: "block-storage",
Usage: "Block-level storage, exposed as volumes to mount to host servers. Work with volumes and their associated snapshots.",
Subcommands: blockstoragecommands.Get(),
},
}
app.Flags = util.GlobalFlags()
app.BashComplete = func(c *cli.Context) {
completeGlobals(globalOptions(app))
}
app.Before = func(c *cli.Context) error {
//fmt.Printf("c.Args: %+v\n", c.Args())
return nil
}
app.CommandNotFound = commandNotFound
app.Run(os.Args)
}