Refactor option parser
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 9c2f6ca · Browse files · 1 file·+177 added−352 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+177 added−352 removed
- other:+177 added−352 removed
Provenance
- Commit
fa3f706- Tree
430e97e- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
9c2f6ca
1 file changed+177 added−352 removed
modified:src/options.go+177 added-352 removed
698 unmodified lines699 Help: false,700 Version: false}701 }702 703-func optString(arg string, prefixes ...string) (bool, string) {704- for _, prefix := range prefixes {705- if strings.HasPrefix(arg, prefix) {706- return true, arg[len(prefix):]707- }708- }709- return false, ""710-}711-712-func nextString(args []string, i *int, message string) (string, error) {713- if len(args) > *i+1 {714- *i++715- } else {716- return "", errors.New(message)717- }718- return args[*i], nil719-}720-721-func optionalNextString(args []string, i *int) (bool, string) {722- if len(args) > *i+1 && !strings.HasPrefix(args[*i+1], "-") && !strings.HasPrefix(args[*i+1], "+") {723- *i++724- return true, args[*i]725- }726- return false, ""727-}728-703 func isDir(path string) bool {704 stat, err := os.Stat(path)705 return err == nil && stat.IsDir()706 }707 734-func nextDirs(args []string, i *int) ([]string, error) {735- dirs := []string{}736- for *i < len(args)-1 {737- arg := args[*i+1]738- if isDir(arg) {739- dirs = append(dirs, arg)740- *i++741- } else {742- break743- }744- }745- if len(dirs) == 0 {746- return nil, errors.New("no directory specified")747- }748- return dirs, nil749-}750-708 func atoi(str string) (int, error) {709 num, err := strconv.Atoi(str)710 if err != nil {711 return 0, errors.New("not a valid integer: " + str)8 unmodified lines720 }721 return num, nil722 }723 767-func nextInt(args []string, i *int, message string) (int, error) {768- if len(args) > *i+1 {769- *i++770- } else {771- return 0, errors.New(message)772- }773- n, err := atoi(args[*i])774- if err != nil {775- return 0, errors.New(message)776- }777- return n, nil778-}779-780-func optionalNumeric(args []string, i *int, defaultValue int) (int, error) {781- if len(args) > *i+1 {782- if strings.IndexAny(args[*i+1], "0123456789") == 0 {783- *i++784- n, err := atoi(args[*i])785- if err != nil {786- return 0, err787- }788- return n, nil789- }790- }791- return defaultValue, nil792-}793-724 func splitNth(str string) ([]Range, error) {725 if match, _ := regexp.MatchString("^[0-9,-.]+$", str); !match {726 return nil, errors.New("invalid format: " + str)727 }1278 unmodified lines2006 }2007 return &result, nil2008 }2009 2010+func optString(arg string, prefix string) (bool, string) {2011+ if strings.HasPrefix(arg, prefix) {2012+ return true, arg[len(prefix):]2013+ }2014+ return false, ""2015+}2016+2017 func parseOptions(index *int, opts *Options, allArgs []string) error {2018 var err error2019 var historyMax int2020 if opts.History == nil {28 unmodified lines2049 opts.Help = false2050 opts.Version = false2051 opts.Man = false2052 }2053+2054 startIndex := *index2055+2056+ var i int2057+ var val *string = nil2058+ nextString := func(message string) (string, error) {2059+ defer func() { val = nil }()2060+ if val != nil {2061+ return *val, nil2062+ }2063+ if len(allArgs) > i+1 {2064+ i++2065+ } else {2066+ return "", errors.New(message)2067+ }2068+ return allArgs[i], nil2069+ }2070+2071+ optionalNextString := func() (bool, string) {2072+ defer func() { val = nil }()2073+ if val != nil {2074+ return true, *val2075+ }2076+ if len(allArgs) > i+1 && !strings.HasPrefix(allArgs[i+1], "-") && !strings.HasPrefix(allArgs[i+1], "+") {2077+ i++2078+ return true, allArgs[i]2079+ }2080+ return false, ""2081+ }2082+2083+ nextDirs := func() ([]string, error) {2084+ defer func() { val = nil }()2085+ dirs := []string{}2086+ if val != nil {2087+ dirs = append(dirs, *val)2088+ }2089+ for i < len(allArgs)-1 {2090+ arg := allArgs[i+1]2091+ if isDir(arg) {2092+ dirs = append(dirs, arg)2093+ i++2094+ } else {2095+ break2096+ }2097+ }2098+ if len(dirs) == 0 {2099+ return nil, errors.New("no directory specified")2100+ }2101+ return dirs, nil2102+ }2103+2104+ nextInt := func(message string) (int, error) {2105+ defer func() { val = nil }()2106+ var str string2107+ if val != nil {2108+ str = *val2109+ } else if len(allArgs) > i+1 {2110+ i++2111+ str = allArgs[i]2112+ } else {2113+ return 0, errors.New(message)2114+ }2115+ n, err := atoi(str)2116+ if err != nil {2117+ return 0, errors.New(message)2118+ }2119+ return n, nil2120+ }2121+2122+ optionalNumeric := func(defaultValue int) (int, error) {2123+ defer func() { val = nil }()2124+ var str string2125+ if val != nil {2126+ str = *val2127+ } else if len(allArgs) > i+1 && strings.IndexAny(allArgs[i+1], "0123456789") == 0 {2128+ i++2129+ str = allArgs[i]2130+ } else {2131+ return defaultValue, nil2132+ }2133+ n, err := atoi(str)2134+ if err != nil {2135+ return 0, err2136+ }2137+ return n, nil2138+ }2139+2117- for i := 0; i < len(allArgs); i++ {2140+ for ; i < len(allArgs); i++ {2141 arg := allArgs[i]2142 index := i + startIndex2143+ if strings.HasPrefix(arg, "--") && strings.IndexRune(arg, '=') > 0 {2144+ tokens := strings.SplitN(arg, "=", 2)2145+ arg = tokens[0]2146+ val = &tokens[1]2147+ }2148 switch arg {2149 case "--man":2150 clearExitingOpts()2151 opts.Man = true14 unmodified lines2166 opts.Version = true2167 case "--no-winpty":2168 opts.NoWinpty = true2169 case "--tmux":2142- given, str := optionalNextString(allArgs, &i)2170+ given, str := optionalNextString()2171 if given {2172 if opts.Tmux, err = parseTmuxOptions(str, index); err != nil {2173 return err2174 }8 unmodified lines2183 opts.ForceTtyIn = true2184 case "--no-force-tty-in":2185 opts.ForceTtyIn = false2186 case "--proxy-script":2159- if opts.ProxyScript, err = nextString(allArgs, &i, ""); err != nil {2187+ if opts.ProxyScript, err = nextString(""); err != nil {2188 return err2189 }2190 case "-x", "--extended":2191 opts.Extended = true7 unmodified lines2199 opts.Extended = false2200 case "+e", "--no-exact":2201 opts.Fuzzy = true2202 case "-q", "--query":2175- if opts.Query, err = nextString(allArgs, &i, "query string required"); err != nil {2203+ if opts.Query, err = nextString("query string required"); err != nil {2204 return err2205 }2206 case "-f", "--filter":2179- filter, err := nextString(allArgs, &i, "query string required")2207+ filter, err := nextString("query string required")2208 if err != nil {2209 return err2210 }2211 opts.Filter = &filter1 unmodified line2213 opts.Normalize = false2214 case "--no-literal":2215 opts.Normalize = true2216 case "--algo":2189- str, err := nextString(allArgs, &i, "algorithm required (v1|v2)")2217+ str, err := nextString("algorithm required (v1|v2)")2218 if err != nil {2219 return err2220 }2221 if opts.FuzzyAlgo, err = parseAlgo(str); err != nil {2222 return err2223 }2224 case "--scheme":2197- str, err := nextString(allArgs, &i, "scoring scheme required (default|path|history)")2225+ str, err := nextString("scoring scheme required (default|path|history)")2226 if err != nil {2227 return err2228 }2229 opts.Scheme = strings.ToLower(str)2230 case "--expect":2203- str, err := nextString(allArgs, &i, "key names required")2231+ str, err := nextString("key names required")2232 if err != nil {2233 return err2234 }2235 chords, err := parseKeyChords(str, "key names required")9 unmodified lines2245 opts.Phony = false2246 case "--disabled", "--phony":2247 opts.Phony = true2248 case "--tiebreak":2221- str, err := nextString(allArgs, &i, "sort criterion required")2249+ str, err := nextString("sort criterion required")2250 if err != nil {2251 return err2252 }2253 if opts.Criteria, err = parseTiebreak(str); err != nil {2254 return err2255 }2256 case "--bind":2229- str, err := nextString(allArgs, &i, "bind expression required")2257+ str, err := nextString("bind expression required")2258 if err != nil {2259 return err2260 }2261 if err := parseKeymap(opts.Keymap, str); err != nil {2262 return err2263 }2264 case "--color":2237- _, spec := optionalNextString(allArgs, &i)2265+ _, spec := optionalNextString()2266 if len(spec) == 0 {2267 opts.Theme = tui.EmptyTheme()2268 } else {2269 if opts.Theme, err = parseTheme(opts.Theme, spec); err != nil {2270 return err2271 }2272 }2273 case "--toggle-sort":2246- str, err := nextString(allArgs, &i, "key name required")2274+ str, err := nextString("key name required")2275 if err != nil {2276 return err2277 }2278 if err := parseToggleSort(opts.Keymap, str); err != nil {2279 return err2280 }2281 case "-d", "--delimiter":2254- str, err := nextString(allArgs, &i, "delimiter required")2282+ str, err := nextString("delimiter required")2283 if err != nil {2284 return err2285 }2286 opts.Delimiter = delimiterRegexp(str)2287 case "-n", "--nth":2260- str, err := nextString(allArgs, &i, "nth expression required")2288+ str, err := nextString("nth expression required")2289 if err != nil {2290 return err2291 }2292 if opts.Nth, err = splitNth(str); err != nil {2293 return err2294 }2295 case "--with-nth":2268- str, err := nextString(allArgs, &i, "nth expression required")2296+ str, err := nextString("nth expression required")2297 if err != nil {2298 return err2299 }2300 if opts.WithNth, err = splitNth(str); err != nil {2301 return err2302 }2303 case "-s", "--sort":2276- if opts.Sort, err = optionalNumeric(allArgs, &i, 1); err != nil {2304+ if opts.Sort, err = optionalNumeric(1); err != nil {2305 return err2306 }2307 case "+s", "--no-sort":2308 opts.Sort = 05 unmodified lines2314 opts.Tac = true2315 case "--no-tac":2316 opts.Tac = false2317 case "--tail":2290- if opts.Tail, err = nextInt(allArgs, &i, "number of items to keep required"); err != nil {2318+ if opts.Tail, err = nextInt("number of items to keep required"); err != nil {2319 return err2320 }2321 if opts.Tail <= 0 {2322 return errors.New("number of items to keep must be a positive integer")6 unmodified lines2329 opts.Case = CaseIgnore2330 case "+i", "--no-ignore-case":2331 opts.Case = CaseRespect2332 case "-m", "--multi":2305- if opts.Multi, err = optionalNumeric(allArgs, &i, maxMulti); err != nil {2333+ if opts.Multi, err = optionalNumeric(maxMulti); err != nil {2334 return err2335 }2336 case "+m", "--no-multi":2337 opts.Multi = 015 unmodified lines2353 opts.Bold = true2354 case "--no-bold":2355 opts.Bold = false2356 case "--layout":2329- str, err := nextString(allArgs, &i, "layout required (default / reverse / reverse-list)")2357+ str, err := nextString("layout required (default / reverse / reverse-list)")2358 if err != nil {2359 return err2360 }2361 if opts.Layout, err = parseLayout(str); err != nil {15 unmodified lines2377 opts.Wrap = true2378 case "--no-wrap":2379 opts.Wrap = false2380 case "--wrap-sign":2353- str, err := nextString(allArgs, &i, "wrap sign required")2381+ str, err := nextString("wrap sign required")2382 if err != nil {2383 return err2384 }2385 opts.WrapSign = &str9 unmodified lines2395 opts.Hscroll = true2396 case "--no-hscroll":2397 opts.Hscroll = false2398 case "--hscroll-off":2371- if opts.HscrollOff, err = nextInt(allArgs, &i, "hscroll offset required"); err != nil {2399+ if opts.HscrollOff, err = nextInt("hscroll offset required"); err != nil {2400 return err2401 }2402 case "--scroll-off":2375- if opts.ScrollOff, err = nextInt(allArgs, &i, "scroll offset required"); err != nil {2403+ if opts.ScrollOff, err = nextInt("scroll offset required"); err != nil {2404 return err2405 }2406 case "--filepath-word":2407 opts.FileWord = true2408 case "--no-filepath-word":2409 opts.FileWord = false2410 case "--info":2383- str, err := nextString(allArgs, &i, "info style required")2411+ str, err := nextString("info style required")2412 if err != nil {2413 return err2414 }2415 if opts.InfoStyle, opts.InfoPrefix, err = parseInfoStyle(str); err != nil {2416 return err2417 }2418 case "--info-command":2391- if opts.InfoCommand, err = nextString(allArgs, &i, "info command required"); err != nil {2419+ if opts.InfoCommand, err = nextString("info command required"); err != nil {2420 return err2421 }2422 case "--no-info-command":2423 opts.InfoCommand = ""4 unmodified lines2428 opts.InfoPrefix = defaultInfoPrefix2429 case "--no-inline-info":2430 opts.InfoStyle = infoDefault2431 case "--separator":2404- separator, err := nextString(allArgs, &i, "separator character required")2432+ separator, err := nextString("separator character required")2433 if err != nil {2434 return err2435 }2436 opts.Separator = &separator2437 case "--no-separator":2438 nosep := ""2439 opts.Separator = &nosep2440 case "--scrollbar":2413- given, bar := optionalNextString(allArgs, &i)2441+ given, bar := optionalNextString()2442 if given {2443 opts.Scrollbar = &bar2444 } else {2445 opts.Scrollbar = nil1 unmodified line2447 case "--no-scrollbar":2448 noBar := ""2449 opts.Scrollbar = &noBar2450 case "--jump-labels":2423- if opts.JumpLabels, err = nextString(allArgs, &i, "label characters required"); err != nil {2451+ if opts.JumpLabels, err = nextString("label characters required"); err != nil {2452 return err2453 }2454 validateJumpLabels = true2455 case "-1", "--select-1":18 unmodified lines2474 opts.PrintQuery = true2475 case "--no-print-query":2476 opts.PrintQuery = false2477 case "--prompt":2450- opts.Prompt, err = nextString(allArgs, &i, "prompt string required")2478+ opts.Prompt, err = nextString("prompt string required")2479 if err != nil {2480 return err2481 }2482 case "--pointer":2455- str, err := nextString(allArgs, &i, "pointer sign required")2483+ str, err := nextString("pointer sign required")2484 if err != nil {2485 return err2486 }2487 str = firstLine(str)2488 opts.Pointer = &str2489 case "--marker":2462- str, err := nextString(allArgs, &i, "marker sign required")2490+ str, err := nextString("marker sign required")2491 if err != nil {2492 return err2493 }2494 str = firstLine(str)2495 opts.Marker = &str2496 case "--marker-multi-line":2469- str, err := nextString(allArgs, &i, "marker sign for multi-line entries required")2497+ str, err := nextString("marker sign for multi-line entries required")2498 if err != nil {2499 return err2500 }2501 if opts.MarkerMulti, err = parseMarkerMultiLine(firstLine(str)); err != nil {5 unmodified lines2507 opts.Sync = false2508 case "--no-history":2509 opts.History = nil2510 case "--history":2483- str, err := nextString(allArgs, &i, "history file path required")2511+ str, err := nextString("history file path required")2512 if err != nil {2513 return err2514 }2515 if err := setHistory(str); err != nil {2516 return err2517 }2518 case "--history-size":2491- n, err := nextInt(allArgs, &i, "history max size required")2519+ n, err := nextInt("history max size required")2520 if err != nil {2521 return err2522 }2523 if err := setHistoryMax(n); err != nil {3 unmodified lines2527 opts.Header = []string{}2528 case "--no-header-lines":2529 opts.HeaderLines = 02530 case "--header":2503- str, err := nextString(allArgs, &i, "header string required")2531+ str, err := nextString("header string required")2532 if err != nil {2533 return err2534 }2535 opts.Header = strLines(str)2536 case "--header-lines":2509- if opts.HeaderLines, err = nextInt(allArgs, &i, "number of header lines required"); err != nil {2537+ if opts.HeaderLines, err = nextInt("number of header lines required"); err != nil {2538 return err2539 }2540 case "--header-first":2541 opts.HeaderFirst = true2542 case "--no-header-first":2543 opts.HeaderFirst = false2544 case "--gap":2517- if opts.Gap, err = optionalNumeric(allArgs, &i, 1); err != nil {2545+ if opts.Gap, err = optionalNumeric(1); err != nil {2546 return err2547 }2548 case "--no-gap":2549 opts.Gap = 02550 case "--ellipsis":2523- str, err := nextString(allArgs, &i, "ellipsis string required")2551+ str, err := nextString("ellipsis string required")2552 if err != nil {2553 return err2554 }2555 str = firstLine(str)2556 opts.Ellipsis = &str2557 case "--preview":2530- if opts.Preview.command, err = nextString(allArgs, &i, "preview command required"); err != nil {2558+ if opts.Preview.command, err = nextString("preview command required"); err != nil {2559 return err2560 }2561 case "--no-preview":2562 opts.Preview.command = ""2563 case "--preview-window":2536- str, err := nextString(allArgs, &i, "preview window layout required: [up|down|left|right][,SIZE[%]][,border-STYLE][,wrap][,cycle][,hidden][,+SCROLL[OFFSETS][/DENOM]][,~HEADER_LINES][,default]")2564+ str, err := nextString("preview window layout required: [up|down|left|right][,SIZE[%]][,border-STYLE][,wrap][,cycle][,hidden][,+SCROLL[OFFSETS][/DENOM]][,~HEADER_LINES][,default]")2565 if err != nil {2566 return err2567 }2568 if err := parsePreviewWindow(&opts.Preview, str); err != nil {1 unmodified line2570 }2571 case "--no-preview-border":2572 opts.Preview.border = tui.BorderNone2573 case "--preview-border":2546- hasArg, arg := optionalNextString(allArgs, &i)2574+ hasArg, arg := optionalNextString()2575 if opts.Preview.border, err = parseBorder(arg, !hasArg, true); err != nil {2576 return err2577 }2578 case "--height":2551- str, err := nextString(allArgs, &i, "height required: [~]HEIGHT[%]")2579+ str, err := nextString("height required: [~]HEIGHT[%]")2580 if err != nil {2581 return err2582 }2583 if opts.Height, err = parseHeight(str, index); err != nil {2584 return err2585 }2586 case "--min-height":2559- if opts.MinHeight, err = nextInt(allArgs, &i, "height required: HEIGHT"); err != nil {2587+ if opts.MinHeight, err = nextInt("height required: HEIGHT"); err != nil {2588 return err2589 }2590 case "--no-height":2591 opts.Height = heightSpec{}3 unmodified lines2595 opts.Padding = defaultMargin()2596 case "--no-border":2597 opts.BorderShape = tui.BorderNone2598 case "--border":2571- hasArg, arg := optionalNextString(allArgs, &i)2599+ hasArg, arg := optionalNextString()2600 if opts.BorderShape, err = parseBorder(arg, !hasArg, false); err != nil {2601 return err2602 }2603 case "--list-border":2576- hasArg, arg := optionalNextString(allArgs, &i)2604+ hasArg, arg := optionalNextString()2605 if opts.ListBorderShape, err = parseBorder(arg, !hasArg, false); err != nil {2606 return err2607 }2608 case "--no-list-border":2609 opts.ListBorderShape = tui.BorderNone2610 case "--no-list-label":2611 opts.ListLabel.label = ""2612 case "--list-label":2585- opts.ListLabel.label, err = nextString(allArgs, &i, "label required")2613+ opts.ListLabel.label, err = nextString("label required")2614 if err != nil {2615 return err2616 }2617 case "--list-label-pos":2590- pos, err := nextString(allArgs, &i, "label position required (positive or negative integer or 'center')")2618+ pos, err := nextString("label position required (positive or negative integer or 'center')")2619 if err != nil {2620 return err2621 }2622 if err := parseLabelPosition(&opts.ListLabel, pos); err != nil {1 unmodified line2624 }2625 case "--no-header-border":2626 opts.HeaderBorderShape = tui.BorderNone2627 case "--header-border":2600- hasArg, arg := optionalNextString(allArgs, &i)2628+ hasArg, arg := optionalNextString()2629 if opts.HeaderBorderShape, err = parseBorder(arg, !hasArg, false); err != nil {2630 return err2631 }2632 case "--no-header-label":2633 opts.HeaderLabel.label = ""2634 case "--header-label":2607- if opts.HeaderLabel.label, err = nextString(allArgs, &i, "header label required"); err != nil {2635+ if opts.HeaderLabel.label, err = nextString("header label required"); err != nil {2636 return err2637 }2638 case "--header-label-pos":2611- pos, err := nextString(allArgs, &i, "header label position required (positive or negative integer or 'center')")2639+ pos, err := nextString("header label position required (positive or negative integer or 'center')")2640 if err != nil {2641 return err2642 }2643 if err := parseLabelPosition(&opts.HeaderLabel, pos); err != nil {1 unmodified line2645 }2646 case "--no-input-border":2647 opts.InputBorderShape = tui.BorderNone2648 case "--input-border":2621- hasArg, arg := optionalNextString(allArgs, &i)2649+ hasArg, arg := optionalNextString()2650 if opts.InputBorderShape, err = parseBorder(arg, !hasArg, false); err != nil {2651 return err2652 }2653 case "--no-input-label":2654 opts.InputLabel.label = ""2655 case "--input-label":2628- if opts.InputLabel.label, err = nextString(allArgs, &i, "input label required"); err != nil {2656+ if opts.InputLabel.label, err = nextString("input label required"); err != nil {2657 return err2658 }2659 case "--input-label-pos":2632- pos, err := nextString(allArgs, &i, "input label position required (positive or negative integer or 'center')")2660+ pos, err := nextString("input label position required (positive or negative integer or 'center')")2661 if err != nil {2662 return err2663 }2664 if err := parseLabelPosition(&opts.InputLabel, pos); err != nil {1 unmodified line2666 }2667 case "--no-border-label":2668 opts.BorderLabel.label = ""2669 case "--border-label":2642- opts.BorderLabel.label, err = nextString(allArgs, &i, "label required")2670+ opts.BorderLabel.label, err = nextString("label required")2671 if err != nil {2672 return err2673 }2674 case "--border-label-pos":2647- pos, err := nextString(allArgs, &i, "label position required (positive or negative integer or 'center')")2675+ pos, err := nextString("label position required (positive or negative integer or 'center')")2676 if err != nil {2677 return err2678 }2679 if err := parseLabelPosition(&opts.BorderLabel, pos); err != nil {1 unmodified line2681 }2682 case "--no-preview-label":2683 opts.PreviewLabel.label = ""2684 case "--preview-label":2657- if opts.PreviewLabel.label, err = nextString(allArgs, &i, "preview label required"); err != nil {2685+ if opts.PreviewLabel.label, err = nextString("preview label required"); err != nil {2686 return err2687 }2688 case "--preview-label-pos":2661- pos, err := nextString(allArgs, &i, "preview label position required (positive or negative integer or 'center')")2689+ pos, err := nextString("preview label position required (positive or negative integer or 'center')")2690 if err != nil {2691 return err2692 }2693 if err := parseLabelPosition(&opts.PreviewLabel, pos); err != nil {2694 return err2695 }2696 case "--style":2669- preset, err := nextString(allArgs, &i, "preset name required: [default|minimal|full]")2697+ preset, err := nextString("preset name required: [default|minimal|full]")2698 if err != nil {2699 return err2700 }2701 if err := applyPreset(opts, preset); err != nil {7 unmodified lines2709 opts.Ambidouble = true2710 case "--no-ambidouble":2711 opts.Ambidouble = false2712 case "--margin":2685- str, err := nextString(allArgs, &i, "margin required (TRBL / TB,RL / T,RL,B / T,R,B,L)")2713+ str, err := nextString("margin required (TRBL / TB,RL / T,RL,B / T,R,B,L)")2714 if err != nil {2715 return err2716 }2717 if opts.Margin, err = parseMargin("margin", str); err != nil {2718 return err2719 }2720 case "--padding":2693- str, err := nextString(allArgs, &i, "padding required (TRBL / TB,RL / T,RL,B / T,R,B,L)")2721+ str, err := nextString("padding required (TRBL / TB,RL / T,RL,B / T,R,B,L)")2722 if err != nil {2723 return err2724 }2725 if opts.Padding, err = parseMargin("padding", str); err != nil {2726 return err2727 }2728 case "--tabstop":2701- if opts.Tabstop, err = nextInt(allArgs, &i, "tab stop required"); err != nil {2729+ if opts.Tabstop, err = nextInt("tab stop required"); err != nil {2730 return err2731 }2732 case "--with-shell":2705- if opts.WithShell, err = nextString(allArgs, &i, "shell command and flags required"); err != nil {2733+ if opts.WithShell, err = nextString("shell command and flags required"); err != nil {2734 return err2735 }2736 case "--listen", "--listen-unsafe":2709- given, str := optionalNextString(allArgs, &i)2737+ given, str := optionalNextString()2738 addr := defaultListenAddr2739 if given {2740 var err error2741 addr, err = parseListenAddress(str)10 unmodified lines2752 opts.ClearOnExit = true2753 case "--no-clear":2754 opts.ClearOnExit = false2755 case "--walker":2728- str, err := nextString(allArgs, &i, "walker options required [file][,dir][,follow][,hidden]")2756+ str, err := nextString("walker options required [file][,dir][,follow][,hidden]")2757 if err != nil {2758 return err2759 }2760 if opts.WalkerOpts, err = parseWalkerOpts(str); err != nil {2761 return err2762 }2763 case "--walker-root":2736- if opts.WalkerRoot, err = nextDirs(allArgs, &i); err != nil {2764+ if opts.WalkerRoot, err = nextDirs(); err != nil {2765 return err2766 }2767 case "--walker-skip":2740- str, err := nextString(allArgs, &i, "directory names to ignore required")2768+ str, err := nextString("directory names to ignore required")2769 if err != nil {2770 return err2771 }2772 opts.WalkerSkip = filterNonEmpty(strings.Split(str, ","))2773 case "--profile-cpu":2746- if opts.CPUProfile, err = nextString(allArgs, &i, "file path required: cpu"); err != nil {2774+ if opts.CPUProfile, err = nextString("file path required: cpu"); err != nil {2775 return err2776 }2777 case "--profile-mem":2750- if opts.MEMProfile, err = nextString(allArgs, &i, "file path required: mem"); err != nil {2778+ if opts.MEMProfile, err = nextString("file path required: mem"); err != nil {2779 return err2780 }2781 case "--profile-block":2754- if opts.BlockProfile, err = nextString(allArgs, &i, "file path required: block"); err != nil {2782+ if opts.BlockProfile, err = nextString("file path required: block"); err != nil {2783 return err2784 }2785 case "--profile-mutex":2758- if opts.MutexProfile, err = nextString(allArgs, &i, "file path required: mutex"); err != nil {2786+ if opts.MutexProfile, err = nextString("file path required: mutex"); err != nil {2787 return err2788 }2789 case "--":2790 // Ignored2791 default:2764- if match, value := optString(arg, "--algo="); match {2765- if opts.FuzzyAlgo, err = parseAlgo(value); err != nil {2766- return err2767- }2768- } else if match, value := optString(arg, "--tmux="); match {2769- if opts.Tmux, err = parseTmuxOptions(value, index); err != nil {2770- return err2771- }2772- } else if match, value := optString(arg, "--style="); match {2773- if err := applyPreset(opts, value); err != nil {2774- return err2775- }2776- } else if match, value := optString(arg, "--scheme="); match {2777- opts.Scheme = strings.ToLower(value)2778- } else if match, value := optString(arg, "-q", "--query="); match {2792+ if match, value := optString(arg, "-q"); match {2793 opts.Query = value2780- } else if match, value := optString(arg, "-f", "--filter="); match {2794+ } else if match, value := optString(arg, "-f"); match {2795 opts.Filter = &value2782- } else if match, value := optString(arg, "-d", "--delimiter="); match {2796+ } else if match, value := optString(arg, "-d"); match {2797 opts.Delimiter = delimiterRegexp(value)2784- } else if match, value := optString(arg, "--border="); match {2785- if opts.BorderShape, err = parseBorder(value, false, false); err != nil {2786- return err2787- }2788- } else if match, value := optString(arg, "--preview-border="); match {2789- if opts.Preview.border, err = parseBorder(value, false, true); err != nil {2790- return err2791- }2792- } else if match, value := optString(arg, "--list-border="); match {2793- if opts.ListBorderShape, err = parseBorder(value, false, false); err != nil {2794- return err2795- }2796- } else if match, value := optString(arg, "--list-label="); match {2797- opts.ListLabel.label = value2798- } else if match, value := optString(arg, "--list-label-pos="); match {2799- if err := parseLabelPosition(&opts.ListLabel, value); err != nil {2800- return err2801- }2802- } else if match, value := optString(arg, "--input-border="); match {2803- if opts.InputBorderShape, err = parseBorder(value, false, false); err != nil {2804- return err2805- }2806- } else if match, value := optString(arg, "--input-label="); match {2807- opts.InputLabel.label = value2808- } else if match, value := optString(arg, "--input-label-pos="); match {2809- if err := parseLabelPosition(&opts.InputLabel, value); err != nil {2810- return err2811- }2812- } else if match, value := optString(arg, "--border-label="); match {2813- opts.BorderLabel.label = value2814- } else if match, value := optString(arg, "--border-label-pos="); match {2815- if err := parseLabelPosition(&opts.BorderLabel, value); err != nil {2816- return err2817- }2818- } else if match, value := optString(arg, "--preview-label="); match {2819- opts.PreviewLabel.label = value2820- } else if match, value := optString(arg, "--preview-label-pos="); match {2821- if err := parseLabelPosition(&opts.PreviewLabel, value); err != nil {2822- return err2823- }2824- } else if match, value := optString(arg, "--wrap-sign="); match {2825- opts.WrapSign = &value2826- } else if match, value := optString(arg, "--prompt="); match {2827- opts.Prompt = value2828- } else if match, value := optString(arg, "--pointer="); match {2829- str := firstLine(value)2830- opts.Pointer = &str2831- } else if match, value := optString(arg, "--marker="); match {2832- str := firstLine(value)2833- opts.Marker = &str2834- } else if match, value := optString(arg, "--marker-multi-line="); match {2835- if opts.MarkerMulti, err = parseMarkerMultiLine(firstLine(value)); err != nil {2836- return err2837- }2838- } else if match, value := optString(arg, "-n", "--nth="); match {2798+ } else if match, value := optString(arg, "-n"); match {2799 if opts.Nth, err = splitNth(value); err != nil {2800 return err2801 }2842- } else if match, value := optString(arg, "--with-nth="); match {2843- if opts.WithNth, err = splitNth(value); err != nil {2844- return err2845- }2846- } else if match, _ := optString(arg, "-s", "--sort="); match {2802+ } else if match, _ := optString(arg, "-s"); match {2803 opts.Sort = 1 // Don't care2848- } else if match, value := optString(arg, "-m", "--multi="); match {2804+ } else if match, value := optString(arg, "-m"); match {2805 if opts.Multi, err = atoi(value); err != nil {2806 return err2807 }2852- } else if match, value := optString(arg, "--height="); match {2853- if opts.Height, err = parseHeight(value, index); err != nil {2854- return err2855- }2856- } else if match, value := optString(arg, "--min-height="); match {2857- if opts.MinHeight, err = atoi(value); err != nil {2858- return err2859- }2860- } else if match, value := optString(arg, "--layout="); match {2861- if opts.Layout, err = parseLayout(value); err != nil {2862- return err2863- }2864- } else if match, value := optString(arg, "--info="); match {2865- if opts.InfoStyle, opts.InfoPrefix, err = parseInfoStyle(value); err != nil {2866- return err2867- }2868- } else if match, value := optString(arg, "--info-command="); match {2869- opts.InfoCommand = value2870- } else if match, value := optString(arg, "--separator="); match {2871- opts.Separator = &value2872- } else if match, value := optString(arg, "--scrollbar="); match {2873- opts.Scrollbar = &value2874- } else if match, value := optString(arg, "--toggle-sort="); match {2875- if err := parseToggleSort(opts.Keymap, value); err != nil {2876- return err2877- }2878- } else if match, value := optString(arg, "--expect="); match {2879- chords, err := parseKeyChords(value, "key names required")2880- if err != nil {2881- return err2882- }2883- for k, v := range chords {2884- opts.Expect[k] = v2885- }2886- } else if match, value := optString(arg, "--tiebreak="); match {2887- if opts.Criteria, err = parseTiebreak(value); err != nil {2888- return err2889- }2890- } else if match, value := optString(arg, "--color="); match {2891- if opts.Theme, err = parseTheme(opts.Theme, value); err != nil {2892- return err2893- }2894- } else if match, value := optString(arg, "--bind="); match {2895- if err := parseKeymap(opts.Keymap, value); err != nil {2896- return err2897- }2898- } else if match, value := optString(arg, "--history="); match {2899- if err := setHistory(value); err != nil {2900- return err2901- }2902- } else if match, value := optString(arg, "--history-size="); match {2903- n, err := atoi(value)2904- if err != nil {2905- return err2906- }2907- if err := setHistoryMax(n); err != nil {2908- return err2909- }2910- } else if match, value := optString(arg, "--header="); match {2911- opts.Header = strLines(value)2912- } else if match, value := optString(arg, "--header-lines="); match {2913- if opts.HeaderLines, err = atoi(value); err != nil {2914- return err2915- }2916- } else if match, value := optString(arg, "--gap="); match {2917- if opts.Gap, err = atoi(value); err != nil {2918- return err2919- }2920- } else if match, value := optString(arg, "--ellipsis="); match {2921- str := firstLine(value)2922- opts.Ellipsis = &str2923- } else if match, value := optString(arg, "--preview="); match {2924- opts.Preview.command = value2925- } else if match, value := optString(arg, "--preview-window="); match {2926- if err := parsePreviewWindow(&opts.Preview, value); err != nil {2927- return err2928- }2929- } else if match, value := optString(arg, "--margin="); match {2930- if opts.Margin, err = parseMargin("margin", value); err != nil {2931- return err2932- }2933- } else if match, value := optString(arg, "--padding="); match {2934- if opts.Padding, err = parseMargin("padding", value); err != nil {2935- return err2936- }2937- } else if match, value := optString(arg, "--tabstop="); match {2938- if opts.Tabstop, err = atoi(value); err != nil {2939- return err2940- }2941- } else if match, value := optString(arg, "--with-shell="); match {2942- opts.WithShell = value2943- } else if match, value := optString(arg, "--listen="); match {2944- addr, err := parseListenAddress(value)2945- if err != nil {2946- return err2947- }2948- opts.ListenAddr = &addr2949- opts.Unsafe = false2950- } else if match, value := optString(arg, "--listen-unsafe="); match {2951- addr, err := parseListenAddress(value)2952- if err != nil {2953- return err2954- }2955- opts.ListenAddr = &addr2956- opts.Unsafe = true2957- } else if match, value := optString(arg, "--walker="); match {2958- if opts.WalkerOpts, err = parseWalkerOpts(value); err != nil {2959- return err2960- }2961- } else if match, value := optString(arg, "--walker-root="); match {2962- if !isDir(value) {2963- return errors.New("not a directory: " + value)2964- }2965- dirs, _ := nextDirs(allArgs, &i)2966- opts.WalkerRoot = append([]string{value}, dirs...)2967- } else if match, value := optString(arg, "--walker-skip="); match {2968- opts.WalkerSkip = filterNonEmpty(strings.Split(value, ","))2969- } else if match, value := optString(arg, "--hscroll-off="); match {2970- if opts.HscrollOff, err = atoi(value); err != nil {2971- return err2972- }2973- } else if match, value := optString(arg, "--scroll-off="); match {2974- if opts.ScrollOff, err = atoi(value); err != nil {2975- return err2976- }2977- } else if match, value := optString(arg, "--jump-labels="); match {2978- opts.JumpLabels = value2979- validateJumpLabels = true2980- } else if match, value := optString(arg, "--tail="); match {2981- if opts.Tail, err = atoi(value); err != nil {2982- return err2983- }2984- if opts.Tail <= 0 {2985- return errors.New("number of items to keep must be a positive integer")2986- }2808 } else {2809 return errors.New("unknown option: " + arg)2810 }2811 }2812+2813+ if val != nil {2814+ return errors.New("unexpected value for " + arg + ": " + *val)2815+ }2816 }2817 *index += len(allArgs)2818 2819 if opts.HeaderLines < 0 {347 unmodified lines