Simplify handling of exiting options

pull/3769/head
Junegunn Choi 2 weeks ago
parent 94d870132f
commit e7b53df06a
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

@ -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")
}

@ -2,7 +2,6 @@
package fzf
import (
"fmt"
"sync"
"time"
"unsafe"
@ -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()

@ -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
@ -368,6 +367,7 @@ type Options struct {
WalkerRoot string
WalkerSkip []string
Version bool
Help bool
CPUProfile string
MEMProfile string
BlockProfile string
@ -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) {
@ -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":
@ -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 {
@ -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 != "" {

Loading…
Cancel
Save