-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement go-arg for parsing in main (#73)
this implements go-arg and argv to handle argument parsing from the command line and go-arg in each command to handle any command specific arguments passed to it.
- Loading branch information
Showing
19 changed files
with
409 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/alexflint/go-arg" | ||
) | ||
|
||
func parseCommandArgs(args []string, cmd Command) (*arg.Parser, error) { | ||
p, err := argParser(args, cmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(args) > 1 { | ||
err = p.Parse(args[1:]) | ||
} else { | ||
err = p.Parse([]string{}) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
return p, nil | ||
} | ||
|
||
func argParser(args []string, cmd Command) (*arg.Parser, error) { | ||
return arg.NewParser(arg.Config{Program: args[0]}, cmd.GetArgs()) | ||
} | ||
|
||
// Usage returns usage information | ||
func Usage(cmd Command) string { | ||
var b bytes.Buffer | ||
p, _ := argParser([]string{cmd.GetName()}, cmd) | ||
p.WriteUsage(&b) | ||
return b.String() | ||
} | ||
|
||
// Help returns extended usage information | ||
func Help(cmd Command) string { | ||
var b bytes.Buffer | ||
p, _ := argParser([]string{cmd.GetName()}, cmd) | ||
p.WriteHelp(&b) | ||
return b.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.