Skip to content

Commit

Permalink
ref #56 change cli library to ogier/pflag.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed May 17, 2019
1 parent 2ac4a93 commit 5d43e71
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 128 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/ogier/pflag"
version = "0.0.1"
12 changes: 5 additions & 7 deletions add/add_cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package add

import (
"flag"
"fmt"

"github.com/mitchellh/cli"
flag "github.com/ogier/pflag"
"github.com/tamada/rrh/common"
)

Expand All @@ -28,8 +28,8 @@ Help function shows the help message.
func (add *Command) Help() string {
return `rrh add [OPTIONS] <REPOSITORY_PATHS...>
OPTIONS
-g, --group <GROUP> add repository to RRH database.
-r, --repository-id <ID> specified repository id of the given repository path.
-g, --group=<GROUP> add repository to RRH database.
-r, --repository-id=<ID> specified repository id of the given repository path.
Specifying this option fails with multiple arguments.
ARGUMENTS
REPOSITORY_PATHS the local path list of the git repositories.`
Expand Down Expand Up @@ -92,10 +92,8 @@ func (add *Command) buildFlagSet(config *common.Config) (*flag.FlagSet, *options
var defaultGroup = config.GetValue(common.RrhDefaultGroupName)
flags := flag.NewFlagSet("add", flag.ContinueOnError)
flags.Usage = func() { fmt.Println(add.Help()) }
flags.StringVar(&opt.group, "g", defaultGroup, "target group")
flags.StringVar(&opt.group, "group", defaultGroup, "target group")
flags.StringVar(&opt.repoID, "r", "", "specifying repository id")
flags.StringVar(&opt.repoID, "repository-id", "", "specifying repository id")
flags.StringVarP(&opt.group, "group", "g", defaultGroup, "target group")
flags.StringVarP(&opt.repoID, "repository-id", "r", "", "specifying repository id")
return flags, &opt
}

Expand Down
10 changes: 5 additions & 5 deletions add/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func TestHelpAndSynopsis(t *testing.T) {
}
if command.Help() != `rrh add [OPTIONS] <REPOSITORY_PATHS...>
OPTIONS
-g, --group <GROUP> add repository to RRH database.
-r, --repository-id <ID> specified repository id of the given repository path.
-g, --group=<GROUP> add repository to RRH database.
-r, --repository-id=<ID> specified repository id of the given repository path.
Specifying this option fails with multiple arguments.
ARGUMENTS
REPOSITORY_PATHS the local path list of the git repositories.` {
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestAdd(t *testing.T) {
rCheckers []repositoryChecker
relCheckers []relationChecker
}{
{[]string{"--group", "group2", "../testdata/helloworld"}, 0,
{[]string{"--group=group2", "../testdata/helloworld"}, 0,
[]groupChecker{{"group2", true}},
[]repositoryChecker{{"helloworld", true}},
[]relationChecker{{"group2", "helloworld", true}},
Expand All @@ -74,12 +74,12 @@ func TestAdd(t *testing.T) {
[]repositoryChecker{{"helloworld", true}},
[]relationChecker{{"no-group", "helloworld", true}},
},
{[]string{"--repository-id", "hw", "../testdata/other/helloworld"}, 0,
{[]string{"--repository-id=hw", "../testdata/other/helloworld"}, 0,
[]groupChecker{},
[]repositoryChecker{{"hw", true}},
[]relationChecker{{"no-group", "hw", true}},
},
{[]string{"--repository-id", "fails", "../testdata/other/helloworld", "../testdata/fibonacci"}, 0,
{[]string{"--repository-id=fails", "../testdata/other/helloworld", "../testdata/fibonacci"}, 0,
[]groupChecker{},
[]repositoryChecker{},
[]relationChecker{},
Expand Down
15 changes: 6 additions & 9 deletions clone/clone_cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package clone

import (
"flag"
"fmt"

"github.com/mitchellh/cli"
flag "github.com/ogier/pflag"
"github.com/tamada/rrh/common"
)

Expand Down Expand Up @@ -34,8 +34,8 @@ Help function shows the help message.
func (clone *Command) Help() string {
return `rrh clone [OPTIONS] <REMOTE_REPOS...>
OPTIONS
-g, --group <GROUP> print managed repositories categorized in the group.
-d, --dest <DEST> specify the destination.
-g, --group=<GROUP> print managed repositories categorized in the group.
-d, --dest=<DEST> specify the destination.
-v, --verbose verbose mode.
ARGUMENTS
REMOTE_REPOS repository urls`
Expand Down Expand Up @@ -109,12 +109,9 @@ func (clone *Command) buildFlagSets(config *common.Config) (*flag.FlagSet, *opti
var options = options{defaultGroup, ".", false}
flags := flag.NewFlagSet("clone", flag.ContinueOnError)
flags.Usage = func() { fmt.Println(clone.Help()) }
flags.StringVar(&options.group, "g", defaultGroup, "belonging group")
flags.StringVar(&options.group, "group", defaultGroup, "belonging group")
flags.StringVar(&options.dest, "d", destination, "destination")
flags.StringVar(&options.dest, "dest", destination, "destination")
flags.BoolVar(&options.verbose, "v", false, "verbose mode")
flags.BoolVar(&options.verbose, "verbose", false, "verbose mode")
flags.StringVarP(&options.group, "group", "g", defaultGroup, "belonging group")
flags.StringVarP(&options.dest, "dest", "d", destination, "destination")
flags.BoolVarP(&options.verbose, "verbose", "v", false, "verbose mode")
return flags, &options
}

Expand Down
6 changes: 3 additions & 3 deletions clone/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestCommand_Run(t *testing.T) {
func TestCommand_SpecifyingId(t *testing.T) {
var dbFile = common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var clone, _ = CommandFactory()
clone.Run([]string{"-d", "../testdata/newid", "../testdata/helloworld"})
clone.Run([]string{"--dest=../testdata/newid", "../testdata/helloworld"})
defer cleanup([]string{"../testdata/newid"})

var config = common.OpenConfig()
Expand Down Expand Up @@ -143,8 +143,8 @@ func TestCloneNotGitRepository(t *testing.T) {
func TestHelpAndSynopsis(t *testing.T) {
var helpMessage = `rrh clone [OPTIONS] <REMOTE_REPOS...>
OPTIONS
-g, --group <GROUP> print managed repositories categorized in the group.
-d, --dest <DEST> specify the destination.
-g, --group=<GROUP> print managed repositories categorized in the group.
-d, --dest=<DEST> specify the destination.
-v, --verbose verbose mode.
ARGUMENTS
REMOTE_REPOS repository urls`
Expand Down
2 changes: 1 addition & 1 deletion export/export_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package export
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/mitchellh/cli"
"github.com/mitchellh/go-homedir"
flag "github.com/ogier/pflag"
"github.com/tamada/rrh/common"
)

Expand Down
5 changes: 2 additions & 3 deletions export/import_cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package export

import (
"flag"
"fmt"

"github.com/mitchellh/cli"
flag "github.com/ogier/pflag"
"github.com/tamada/rrh/common"
)

Expand Down Expand Up @@ -82,8 +82,7 @@ func buildFlagSet(command *ImportCommand) (*flag.FlagSet, *importOptions) {
flags.Usage = func() { fmt.Println(command.Help()) }
flags.BoolVar(&options.overwrite, "overwrite", false, "overwrite mode")
flags.BoolVar(&options.autoClone, "auto-clone", false, "auto clone mode")
flags.BoolVar(&options.verbose, "v", false, "verbose mode")
flags.BoolVar(&options.verbose, "verbose", false, "verbose mode")
flags.BoolVarP(&options.verbose, "verbose", "v", false, "verbose mode")
return flags, &options
}

Expand Down
4 changes: 2 additions & 2 deletions export/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ func TestParsingFailOfArgs(t *testing.T) {
}{
{[]string{}, "too few arguments"},
{[]string{"a", "b", "c"}, "too many arguments: [a b c]"},
{[]string{"-unknown-option"}, `rrh import [OPTIONS] <DATABASE_JSON>
{[]string{"--unknown-option"}, `rrh import [OPTIONS] <DATABASE_JSON>
OPTIONS
--auto-clone clone the repository, if paths do not exist.
--overwrite replace the local RRH database to the given database.
-v, --verbose verbose mode.
ARGUMENTS
DATABASE_JSON the exported RRH database.
flag provided but not defined: -unknown-option`},
unknown flag: --unknown-option`},
}

for _, testcase := range testcases {
Expand Down
6 changes: 3 additions & 3 deletions fetch/fetch_all_cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package fetch

import (
"flag"
"fmt"

"github.com/mitchellh/cli"
flag "github.com/ogier/pflag"
"github.com/tamada/rrh/common"
)

Expand All @@ -26,7 +26,7 @@ Help returns the help message.
func (fetchAll *AllCommand) Help() string {
return `rrh fetch-all [OPTIONS]
OPTIONS
-r, --remote <REMOTE> specify the remote name. Default is "origin."`
-r, --remote=<REMOTE> specify the remote name. Default is "origin."`
}

func (fetchAll *AllCommand) validateArguments(args []string) (*options, error) {
Expand Down Expand Up @@ -94,7 +94,7 @@ func (fetchAll *AllCommand) parse(args []string) (*options, error) {
var options = options{"origin", []string{}}
flags := flag.NewFlagSet("fetch-all", flag.ExitOnError)
flags.Usage = func() { fmt.Println(fetchAll.Help()) }
flags.StringVar(&options.remote, "r", "origin", "remote name")
flags.StringVarP(&options.remote, "remote", "r", "origin", "remote name")

if err := flags.Parse(args); err != nil {
return nil, err
Expand Down
7 changes: 3 additions & 4 deletions fetch/fetch_cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package fetch

import (
"flag"
"fmt"

"github.com/mitchellh/cli"
flag "github.com/ogier/pflag"
"github.com/tamada/rrh/common"
)

Expand All @@ -28,7 +28,7 @@ Help returns the help message of the command.
func (fetch *Command) Help() string {
return `rrh fetch [OPTIONS] [GROUPS...]
OPTIONS
-r, --remote <REMOTE> specify the remote name. Default is "origin."
-r, --remot=<REMOTE> specify the remote name. Default is "origin."
ARGUMENTS
GROUPS run "git fetch" command on each repository on the group.
if no value is specified, run on the default group.`
Expand Down Expand Up @@ -92,8 +92,7 @@ func (fetch *Command) parse(args []string) (*options, error) {
var options = options{"origin", []string{}}
flags := flag.NewFlagSet("fetch", flag.ExitOnError)
flags.Usage = func() { fmt.Println(fetch.Help()) }
flags.StringVar(&options.remote, "r", "origin", "remote name")
flags.StringVar(&options.remote, "remote", "origin", "remote name")
flags.StringVarP(&options.remote, "remote", "r", "origin", "remote name")
// flags.StringVar(&options.key, "k", "", "private key path")
// flags.StringVar(&options.userName, "u", "", "user name")
// flags.StringVar(&options.password, "p", "", "password")
Expand Down
45 changes: 17 additions & 28 deletions group/group_cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package group

import (
"flag"
"fmt"

"github.com/mitchellh/cli"
flag "github.com/ogier/pflag"
"github.com/tamada/rrh/common"
)

Expand Down Expand Up @@ -50,8 +50,8 @@ func removeCommandFactory() (cli.Command, error) {
func (gac *addCommand) Help() string {
return `rrh group add [OPTIONS] <GROUPS...>
OPTIONS
-d, --desc <DESC> gives the description of the group.
-o, --omit-list <FLAG> gives the omit list flag of the group.
-d, --desc=<DESC> gives the description of the group.
-o, --omit-list=<FLAG> gives the omit list flag of the group.
ARGUMENTS
GROUPS gives group names.`
}
Expand Down Expand Up @@ -83,9 +83,9 @@ ARGUMENTS
func (guc *updateCommand) Help() string {
return `rrh group update [OPTIONS] <GROUP>
OPTIONS
-n, --name <NAME> change group name to NAME.
-d, --desc <DESC> change description to DESC.
-o, --omit-list <FLAG> change omit-list of the group. FLAG must be "true" or "false".
-n, --name=<NAME> change group name to NAME.
-d, --desc=<DESC> change description to DESC.
-o, --omit-list=<FLAG> change omit-list of the group. FLAG must be "true" or "false".
ARGUMENTS
GROUP update target group names.`
}
Expand Down Expand Up @@ -138,10 +138,8 @@ func (gac *addCommand) buildFlagSet() (*flag.FlagSet, *addOptions) {
var opt = addOptions{}
flags := flag.NewFlagSet("add", flag.ContinueOnError)
flags.Usage = func() { fmt.Println(gac.Help()) }
flags.StringVar(&opt.desc, "d", "", "description")
flags.StringVar(&opt.desc, "desc", "", "description")
flags.StringVar(&opt.omit, "o", "", "omit list flag")
flags.StringVar(&opt.omit, "omit-list", "", "omit list flag")
flags.StringVarP(&opt.desc, "desc", "d", "", "description")
flags.StringVarP(&opt.omit, "omit-list", "o", "", "omit list flag")
return flags, &opt
}

Expand Down Expand Up @@ -191,12 +189,9 @@ func (glc *listCommand) buildFlagSet() (*flag.FlagSet, *listOptions) {
var opt = listOptions{}
flags := flag.NewFlagSet("list", flag.ContinueOnError)
flags.Usage = func() { fmt.Println(glc.Help()) }
flags.BoolVar(&opt.desc, "d", false, "show description")
flags.BoolVar(&opt.desc, "desc", false, "show description")
flags.BoolVar(&opt.repositories, "r", false, "show repositories")
flags.BoolVar(&opt.repositories, "repository", false, "show repositories")
flags.BoolVar(&opt.nameOnly, "o", false, "show only group names")
flags.BoolVar(&opt.nameOnly, "only-groupname", false, "show only group names")
flags.BoolVarP(&opt.desc, "desc", "d", false, "show description")
flags.BoolVarP(&opt.repositories, "repository", "r", false, "show repositories")
flags.BoolVarP(&opt.nameOnly, "only-groupname", "o", false, "show only group names")
return flags, &opt
}

Expand Down Expand Up @@ -312,12 +307,9 @@ func (grc *removeCommand) buildFlagSet() (*flag.FlagSet, *removeOptions) {
var opt = removeOptions{}
flags := flag.NewFlagSet("rm", flag.ContinueOnError)
flags.Usage = func() { fmt.Println(grc.Help()) }
flags.BoolVar(&opt.inquiry, "i", false, "inquiry mode")
flags.BoolVar(&opt.verbose, "v", false, "verbose mode")
flags.BoolVar(&opt.force, "f", false, "force remove")
flags.BoolVar(&opt.inquiry, "inquiry", false, "inquiry mode")
flags.BoolVar(&opt.verbose, "verbose", false, "verbose mode")
flags.BoolVar(&opt.force, "force", false, "force remove")
flags.BoolVarP(&opt.inquiry, "inquiry", "i", false, "inquiry mode")
flags.BoolVarP(&opt.verbose, "verbose", "v", false, "verbose mode")
flags.BoolVarP(&opt.force, "force", "f", false, "force remove")
return flags, &opt
}

Expand Down Expand Up @@ -392,12 +384,9 @@ func (guc *updateCommand) buildFlagSet() (*flag.FlagSet, *updateOptions) {
var opt = updateOptions{}
flags := flag.NewFlagSet("update", flag.ContinueOnError)
flags.Usage = func() { fmt.Println(guc.Help()) }
flags.StringVar(&opt.newName, "n", "", "specify new group name")
flags.StringVar(&opt.newName, "name", "", "specify new group name")
flags.StringVar(&opt.desc, "d", "", "specify the description")
flags.StringVar(&opt.desc, "desc", "", "specify the description")
flags.StringVar(&opt.omitList, "omit-list", "", "set the omit list flag.")
flags.StringVar(&opt.omitList, "o", "", "set the omit list flag.")
flags.StringVarP(&opt.newName, "name", "n", "", "specify new group name")
flags.StringVarP(&opt.desc, "desc", "d", "", "specify the description")
flags.StringVarP(&opt.omitList, "omit-list", "o", "", "set the omit list flag.")
return flags, &opt
}

Expand Down
Loading

0 comments on commit 5d43e71

Please sign in to comment.