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
8570a529
Commit
8570a529
authored
8 years ago
by
Kevin Atkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cli: refactor to expose argument parsing functionality
License: MIT Signed-off-by:
Kevin Atkinson
<
k@kevina.org
>
parent
d5c716a3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
22 deletions
+31
-22
commands/cli/cmd_suggestion.go
commands/cli/cmd_suggestion.go
+4
-0
commands/cli/parse.go
commands/cli/parse.go
+27
-22
No files found.
commands/cli/cmd_suggestion.go
View file @
8570a529
...
...
@@ -30,6 +30,10 @@ func (s suggestionSlice) Less(i, j int) bool {
}
func
suggestUnknownCmd
(
args
[]
string
,
root
*
cmds
.
Command
)
[]
string
{
if
root
==
nil
{
return
nil
}
arg
:=
args
[
0
]
var
suggestions
[]
string
sortableSuggestions
:=
make
(
suggestionSlice
,
0
)
...
...
This diff is collapsed.
Click to expand it.
commands/cli/parse.go
View file @
8570a529
...
...
@@ -36,27 +36,6 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
return
nil
,
cmd
,
path
,
err
}
// if -r is provided, and it is associated with the package builtin
// recursive path option, allow recursive file paths
recursiveOpt
:=
req
.
Option
(
cmds
.
RecShort
)
recursive
:=
false
if
recursiveOpt
!=
nil
&&
recursiveOpt
.
Definition
()
==
cmds
.
OptionRecursivePath
{
recursive
,
_
,
err
=
recursiveOpt
.
Bool
()
if
err
!=
nil
{
return
req
,
nil
,
nil
,
u
.
ErrCast
()
}
}
// if '--hidden' is provided, enumerate hidden paths
hiddenOpt
:=
req
.
Option
(
"hidden"
)
hidden
:=
false
if
hiddenOpt
!=
nil
{
hidden
,
_
,
err
=
hiddenOpt
.
Bool
()
if
err
!=
nil
{
return
req
,
nil
,
nil
,
u
.
ErrCast
()
}
}
// This is an ugly hack to maintain our current CLI interface while fixing
// other stdin usage bugs. Let this serve as a warning, be careful about the
// choices you make, they will haunt you forever.
...
...
@@ -67,7 +46,7 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
}
}
stringArgs
,
fileArgs
,
err
:=
p
arseArgs
(
stringVals
,
stdin
,
cmd
.
Arguments
,
recursive
,
hidden
,
root
)
stringArgs
,
fileArgs
,
err
:=
P
arseArgs
(
req
,
stringVals
,
stdin
,
cmd
.
Arguments
,
root
)
if
err
!=
nil
{
return
req
,
cmd
,
path
,
err
}
...
...
@@ -86,6 +65,32 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
return
req
,
cmd
,
path
,
nil
}
func
ParseArgs
(
req
cmds
.
Request
,
inputs
[]
string
,
stdin
*
os
.
File
,
argDefs
[]
cmds
.
Argument
,
root
*
cmds
.
Command
)
([]
string
,
[]
files
.
File
,
error
)
{
var
err
error
// if -r is provided, and it is associated with the package builtin
// recursive path option, allow recursive file paths
recursiveOpt
:=
req
.
Option
(
cmds
.
RecShort
)
recursive
:=
false
if
recursiveOpt
!=
nil
&&
recursiveOpt
.
Definition
()
==
cmds
.
OptionRecursivePath
{
recursive
,
_
,
err
=
recursiveOpt
.
Bool
()
if
err
!=
nil
{
return
nil
,
nil
,
u
.
ErrCast
()
}
}
// if '--hidden' is provided, enumerate hidden paths
hiddenOpt
:=
req
.
Option
(
"hidden"
)
hidden
:=
false
if
hiddenOpt
!=
nil
{
hidden
,
_
,
err
=
hiddenOpt
.
Bool
()
if
err
!=
nil
{
return
nil
,
nil
,
u
.
ErrCast
()
}
}
return
parseArgs
(
inputs
,
stdin
,
argDefs
,
recursive
,
hidden
,
root
)
}
// Parse a command line made up of sub-commands, short arguments, long arguments and positional arguments
func
parseOpts
(
args
[]
string
,
root
*
cmds
.
Command
)
(
path
[]
string
,
...
...
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