From 2750bbb333f071a1fd40897a64e992965c414184 Mon Sep 17 00:00:00 2001 From: Arsham Shirvani Date: Mon, 11 Jun 2018 14:40:25 +0100 Subject: [PATCH] show usage of program (closes #3) --- README.md | 2 +- cmd/helper_test.go | 11 +++++++---- cmd/main.go | 19 ++++++++++++++++--- cmd/main_test.go | 15 +++++++++++++++ cmd/usage.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 cmd/usage.go diff --git a/README.md b/README.md index bae80c2..1139087 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ are not followed by colouring arguments are coloured based on previously provided colour: ```bash -$ blush -b match1 match3 FILENAME +$ blush -b match1 match2 FILENAME ``` ![Colored](http://i.imgur.com/J6uZPQD.png) diff --git a/cmd/helper_test.go b/cmd/helper_test.go index 7d39122..574af3d 100644 --- a/cmd/helper_test.go +++ b/cmd/helper_test.go @@ -56,10 +56,12 @@ func setup(t *testing.T, args string) (stdout, stderr *stdFile, cleanup func()) if len(args) > 1 { os.Args = append(os.Args, strings.Split(args, " ")...) } - logFatalErr := func(msg ...interface{}) { + fatalPatch := monkey.Patch(log.Fatal, func(msg ...interface{}) { fmt.Fprintln(os.Stderr, msg) - } - patch := monkey.Patch(log.Fatal, logFatalErr) + }) + fatalfPatch := monkey.Patch(log.Fatalf, func(format string, v ...interface{}) { + fmt.Fprintf(os.Stderr, format, v...) + }) cleanup = func() { outCleanup() @@ -67,7 +69,8 @@ func setup(t *testing.T, args string) (stdout, stderr *stdFile, cleanup func()) os.Args = oldArgs os.Stdout = oldStdout os.Stderr = oldStderr - patch.Unpatch() + fatalPatch.Unpatch() + fatalfPatch.Unpatch() } return stdout, stderr, cleanup } diff --git a/cmd/main.go b/cmd/main.go index f1ef18f..42168e4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "io" "log" "os" @@ -16,8 +17,13 @@ import ( // blush.Blush instance. It then uses io.Copy() to write to standard output. func Main() { b, err := GetBlush(os.Args) - if err != nil { - log.Fatal(err) + switch err { + case nil: + case errShowHelp: + fmt.Println(Usage) + return + default: + log.Fatalf("%s\n%s", err, Help) return // this return statement should be here to support tests. } defer func() { @@ -43,11 +49,18 @@ func GetBlush(input []string) (*blush.Blush, error) { ok bool noCut bool noFileName bool + remaining []string + err error + r io.ReadCloser ) if len(input) == 1 { return nil, ErrNoInput } - remaining, r, err := getReader(input[1:]) + if remaining, ok = hasArg(input[1:], "--help"); ok { + return nil, errShowHelp + } + + remaining, r, err = getReader(remaining) if err != nil { return nil, err } diff --git a/cmd/main_test.go b/cmd/main_test.go index 8aa9c7f..902305e 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -21,6 +21,21 @@ func TestMainNoArgs(t *testing.T) { if !strings.Contains(stderr.String(), cmd.ErrNoInput.Error()) { t.Errorf("stderr = `%s`, want `%s` in it", stderr.String(), cmd.ErrNoInput.Error()) } + if !strings.Contains(stderr.String(), cmd.Help) { + t.Errorf("stderr = `%s`, want `%s` in it", stderr.String(), cmd.Help) + } +} + +func TestMainHelp(t *testing.T) { + stdout, stderr, cleanup := setup(t, "--help") + defer cleanup() + cmd.Main() + if len(stderr.String()) > 0 { + t.Errorf("didn't expect any stderr, got: %s", stderr.String()) + } + if !strings.Contains(stdout.String(), cmd.Usage) { + t.Errorf("stdout = `%s`, want `%s` in it", stdout.String(), cmd.Usage) + } } func TestPipeInput(t *testing.T) { diff --git a/cmd/usage.go b/cmd/usage.go new file mode 100644 index 0000000..1ea8803 --- /dev/null +++ b/cmd/usage.go @@ -0,0 +1,46 @@ +package cmd + +import "errors" + +// These variables are used for showing help messages on command line. +var ( + errShowHelp = errors.New("show errors") + + Help = "Usage: blush [OPTION]... PATTERN [FILE]...\nTry 'blush --help' for more information." + Usage = `Usage: blush [OPTION]... PATTERN [FILE]... +Colours: + -r, --red Match decorated with red colour. See Stock Colours section. + -r[G], --red[G] Matches are grouped with the group number. + Example: blush -b1 match filename + -#RGB, --#RGB Use user defined colour schemas. + Example: blush -#1eF match filename + -#RRGGBB, --#RRGGBB Same as -#RGB/--#RGB. + +Pattern: + You can use simple pattern or regexp. If your pattern expands between + multiple words or has space in between, you should put them in quotations. + +Stock Colours: + -r, --red + -g, --green + -b, --blue + -w, --white + -bl, --black + -yl, --yellow + -mg, --magenta + -cy, --cyan + + +Control arguments: + -C, --colour Don't drop unmatched lines. + -i Case insensitive match. + --no-colour, --no-color Don't colourise the output. + -h, --no-filename Suppress the prefixing of file names on output. + +Multi match colouring: + blush -b match1 [match2]...: will colourise all matches with the same colour. + +Using pipes: + cat FILE | blush -b match [-g match]... +` +)