diff --git a/cmd/root.go b/cmd/root.go index 01ac7cf..b12c866 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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) @@ -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() { diff --git a/option/option.go b/option/option.go index a7732b1..d3a55cb 100644 --- a/option/option.go +++ b/option/option.go @@ -2,9 +2,10 @@ package option import ( "fmt" - "github.com/spf13/viper" "os" "path/filepath" + + "github.com/spf13/viper" ) // Option is commandline options @@ -76,6 +77,7 @@ const ( NameUseRegexp = "use-regexp" NameInputFiles = "input-files" NameSplitBefore = "split-before" + NameFieldSplit = "field-split" ) type SplitStrategy int @@ -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), diff --git a/option/option_test.go b/option/option_test.go index 281a6a1..4bfa1c4 100644 --- a/option/option_test.go +++ b/option/option_test.go @@ -2,7 +2,6 @@ package option_test import ( "fmt" - "github.com/spf13/viper" "io/ioutil" "os" "path/filepath" @@ -10,6 +9,8 @@ import ( "runtime" "testing" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" "github.com/xztaityozx/sel/option" ) @@ -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) {