Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
1f8230c9
Commit
1f8230c9
authored
9 years ago
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1263 from ipfs/stdin-parsing-optional-arg
Stdin parsing optional arg
parents
5c94a68a
23681727
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
2 deletions
+22
-2
commands/cli/parse.go
commands/cli/parse.go
+4
-2
commands/cli/parse_test.go
commands/cli/parse_test.go
+18
-0
No files found.
commands/cli/parse.go
View file @
1f8230c9
...
...
@@ -219,9 +219,11 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
}
}
// count number of values provided by user
// count number of values provided by user.
// if there is at least one ArgDef, we can safely trigger the inputs loop
// below to parse stdin.
numInputs
:=
len
(
inputs
)
if
stdin
!=
nil
{
if
len
(
argDefs
)
>
0
&&
argDefs
[
len
(
argDefs
)
-
1
]
.
SupportsStdin
&&
stdin
!=
nil
{
numInputs
+=
1
}
...
...
This diff is collapsed.
Click to expand it.
commands/cli/parse_test.go
View file @
1f8230c9
...
...
@@ -144,6 +144,12 @@ func TestArgumentParsing(t *testing.T) {
commands
.
StringArg
(
"b"
,
false
,
true
,
"another arg"
),
},
},
"optionalsecond"
:
{
Arguments
:
[]
commands
.
Argument
{
commands
.
StringArg
(
"a"
,
true
,
false
,
"some arg"
),
commands
.
StringArg
(
"b"
,
false
,
false
,
"another arg"
),
},
},
"reversedoptional"
:
{
Arguments
:
[]
commands
.
Argument
{
commands
.
StringArg
(
"a"
,
false
,
false
,
"some arg"
),
...
...
@@ -213,6 +219,12 @@ func TestArgumentParsing(t *testing.T) {
test
([]
string
{
"optional"
,
"value!"
},
nil
,
[]
string
{
"value!"
})
test
([]
string
{
"optional"
},
nil
,
[]
string
{})
test
([]
string
{
"optional"
,
"value1"
,
"value2"
},
nil
,
[]
string
{
"value1"
,
"value2"
})
test
([]
string
{
"optionalsecond"
,
"value!"
},
nil
,
[]
string
{
"value!"
})
test
([]
string
{
"optionalsecond"
,
"value1"
,
"value2"
},
nil
,
[]
string
{
"value1"
,
"value2"
})
testFail
([]
string
{
"optionalsecond"
},
"didn't provide any args, 1 required"
)
testFail
([]
string
{
"optionalsecond"
,
"value1"
,
"value2"
,
"value3"
},
"provided too many args, takes 2 maximum"
)
test
([]
string
{
"reversedoptional"
,
"value1"
,
"value2"
},
nil
,
[]
string
{
"value1"
,
"value2"
})
test
([]
string
{
"reversedoptional"
,
"value!"
},
nil
,
[]
string
{
"value!"
})
...
...
@@ -268,4 +280,10 @@ func TestArgumentParsing(t *testing.T) {
fstdin
=
fileToSimulateStdin
(
t
,
"stdin1"
)
test
([]
string
{
"stdinenablednotvariadic2args"
,
"value1"
},
fstdin
,
[]
string
{
"value1"
,
"stdin1"
})
test
([]
string
{
"stdinenablednotvariadic2args"
,
"value1"
,
"value2"
},
fstdin
,
[]
string
{
"value1"
,
"value2"
})
fstdin
=
fileToSimulateStdin
(
t
,
"stdin1"
)
test
([]
string
{
"noarg"
},
fstdin
,
[]
string
{})
fstdin
=
fileToSimulateStdin
(
t
,
"stdin1"
)
test
([]
string
{
"optionalsecond"
,
"value1"
,
"value2"
},
fstdin
,
[]
string
{
"value1"
,
"value2"
})
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment