Skip to content

Commit

Permalink
implement cli parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed May 12, 2023
1 parent ba60ce4 commit 75237d7
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ VERSION := 0.1.16
NAME := urleap
DIST := $(NAME)-$(VERSION)

urleap: coverage.out
go build -o urleap $(PACKAGE_LIST)
urleap: coverage.out cmd/urleap/main.go
go build -o urleap cmd/urleap/main.go

coverage.out:
coverage.out: cmd/urleap/main_test.go
go test -covermode=count \
-coverprofile=coverage.out $(PACKAGE_LIST)

Expand Down
92 changes: 91 additions & 1 deletion cmd/urleap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,102 @@ package main
import (
"fmt"
"os"
"path/filepath"

flag "github.com/spf13/pflag"
)

const VERSION = "0.1.16"

func goMain(args []string) int {
/*
helpMessage prints the help message.
This function is used in the small tests, so it may be called with a zero-length slice.
*/
func helpMessage(args []string) string {
prog := "urleap"
if len(args) > 0 {
prog = filepath.Base(args[0])
}
return fmt.Sprintf(`%s [OPTIONS] [URLs...]
OPTIONS
-t, --token <TOKEN> specify the token for the service. This option is mandatory.
-q, --qrcode <FILE> include QR-code of the URL in the output.
-c, --config <CONFIG> specify the configuration file.
-h, --help print this mesasge and exit.
-v, --version print the version and exit.
ARGUMENT
URL specify the url for shortening. this arguments accept multiple values.
if no arguments were specified, urleap prints the list of available shorten urls.`, prog)
}

type UrleapError struct {
statusCode int
message string
}

func (e UrleapError) Error() string {
return e.message
}

/*
This struct holds the values of the options.
*/
type options struct {
token string
qrcode string
config string
help bool
version bool
}

/*
Define the options and return the pointer to the options and the pointer to the flagset.
*/
func buildOptions(args []string) (*options, *flag.FlagSet) {
opts := &options{}
flags := flag.NewFlagSet(args[0], flag.ContinueOnError)
flags.Usage = func() { fmt.Println(helpMessage(args)) }
flags.StringVarP(&opts.token, "token", "t", "", "specify the token for the service. This option is mandatory.")
flags.StringVarP(&opts.qrcode, "qrcode", "q", "", "include QR-code of the URL in the output.")
flags.StringVarP(&opts.config, "config", "c", "", "specify the configuration file.")
flags.BoolVarP(&opts.help, "help", "h", false, "print this mesasge and exit.")
flags.BoolVarP(&opts.version, "version", "v", false, "print the version and exit.")
return opts, flags
}

/*
parseOptions parses options from the given command line arguments.
*/
func parseOptions(args []string) (*options, []string, *UrleapError) {
opts, flags := buildOptions(args)
flags.Parse(args[1:])
if opts.help {
fmt.Println(helpMessage(args))
return nil, nil, &UrleapError{statusCode: 0, message: ""}
}
if opts.token == "" {
return nil, nil, &UrleapError{statusCode: 3, message: "no token was given"}
}
return opts, flags.Args(), nil
}

func perform(opts *options, args []string) *UrleapError {
fmt.Println("Hello World")
return nil
}

func goMain(args []string) int {
opts, args, err := parseOptions(args)
if err != nil {
if err.statusCode != 0 {
fmt.Println(err.Error())
}
return err.statusCode
}
if err := perform(opts, args); err != nil {
fmt.Println(err.Error())
return err.statusCode
}
return 0
}

Expand Down
19 changes: 17 additions & 2 deletions cmd/urleap/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ package main
import "testing"

func Example_Main() {
goMain([]string{})
goMain([]string{"./urleap", "-t", "token"})
// Output:
// Hello World
}

func Example_Help() {
goMain([]string{"./urleap", "--help"})
// Output:
// urleap [OPTIONS] [URLs...]
// OPTIONS
// -t, --token <TOKEN> specify the token for the service. This option is mandatory.
// -q, --qrcode <FILE> include QR-code of the URL in the output.
// -c, --config <CONFIG> specify the configuration file.
// -h, --help print this mesasge and exit.
// -v, --version print the version and exit.
// ARGUMENT
// URL specify the url for shortening. this arguments accept multiple values.
// if no arguments were specified, urleap prints the list of available shorten urls.
}

func Test_Main(t *testing.T) {
if status := goMain([]string{}); status != 0 {
if status := goMain([]string{"./urleap", "-t", "token"}); status != 0 {
t.Error("Expected 0, got ", status)
}
}
1 change: 1 addition & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/tamada/urleap

go 1.20

require github.com/spf13/pflag v1.0.5 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=

0 comments on commit 75237d7

Please sign in to comment.