Fix parsing arguments for `browse` command

After `browse` was indicated as "GitExtension: false", it now gets
automatically parsed by pflag library. That has an adverse effect that
it loses information about the `--` value being passed, since pflag
treats it as terminator value and swallows it silently.

Now, introduce the `args.Terminator` property to check if terminator was
present or not, and use that in `browse` to restore previous behavior.
This commit is contained in:
Mislav Marohnić 2015-06-05 01:01:02 +02:00
Родитель a287206616
Коммит d45511c7aa
3 изменённых файлов: 20 добавлений и 19 удалений

Просмотреть файл

@ -18,6 +18,7 @@ type Args struct {
beforeChain []*cmd.Cmd
afterChain []*cmd.Cmd
Noop bool
Terminator bool
}
func (a *Args) Words() []string {

Просмотреть файл

@ -10,10 +10,9 @@ import (
)
var cmdBrowse = &Command{
Run: browse,
GitExtension: false,
Usage: "browse [-u] [[<USER>/]<REPOSITORY>|--] [SUBPAGE]",
Short: "Open a GitHub page in the default browser",
Run: browse,
Usage: "browse [-u] [[<USER>/]<REPOSITORY>|--] [SUBPAGE]",
Short: "Open a GitHub page in the default browser",
Long: `Open repository's GitHub page in the system's default web browser using
"open(1)" or the "BROWSER" env variable. If the repository isn't
specified, "browse" opens the page of the repository found in the current
@ -23,7 +22,13 @@ subpage: one of "wiki", "commits", "issues" or other (the default is
`,
}
var (
flagBrowseURLOnly bool
)
func init() {
cmdBrowse.Flag.BoolVarP(&flagBrowseURLOnly, "url-only", "u", false, "URL")
CmdRunner.Use(cmdBrowse)
}
@ -53,20 +58,19 @@ func browse(command *Command, args *Args) {
err error
)
flagBrowseURLOnly := parseFlagBrowseURLOnly(args)
if !args.IsParamsEmpty() {
dest = args.RemoveParam(0)
}
if dest == "--" {
dest = ""
}
if !args.IsParamsEmpty() {
subpage = args.RemoveParam(0)
}
if args.Terminator {
subpage = dest
dest = ""
}
localRepo, _ := github.LocalRepo()
if dest != "" {
project = github.NewProject("", dest, "")
@ -114,15 +118,6 @@ func browse(command *Command, args *Args) {
}
}
func parseFlagBrowseURLOnly(args *Args) bool {
if i := args.IndexOfParam("-u"); i != -1 {
args.RemoveParam(i)
return true
}
return false
}
func branchInURL(branch *github.Branch) string {
parts := strings.Split(branch.ShortName(), "/")
newPath := make([]string, len(parts))

Просмотреть файл

@ -54,6 +54,11 @@ func (c *Command) parseArguments(args *Args) (err error) {
c.Flag.Init(c.Name(), flag.ContinueOnError)
c.Flag.Usage = c.PrintUsage
if err = c.Flag.Parse(args.Params); err == nil {
for _, arg := range args.Params {
if arg == "--" {
args.Terminator = true
}
}
args.Params = c.Flag.Args()
}