@cryptotaxi247 / kubo / commits / 8570a5293

cli: refactor to expose argument parsing functionality

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Oct 15, 2016 at 19:38 UTC 8570a5293b589d0653fffc28919c93685068018d
2 files changed +31 -22
commands/cli/cmd_suggestion.go
+4
@@ -30,6 +30,10 @@ func (s suggestionSlice) Less(i, j int) bool {
30 }
31
32 func suggestUnknownCmd(args []string, root *cmds.Command) []string {
33 + if root == nil {
34 + return nil
35 + }
36 +
37 arg := args[0]
38 var suggestions []string
39 sortableSuggestions := make(suggestionSlice, 0)
commands/cli/parse.go
+27 -22
@@ -36,27 +36,6 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
36 return nil, cmd, path, err
37 }
38
39 - // if -r is provided, and it is associated with the package builtin
40 - // recursive path option, allow recursive file paths
41 - recursiveOpt := req.Option(cmds.RecShort)
42 - recursive := false
43 - if recursiveOpt != nil && recursiveOpt.Definition() == cmds.OptionRecursivePath {
44 - recursive, _, err = recursiveOpt.Bool()
45 - if err != nil {
46 - return req, nil, nil, u.ErrCast()
47 - }
48 - }
49 -
50 - // if '--hidden' is provided, enumerate hidden paths
51 - hiddenOpt := req.Option("hidden")
52 - hidden := false
53 - if hiddenOpt != nil {
54 - hidden, _, err = hiddenOpt.Bool()
55 - if err != nil {
56 - return req, nil, nil, u.ErrCast()
57 - }
58 - }
59 -
39 // This is an ugly hack to maintain our current CLI interface while fixing
40 // other stdin usage bugs. Let this serve as a warning, be careful about the
41 // 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
46 }
47 }
48
70 - stringArgs, fileArgs, err := parseArgs(stringVals, stdin, cmd.Arguments, recursive, hidden, root)
49 + stringArgs, fileArgs, err := ParseArgs(req, stringVals, stdin, cmd.Arguments, root)
50 if err != nil {
51 return req, cmd, path, err
52 }
@@ -86,6 +65,32 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
65 return req, cmd, path, nil
66 }
67
68 +func ParseArgs(req cmds.Request, inputs []string, stdin *os.File, argDefs []cmds.Argument, root *cmds.Command) ([]string, []files.File, error) {
69 + var err error
70 +
71 + // if -r is provided, and it is associated with the package builtin
72 + // recursive path option, allow recursive file paths
73 + recursiveOpt := req.Option(cmds.RecShort)
74 + recursive := false
75 + if recursiveOpt != nil && recursiveOpt.Definition() == cmds.OptionRecursivePath {
76 + recursive, _, err = recursiveOpt.Bool()
77 + if err != nil {
78 + return nil, nil, u.ErrCast()
79 + }
80 + }
81 +
82 + // if '--hidden' is provided, enumerate hidden paths
83 + hiddenOpt := req.Option("hidden")
84 + hidden := false
85 + if hiddenOpt != nil {
86 + hidden, _, err = hiddenOpt.Bool()
87 + if err != nil {
88 + return nil, nil, u.ErrCast()
89 + }
90 + }
91 + return parseArgs(inputs, stdin, argDefs, recursive, hidden, root)
92 +}
93 +
94 // Parse a command line made up of sub-commands, short arguments, long arguments and positional arguments
95 func parseOpts(args []string, root *cmds.Command) (
96 path []string,