Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: refactor to expose argument parsing functionality #3308

Merged
merged 1 commit into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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