aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/codegangsta/cli/command.go
blob: b61691c86551ca7278f844c0568610c3c92e43da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cli

import (
    "fmt"
    "io/ioutil"
    "strings"
)

// Command is a subcommand for a cli.App.
type Command struct {
    // The name of the command
    Name string
    // short name of the command. Typically one character (deprecated, use `Aliases`)
    ShortName string
    // A list of aliases for the command
    Aliases []string
    // A short description of the usage of this command
    Usage string
    // A longer explanation of how the command works
    Description string
    // The function to call when checking for bash command completions
    BashComplete func(context *Context)
    // An action to execute before any sub-subcommands are run, but after the context is ready
    // If a non-nil error is returned, no sub-subcommands are run
    Before func(context *Context) error
    // An action to execute after any subcommands are run, but after the subcommand has finished
    // It is run even if Action() panics
    After func(context *Context) error
    // The function to call when this command is invoked
    Action func(context *Context)
    // List of child commands
    Subcommands []Command
    // List of flags to parse
    Flags []Flag
    // Treat all flags as normal arguments if true
    SkipFlagParsing bool
    // Boolean to hide built-in help command
    HideHelp bool
}

// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
func (c Command) Run(ctx *Context) error {

    if len(c.Subcommands) > 0 || c.Before != nil || c.After != nil {
        return c.startApp(ctx)
    }

    if !c.HideHelp && (HelpFlag != BoolFlag{}) {
        // append help to flags
        c.Flags = append(
            c.Flags,
            HelpFlag,
        )
    }

    if ctx.App.EnableBashCompletion {
        c.Flags = append(c.Flags, BashCompletionFlag)
    }

    set := flagSet(c.Name, c.Flags)
    set.SetOutput(ioutil.Discard)

    firstFlagIndex := -1
    terminatorIndex := -1
    for index, arg := range ctx.Args() {
        if arg == "--" {
            terminatorIndex = index
            break
        } else if strings.HasPrefix(arg, "-") && firstFlagIndex == -1 {
            firstFlagIndex = index
        }
    }

    var err error
    if firstFlagIndex > -1 && !c.SkipFlagParsing {
        args := ctx.Args()
        regularArgs := make([]string, len(args[1:firstFlagIndex]))
        copy(regularArgs, args[1:firstFlagIndex])

        var flagArgs []string
        if terminatorIndex > -1 {
            flagArgs = args[firstFlagIndex:terminatorIndex]
            regularArgs = append(regularArgs, args[terminatorIndex:]...)
        } else {
            flagArgs = args[firstFlagIndex:]
        }

        err = set.Parse(append(flagArgs, regularArgs...))
    } else {
        err = set.Parse(ctx.Args().Tail())
    }

    if err != nil {
        fmt.Fprint(ctx.App.Writer, "Incorrect Usage.\n\n")
        ShowCommandHelp(ctx, c.Name)
        fmt.Fprintln(ctx.App.Writer)
        return err
    }

    nerr := normalizeFlags(c.Flags, set)
    if nerr != nil {
        fmt.Fprintln(ctx.App.Writer, nerr)
        fmt.Fprintln(ctx.App.Writer)
        ShowCommandHelp(ctx, c.Name)
        fmt.Fprintln(ctx.App.Writer)
        return nerr
    }
    context := NewContext(ctx.App, set, ctx.globalSet)

    if checkCommandCompletions(context, c.Name) {
        return nil
    }

    if checkCommandHelp(context, c.Name) {
        return nil
    }
    context.Command = c
    c.Action(context)
    return nil
}

func (c Command) Names() []string {
    names := []string{c.Name}

    if c.ShortName != "" {
        names = append(names, c.ShortName)
    }

    return append(names, c.Aliases...)
}

// Returns true if Command.Name or Command.ShortName matches given name
func (c Command) HasName(name string) bool {
    for _, n := range c.Names() {
        if n == name {
            return true
        }
    }
    return false
}

func (c Command) startApp(ctx *Context) error {
    app := NewApp()

    // set the name and usage
    app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name)
    if c.Description != "" {
        app.Usage = c.Description
    } else {
        app.Usage = c.Usage
    }

    // set CommandNotFound
    app.CommandNotFound = ctx.App.CommandNotFound

    // set the flags and commands
    app.Commands = c.Subcommands
    app.Flags = c.Flags
    app.HideHelp = c.HideHelp

    // bash completion
    app.EnableBashCompletion = ctx.App.EnableBashCompletion
    if c.BashComplete != nil {
        app.BashComplete = c.BashComplete
    }

    // set the actions
    app.Before = c.Before
    app.After = c.After
    if c.Action != nil {
        app.Action = c.Action
    } else {
        app.Action = helpSubcommand.Action
    }

    return app.RunAsSubcommand(ctx)
}