~yerinalexey/rex

07c5375b34439bee3555057844a5830dae581b7d — Alexey Yerin 25 days ago 59e48c3
Use pflag module for option parsing

The default flag module makes long options a bit confusing by adding a
single dash before them. With pflag, long options have --option and -o
for short options.
4 files changed, 24 insertions(+), 17 deletions(-)

M README.md
M go.mod
M go.sum
M main.go
M README.md => README.md +10 -6
@@ 31,7 31,7 @@ of test data highlighted accordingly to them matching a regex.

You can switch between them using `Tab` or `Shift+Tab`. `Escape` exits
the application and prints input regex to standard output unless
`-print-regex=0` flag is specified.
`--print-regex=0` flag is specified.

List can be scrolled using Vi keys: `j` - down, `k` - up. Input field
supports readline-like bindings: [reference][inputfield].


@@ 39,12 39,16 @@ supports readline-like bindings: [reference][inputfield].
[inputfield]: https://godocs.io/github.com/rivo/tview#InputField

Flags/options reference:
- `-input REGEX` - set default value for regex field (`.` if not set)
- `-print-regex` - print regex after exit (`true` by default)
- `-version` - print version number
- `-help` - show help message
- `--input REGEX` (`-i REGEX`) - set default value for regex field (`.` if not set)
- `--print-regex` (`-p`) - print regex after exit (`true` by default)
- `--version` (`-V`) - print version number
- `--help` (`-h`) - show help message

[Command line flag syntax](https://godocs.io/flag#hdr-Command_line_flag_syntax)
To turn off boolean flags, you can assign them to a value of zero after an equal
sign: `-p=0` or `--print-regex=false`.

The following values can be accepted by boolean flags: 1, 0, t, f, true, false,
TRUE, FALSE, True, False.

# Contributing
Send patches using [git send-email][gitsendemail] to my

M go.mod => go.mod +1 -0
@@ 5,4 5,5 @@ go 1.15
require (
	github.com/gdamore/tcell/v2 v2.0.1-0.20201017141208-acf90d56d591
	github.com/rivo/tview v0.0.0-20201204190810-5406288b8e4e
	github.com/spf13/pflag v1.0.5
)

M go.sum => go.sum +2 -0
@@ 11,6 11,8 @@ github.com/rivo/tview v0.0.0-20201204190810-5406288b8e4e h1:eP1XZiExUPO/FjS2q/PB
github.com/rivo/tview v0.0.0-20201204190810-5406288b8e4e/go.mod h1:0ha5CGekam8ZV1kxkBxSlh7gfQ7YolUj2P/VruwH0QY=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7 h1:XtNJkfEjb4zR3q20BBBcYUykVOEMgZeIUOpBPfNYgxg=
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

M main.go => main.go +11 -11
@@ 18,7 18,6 @@ package main

import (
	"bufio"
	"flag"
	"fmt"
	"io/ioutil"
	"os"


@@ 29,6 28,7 @@ import (

	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
	"github.com/spf13/pflag"
)

const (


@@ 114,25 114,25 @@ func readFile(fname string) (string, error) {
}

func main() {
	flag.Usage = func() {
	pflag.Usage = func() {
		fmt.Fprintln(os.Stderr, "Usage: rex [flags] [file]")
		fmt.Fprintln(os.Stderr)
		fmt.Fprintln(os.Stderr, "Flags:")

		flag.PrintDefaults()
		pflag.PrintDefaults()
	}

	opts := Options{}

	// TODO: More flags
	flag.StringVar(&opts.input, "input", ".", "Default value for regex input")
	flag.BoolVar(&opts.printRegex, "print-regex", true, "Print regex after exit")
	flag.BoolVar(&opts.version, "version", false, "Show version number")
	flag.BoolVar(&opts.help, "help", false, "Show this message")
	flag.Parse()
	pflag.StringVarP(&opts.input, "input", "i", ".", "Default value for regex input")
	pflag.BoolVarP(&opts.printRegex, "print-regex", "p", true, "Print regex after exit")
	pflag.BoolVarP(&opts.version, "version", "V", false, "Show version number")
	pflag.BoolVarP(&opts.help, "help", "h", false, "Show this message")
	pflag.Parse()

	if opts.help {
		flag.Usage()
		pflag.Usage()
		return
	}



@@ 141,7 141,7 @@ func main() {
		return
	}

	file := flag.Arg(0)
	file := pflag.Arg(0)

	var data string



@@ 158,7 158,7 @@ func main() {
	}

	if data == "" {
		flag.Usage()
		pflag.Usage()
		return
	}