Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
ref #2 change cli library to ogier/pflag.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed May 16, 2019
1 parent dcf57ae commit e0a438f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 71 deletions.
4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@


[[constraint]]
name = "github.com/urfave/cli"
version = "1.20.0"
name = "github.com/ogier/pflag"
version = "0.0.1"

[prune]
go-tests = true
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GO=go
NAME := uniq2
VERSION := 0.2.0
VERSION := 1.0.0
REVISION := $(shell git rev-parse --short HEAD)
LDFLAGS := -X 'main.version=$(VERSION)'
-X 'main.revision=$(REVISION)'
Expand All @@ -21,6 +21,9 @@ setup: deps update_version
git submodule update --init

update_version:
@for i in README.md docs/content/_index.md; do\
sed -e 's!Version-[0-9.]*-yellowgreen!Version-${VERSION}-yellowgreen!g' -e 's!tag/v[0-9.]*!tag/v${VERSION}!g' $$i > a ; mv a $$i; \
done
@sed 's/const VERSION = .*/const VERSION = "${VERSION}"/g' cmd/uniq2/main.go > a
@mv a cmd/uniq2/main.go
@echo "Replace version to \"${VERSION}\""
Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![codebeat badge](https://codebeat.co/badges/855266ea-99d4-4d80-ac43-81a1712f0f90)](https://codebeat.co/projects/github-com-tamada-uniq2-master)
[![Go Report Card](https://goreportcard.com/badge/github.com/tamada/uniq2)](https://goreportcard.com/report/github.com/tamada/uniq2)
[![License](https://img.shields.io/badge/License-WTFPL-blue.svg)](https://github.com/tamada/uniq2/blob/master/LICENSE)
[![Version](https://img.shields.io/badge/Version-0.1.0-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v0.1.0)
[![Version](https://img.shields.io/badge/Version-1.0.0-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v1.0.0)

# uniq2

Expand All @@ -17,12 +17,38 @@ We want to delete not continuous duplicated lines with remaining the order.

## Install

### Install by Homebrew

Simply type the following commands.

```sh
$ brew tap tamada/brew
$ brew install uniq2
```

### Install by Go

Simply type the following command.

```sh
$ go get github.com/tamada/uniq2
```

## Usage

```
uniq2 [OPTIONS] [INPUT [OUTPUT]]
OPTIONS
-a, --adjacent delete only adjacent duplicated lines.
-d, --delete-lines only prints deleted lines.
-i, --ignore-case case sensitive.
-h, --help print this message.
INPUT gives file name of input. If argument is single dash ('-')
or absent, the program read strings from stdin.
OUTPUT represents the destination.
```

## License

[WTFPL](https://github.com/tamada/uniq2/blob/master/LICENSE)
94 changes: 28 additions & 66 deletions cmd/uniq2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,98 +4,60 @@ import (
"fmt"
"os"

flag "github.com/ogier/pflag"
"github.com/tamada/uniq2/lib"
"github.com/urfave/cli"
)

const VERSION = "0.2.0"
const VERSION = "1.0.0"

func printHelp(app *cli.App) {
fmt.Printf(`%s
func printHelp(appName string) {
fmt.Printf(`%s [OPTIONS] [INPUT [OUTPUT]]
OPTIONS
-a, --adjacent delete only adjacent duplicated lines.
-c, --show-counts show counts of deleted lines.
-d, --delete-lines only prints deleted lines.
-i, --ignore-case case sensitive.
-h, --help print this message.
INPUT gives file name of input. If argument is single dash ('-')
or absent, the program read strings from stdin.
OUTPUT represents the destination.
`, app.Usage)
`, appName)
}

func buildFlags() []cli.Flag {
return []cli.Flag{
cli.BoolFlag{
Name: "adjacent, a",
Usage: "delete only adjacent duplicated lines.",
},
cli.BoolFlag{
Name: "show-counts, c",
Usage: "show counts of deleted lines.",
},
cli.BoolFlag{
Name: "delete-lines, d",
Usage: "only prints deleted lines.",
},
cli.BoolFlag{
Name: "ignore-case, i",
Usage: "case sensitive",
},
}
}

func constructApp() *cli.App {
var app = cli.NewApp()
app.Name = "uniq2"
app.Usage = "Eliminates duplicated lines"
app.UsageText = "uniq2 [OPTIONS] [INPUT [OUTPUT]]"
app.Version = VERSION
app.Flags = buildFlags()
app.Action = func(c *cli.Context) error {
return action(app, c)
}
return app
}

func parseOptions(c *cli.Context) *lib.Options {
return &lib.Options{
Adjacent: c.Bool("adjacent"),
ShowCounts: c.Bool("show-counts"),
DeleteLines: c.Bool("delete-lines"),
IgnoreCase: c.Bool("ignore-case"),
}
}

func perform(args *lib.Arguments) error {
func perform(flags *flag.FlagSet, opts *lib.Options) int {
var args, err = lib.NewArguments(opts, flags.Args()[1:])
defer args.Close()
return args.Perform()
}

func action(app *cli.App, c *cli.Context) error {
var options = parseOptions(c)
if c.Bool("help") {
printHelp(app)
return nil
if err == nil {
err = args.Perform()
}
var args, err = lib.NewArguments(options, c.Args())
if err != nil {
return err
fmt.Println(err.Error())
return 1
}
return perform(args)
return 0
}

func goMain() int {
var app = constructApp()
var err = app.Run(os.Args)
if err != nil {
fmt.Println(err.Error())
return 1
var flags, opts = buildFlagSet()
var err = flags.Parse(os.Args)
if err == nil {
return perform(flags, opts)
}
fmt.Println(err.Error())

return 0
}

func buildFlagSet() (*flag.FlagSet, *lib.Options) {
var opts = lib.Options{}
var flags = flag.NewFlagSet("uniq2", flag.ContinueOnError)
flags.Usage = func() { printHelp("uniq2") }
flags.BoolVarP(&opts.Adjacent, "adjacent", "a", false, "delete only the adjacent duplicate lines")
flags.BoolVarP(&opts.DeleteLines, "delete-lines", "d", false, "only prints deleted lines")
flags.BoolVarP(&opts.IgnoreCase, "ignore-case", "i", false, "case sensitive")
return flags, &opts
}

func main() {
// separates main function in order to run defers before exit.
var exitStatus = goMain()
Expand Down
2 changes: 1 addition & 1 deletion docs/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: uniq2
[![codebeat badge](https://codebeat.co/badges/855266ea-99d4-4d80-ac43-81a1712f0f90)](https://codebeat.co/projects/github-com-tamada-uniq2-master)
[![Go Report Card](https://goreportcard.com/badge/github.com/tamada/uniq2)](https://goreportcard.com/report/github.com/tamada/uniq2)
[![License](https://img.shields.io/badge/License-WTFPL-blue.svg)](https://github.com/tamada/uniq2/blob/master/LICENSE)
[![Version](https://img.shields.io/badge/Version-0.1.0-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v0.1.0)
[![Version](https://img.shields.io/badge/Version-1.0.0-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v1.0.0)

## Description

Expand Down

0 comments on commit e0a438f

Please sign in to comment.