Skip to content

Commit

Permalink
Merge pull request #25 from xztaityozx/issue/add-shorthand-for-field-…
Browse files Browse the repository at this point in the history
…split

Issue/add shorthand for field split
  • Loading branch information
xztaityozx authored Jan 27, 2022
2 parents db32960 + ba681e0 commit a798748
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var rootCmd = &cobra.Command{
__sel__ect column`,
Args: cobra.MinimumNArgs(1),
Version: "1.1.4",
Version: "1.1.5",
Run: func(cmd *cobra.Command, args []string) {
opt := option.NewOption(viper.GetViper())
selectors, err := parser.Parse(args)
Expand Down Expand Up @@ -103,6 +103,7 @@ func init() {
rootCmd.Flags().BoolP(option.NameRemoveEmpty, "r", false, "remove empty sequence")
rootCmd.Flags().BoolP(option.NameUseRegexp, "g", false, "use regular expressions for input delimiter")
rootCmd.Flags().BoolP(option.NameSplitBefore, "S", false, "split all column before select")
rootCmd.Flags().BoolP(option.NameFieldSplit, "a", false, "Shorthand for -gd '\\s+'")
_ = rootCmd.MarkFlagFilename(option.NameInputFiles)

for _, key := range option.GetOptionNames() {
Expand Down
19 changes: 18 additions & 1 deletion option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package option

import (
"fmt"
"github.com/spf13/viper"
"os"
"path/filepath"

"github.com/spf13/viper"
)

// Option is commandline options
Expand Down Expand Up @@ -76,6 +77,7 @@ const (
NameUseRegexp = "use-regexp"
NameInputFiles = "input-files"
NameSplitBefore = "split-before"
NameFieldSplit = "field-split"
)

type SplitStrategy int
Expand All @@ -88,11 +90,26 @@ func GetOptionNames() []string {
NameUseRegexp,
NameRemoveEmpty,
NameSplitBefore,
NameFieldSplit,
}
}

// NewOption は viper.Viper からフラグの値を取り出して Option を作って返す
func NewOption(v *viper.Viper) Option {

if v.GetBool(NameFieldSplit) {
return Option{
DelimiterOption: DelimiterOption{
InputDelimiter: `\s+`,
OutPutDelimiter: v.GetString(NameOutPutDelimiter),
RemoveEmpty: v.GetBool(NameRemoveEmpty),
UseRegexp: true,
SplitBefore: v.GetBool(NameSplitBefore),
},
InputFiles: InputFiles{v.GetStringSlice(NameInputFiles)},
}
}

return Option{
DelimiterOption: DelimiterOption{
InputDelimiter: v.GetString(NameInputDelimiter),
Expand Down
5 changes: 3 additions & 2 deletions option/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package option_test

import (
"fmt"
"github.com/spf13/viper"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"runtime"
"testing"

"github.com/spf13/viper"

"github.com/stretchr/testify/assert"
"github.com/xztaityozx/sel/option"
)
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestGetOptionNames(t *testing.T) {
name string
want []string
}{
{name: "とれてますか", want: []string{option.NameInputFiles, option.NameInputDelimiter, option.NameOutPutDelimiter, option.NameUseRegexp, option.NameRemoveEmpty, option.NameSplitBefore}},
{name: "とれてますか", want: []string{option.NameInputFiles, option.NameInputDelimiter, option.NameOutPutDelimiter, option.NameUseRegexp, option.NameRemoveEmpty, option.NameSplitBefore, option.NameFieldSplit}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit a798748

Please sign in to comment.