From 23681727e04e652b7f67f8bfa94065209a8b63f3 Mon Sep 17 00:00:00 2001
From: Christian Couder <chriscool@tuxfamily.org>
Date: Thu, 21 May 2015 00:30:08 +0200
Subject: [PATCH] parse: fix parsing optional arg with stdin

License: MIT
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 commands/cli/parse.go      | 2 +-
 commands/cli/parse_test.go | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/commands/cli/parse.go b/commands/cli/parse.go
index 11003335e..0b99fd499 100644
--- a/commands/cli/parse.go
+++ b/commands/cli/parse.go
@@ -223,7 +223,7 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
 	// if there is at least one ArgDef, we can safely trigger the inputs loop
 	// below to parse stdin.
 	numInputs := len(inputs)
-	if len(argDefs) > 0 && stdin != nil {
+	if len(argDefs) > 0 && argDefs[len(argDefs)-1].SupportsStdin && stdin != nil {
 		numInputs += 1
 	}
 
diff --git a/commands/cli/parse_test.go b/commands/cli/parse_test.go
index 2c42ddcc4..c6215299b 100644
--- a/commands/cli/parse_test.go
+++ b/commands/cli/parse_test.go
@@ -283,4 +283,7 @@ func TestArgumentParsing(t *testing.T) {
 
 	fstdin = fileToSimulateStdin(t, "stdin1")
 	test([]string{"noarg"}, fstdin, []string{})
+
+	fstdin = fileToSimulateStdin(t, "stdin1")
+	test([]string{"optionalsecond", "value1", "value2"}, fstdin, []string{"value1", "value2"})
 }
-- 
GitLab