Skip to content

Commit

Permalink
Simplify handling of exiting options
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed May 6, 2024
1 parent c2e3afa commit e9d7526
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 45 deletions.
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,20 @@ func main() {
fmt.Println("fzf_key_bindings")
return
}
code, err := fzf.Run(options, version, revision)
if options.Help {
fmt.Print(fzf.Usage)
return
}
if options.Version {
if len(revision) > 0 {
fmt.Printf("%s (%s)\n", version, revision)
} else {
fmt.Println(version)
}
return
}

code, err := fzf.Run(options)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
}
Expand Down
12 changes: 1 addition & 11 deletions src/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package fzf

import (
"fmt"
"sync"
"time"
"unsafe"
Expand All @@ -28,22 +27,13 @@ func sbytes(data string) []byte {
}

// Run starts fzf
func Run(opts *Options, version string, revision string) (int, error) {
func Run(opts *Options) (int, error) {
defer clearCaches()
defer util.RunAtExitFuncs()

sort := opts.Sort > 0
sortCriteria = opts.Criteria

if opts.Version {
if len(revision) > 0 {
fmt.Printf("%s (%s)\n", version, revision)
} else {
fmt.Println(version)
}
return ExitOk, nil
}

// Event channel
eventBox := util.NewEventBox()

Expand Down
50 changes: 17 additions & 33 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (

"github.com/junegunn/fzf/src/algo"
"github.com/junegunn/fzf/src/tui"
"github.com/junegunn/fzf/src/util"

"github.com/mattn/go-shellwords"
"github.com/rivo/uniseg"
)

const usage = `usage: fzf [options]
const Usage = `usage: fzf [options]
Search
-x, --extended Extended-search mode
Expand Down Expand Up @@ -368,6 +367,7 @@ type Options struct {
WalkerRoot string
WalkerSkip []string
Version bool
Help bool
CPUProfile string
MEMProfile string
BlockProfile string
Expand Down Expand Up @@ -458,14 +458,10 @@ func defaultOptions() *Options {
WalkerOpts: walkerOpts{file: true, hidden: true, follow: true},
WalkerRoot: ".",
WalkerSkip: []string{".git", "node_modules"},
Help: false,
Version: false}
}

func help(code int) {
os.Stdout.WriteString(usage)
util.Exit(code)
}

func optString(arg string, prefixes ...string) (bool, string) {
for _, prefix := range prefixes {
if strings.HasPrefix(arg, prefix) {
Expand Down Expand Up @@ -1749,26 +1745,29 @@ func parseOptions(opts *Options, allArgs []string) error {
return nil
}
validateJumpLabels := false
clearExitingOpts := func() {
// Last-one-wins strategy
opts.Bash = false
opts.Zsh = false
opts.Fish = false
opts.Help = false
opts.Version = false
}
for i := 0; i < len(allArgs); i++ {
arg := allArgs[i]
switch arg {
case "--bash":
clearExitingOpts()
opts.Bash = true
if opts.Zsh || opts.Fish {
return errors.New("cannot specify --bash with --zsh or --fish")
}
case "--zsh":
clearExitingOpts()
opts.Zsh = true
if opts.Bash || opts.Fish {
return errors.New("cannot specify --zsh with --bash or --fish")
}
case "--fish":
clearExitingOpts()
opts.Fish = true
if opts.Bash || opts.Zsh {
return errors.New("cannot specify --fish with --bash or --zsh")
}
case "-h", "--help":
help(ExitOk)
clearExitingOpts()
opts.Help = true
case "-x", "--extended":
opts.Extended = true
case "-e", "--exact":
Expand Down Expand Up @@ -2225,6 +2224,7 @@ func parseOptions(opts *Options, allArgs []string) error {
}
opts.WalkerSkip = filterNonEmpty(strings.Split(str, ","))
case "--version":
clearExitingOpts()
opts.Version = true
case "--profile-cpu":
if opts.CPUProfile, err = nextString(allArgs, &i, "file path required: cpu"); err != nil {
Expand Down Expand Up @@ -2575,26 +2575,10 @@ func postProcessOptions(opts *Options) error {
return processScheme(opts)
}

func expectsArbitraryString(opt string) bool {
switch opt {
case "-q", "--query", "-f", "--filter", "--header", "--prompt",
"--border-label", "--preview-label", "--separator", "--ellipsis": // Seriously?
return true
}
return false
}

// ParseOptions parses command-line options
func ParseOptions(useDefaults bool, args []string) (*Options, error) {
opts := defaultOptions()

for idx, arg := range args {
if arg == "--version" && (idx == 0 || idx > 0 && !expectsArbitraryString(args[idx-1])) {
opts.Version = true
return opts, nil
}
}

if useDefaults {
// 1. Options from $FZF_DEFAULT_OPTS_FILE
if path := os.Getenv("FZF_DEFAULT_OPTS_FILE"); path != "" {
Expand Down

0 comments on commit e9d7526

Please sign in to comment.