(ctx context.Context, osArgs []string)
| 97 | } |
| 98 | |
| 99 | func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context, deferErr error) { |
| 100 | tracef("running with arguments %[1]q (cmd=%[2]q)", osArgs, cmd.Name) |
| 101 | cmd.setupDefaults(osArgs) |
| 102 | |
| 103 | // Validate StopOnNthArg |
| 104 | if cmd.StopOnNthArg != nil && *cmd.StopOnNthArg < 0 { |
| 105 | return ctx, fmt.Errorf("StopOnNthArg must be non-negative, got %d", *cmd.StopOnNthArg) |
| 106 | } |
| 107 | |
| 108 | if v, ok := ctx.Value(commandContextKey).(*Command); ok { |
| 109 | tracef("setting parent (cmd=%[1]q) command from context.Context value (cmd=%[2]q)", v.Name, cmd.Name) |
| 110 | cmd.parent = v |
| 111 | } |
| 112 | |
| 113 | if cmd.parent == nil { |
| 114 | if cmd.ReadArgsFromStdin { |
| 115 | if args, err := cmd.parseArgsFromStdin(); err != nil { |
| 116 | return ctx, err |
| 117 | } else { |
| 118 | osArgs = append(osArgs, args...) |
| 119 | } |
| 120 | } |
| 121 | // handle the completion flag separately from the flagset since |
| 122 | // completion could be attempted after a flag, but before its value was put |
| 123 | // on the command line. this causes the flagset to interpret the completion |
| 124 | // flag name as the value of the flag before it which is undesirable |
| 125 | // note that we can only do this because the shell autocomplete function |
| 126 | // always appends the completion flag at the end of the command |
| 127 | tracef("checking osArgs %v (cmd=%[2]q)", osArgs, cmd.Name) |
| 128 | cmd.shellCompletion, osArgs = checkShellCompleteFlag(cmd, osArgs) |
| 129 | |
| 130 | tracef("setting cmd.shellCompletion=%[1]v from checkShellCompleteFlag (cmd=%[2]q)", cmd.shellCompletion && cmd.EnableShellCompletion, cmd.Name) |
| 131 | cmd.shellCompletion = cmd.EnableShellCompletion && cmd.shellCompletion |
| 132 | } |
| 133 | |
| 134 | tracef("using post-checkShellCompleteFlag arguments %[1]q (cmd=%[2]q)", osArgs, cmd.Name) |
| 135 | |
| 136 | tracef("setting self as cmd in context (cmd=%[1]q)", cmd.Name) |
| 137 | ctx = context.WithValue(ctx, commandContextKey, cmd) |
| 138 | |
| 139 | if cmd.parent == nil { |
| 140 | cmd.setupCommandGraph() |
| 141 | } |
| 142 | |
| 143 | var rargs Args = &stringSliceArgs{v: osArgs} |
| 144 | var args Args = &stringSliceArgs{rargs.Tail()} |
| 145 | |
| 146 | for _, f := range cmd.allFlags() { |
| 147 | if cmd.hasPersistentFlagOnAncestor(f) { |
| 148 | continue |
| 149 | } |
| 150 | if err := f.PreParse(); err != nil { |
| 151 | return ctx, err |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | var err error |
| 156 |
no test coverage detected