@cryptotaxi247 / kubo / commits / 2b6b6fac0

commands: Added a flag to enable stdin arguments

Matt Bell committed Nov 14, 2014 at 02:22 UTC 2b6b6fac00197323f172bd07e1e928f05f1f318d
2 files changed +13 -7
commands/argument.go
+11 -5
@@ -8,11 +8,12 @@ const (
8 )
9
10 type Argument struct {
11 - Name string
12 - Type ArgumentType
13 - Required bool
14 - Variadic bool
15 - Description string
11 + Name string
12 + Type ArgumentType
13 + Required bool
14 + Variadic bool
15 + SupportsStdin bool
16 + Description string
17 }
18
19 func StringArg(name string, required, variadic bool, description string) Argument {
@@ -34,3 +35,8 @@ func FileArg(name string, required, variadic bool, description string) Argument
35 Description: description,
36 }
37 }
38 +
39 +func (a Argument) EnableStdin() Argument {
40 + a.SupportsStdin = true
41 + return a
42 +}
commands/cli/parse.go
+2 -2
@@ -171,7 +171,7 @@ func parseArgs(stringArgs []string, stdin *os.File, arguments []cmds.Argument) (
171 args = append(args, stringArgs[0])
172 stringArgs = stringArgs[1:]
173
174 - } else {
174 + } else if argDef.SupportsStdin {
175 // if we have a stdin, read it in and use the data as a string value
176 var buf bytes.Buffer
177 _, err := buf.ReadFrom(stdin)
@@ -192,7 +192,7 @@ func parseArgs(stringArgs []string, stdin *os.File, arguments []cmds.Argument) (
192 args = append(args, file)
193 stringArgs = stringArgs[1:]
194
195 - } else {
195 + } else if argDef.SupportsStdin {
196 // if we have a stdin, use that as a reader
197 args = append(args, stdin)
198 stdin = nil