Skip to content

Commit

Permalink
cli: refactor to expose argument parsing functionality
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Oct 16, 2016
1 parent d5c716a commit 8570a52
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
4 changes: 4 additions & 0 deletions commands/cli/cmd_suggestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 27 additions & 22 deletions commands/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -67,7 +46,7 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
}
}

stringArgs, fileArgs, err := parseArgs(stringVals, stdin, cmd.Arguments, recursive, hidden, root)
stringArgs, fileArgs, err := ParseArgs(req, stringVals, stdin, cmd.Arguments, root)
if err != nil {
return req, cmd, path, err
}
Expand All @@ -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,
Expand Down

0 comments on commit 8570a52

Please sign in to comment.