Relicensed under MIT.

This commit is contained in:
Stanislav N. aka pztrn 2018-03-23 08:14:25 +05:00
parent c9b4726e17
commit 0754a665e1
8 changed files with 236 additions and 209 deletions

View File

@ -1,8 +0,0 @@
stages:
- test
test_job:
stage: test
tags:
- linux1
script: go test -test.v .

3
.travis.yml Normal file
View File

@ -0,0 +1,3 @@
language: go
go:
- "1.10"

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright 2017-2018, Stanislav N. aka pztrn
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,29 +1,34 @@
// Flagger - arbitrary CLI flags parser.
//
// Copyright (c) 2017, Stanislav N. aka pztrn.
// All rights reserved.
// Copyright (c) 2017-2018, Stanislav N. aka pztrn.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package flagger
var (
logger LoggerInterface
logger LoggerInterface
)
func New(l LoggerInterface) *Flagger {
logger = l
f := Flagger{}
return &f
logger = l
f := Flagger{}
return &f
}

45
flag.go
View File

@ -1,31 +1,36 @@
// Flagger - arbitrary CLI flags parser.
//
// Copyright (c) 2017, Stanislav N. aka pztrn.
// All rights reserved.
// Copyright (c) 2017-2018, Stanislav N. aka pztrn.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package flagger
// This structure represents addable flag for Flagger.
type Flag struct {
// Flag name. It will be accessible using this name later.
Name string
// Description for help output.
Description string
// Type can be one of "bool", "int", "string".
Type string
// This value will be reflected.
DefaultValue interface{}
// Flag name. It will be accessible using this name later.
Name string
// Description for help output.
Description string
// Type can be one of "bool", "int", "string".
Type string
// This value will be reflected.
DefaultValue interface{}
}

View File

@ -1,28 +1,33 @@
// Flagger - arbitrary CLI flags parser.
//
// Copyright (c) 2017, Stanislav N. aka pztrn.
// All rights reserved.
// Copyright (c) 2017-2018, Stanislav N. aka pztrn.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package flagger
import (
// stdlib
"errors"
"flag"
"sync"
// stdlib
"errors"
"flag"
"sync"
)
// Flagger implements (kinda) extended CLI parameters parser. As it
@ -32,90 +37,90 @@ import (
// It uses reflection to determine what kind of variable we should
// parse or get.
type Flagger struct {
// Flags that was added by user.
flags map[string]*Flag
flagsMutex sync.Mutex
// Flags that was added by user.
flags map[string]*Flag
flagsMutex sync.Mutex
// Flags that will be passed to flag module.
flagsBool map[string]*bool
flagsInt map[string]*int
flagsString map[string]*string
// Flags that will be passed to flag module.
flagsBool map[string]*bool
flagsInt map[string]*int
flagsString map[string]*string
}
// Adds flag to list of flags we will pass to ``flag`` package.
func (f *Flagger) AddFlag(flag *Flag) error {
_, present := f.flags[flag.Name]
if present {
logger.Fatalln("Cannot add flag '" + flag.Name + "' - already added!")
return errors.New("Cannot add flag '" + flag.Name + "' - already added!")
}
_, present := f.flags[flag.Name]
if present {
logger.Fatalln("Cannot add flag '" + flag.Name + "' - already added!")
return errors.New("Cannot add flag '" + flag.Name + "' - already added!")
}
f.flags[flag.Name] = flag
return nil
f.flags[flag.Name] = flag
return nil
}
// This function returns boolean value for flag with given name.
// Returns bool value for flag and nil as error on success
// and false bool plus error with text on error.
func (f *Flagger) GetBoolValue(name string) (bool, error) {
fl, present := f.flagsBool[name]
if !present {
return false, errors.New("No such flag: " + name)
}
return (*fl), nil
fl, present := f.flagsBool[name]
if !present {
return false, errors.New("No such flag: " + name)
}
return (*fl), nil
}
// This function returns integer value for flag with given name.
// Returns integer on success and 0 on error.
func (f *Flagger) GetIntValue(name string) (int, error) {
fl, present := f.flagsInt[name]
if !present {
return 0, errors.New("No such flag: " + name)
}
return (*fl), nil
fl, present := f.flagsInt[name]
if !present {
return 0, errors.New("No such flag: " + name)
}
return (*fl), nil
}
// This function returns string value for flag with given name.
// Returns string on success or empty string on error.
func (f *Flagger) GetStringValue(name string) (string, error) {
fl, present := f.flagsString[name]
if !present {
return "", errors.New("No such flag: " + name)
}
return (*fl), nil
fl, present := f.flagsString[name]
if !present {
return "", errors.New("No such flag: " + name)
}
return (*fl), nil
}
// Flagger initialization.
func (f *Flagger) Initialize() {
logger.Println("Initializing CLI parameters parser...")
logger.Println("Initializing CLI parameters parser...")
f.flags = make(map[string]*Flag)
f.flags = make(map[string]*Flag)
f.flagsBool = make(map[string]*bool)
f.flagsInt = make(map[string]*int)
f.flagsString = make(map[string]*string)
f.flagsBool = make(map[string]*bool)
f.flagsInt = make(map[string]*int)
f.flagsString = make(map[string]*string)
}
// This function adds flags from flags map to flag package and parse
// them. They can be obtained later by calling GetTYPEValue(name),
// where TYPE is one of Bool, Int, String.
func (f *Flagger) Parse() {
for name, fl := range f.flags {
if fl.Type == "bool" {
fdef := fl.DefaultValue.(bool)
f.flagsBool[name] = &fdef
flag.BoolVar(&fdef, name, fdef, fl.Description)
} else if fl.Type == "int" {
fdef := fl.DefaultValue.(int)
f.flagsInt[name] = &fdef
flag.IntVar(&fdef, name, fdef, fl.Description)
} else if fl.Type == "string" {
fdef := fl.DefaultValue.(string)
f.flagsString[name] = &fdef
flag.StringVar(&fdef, name, fdef, fl.Description)
}
}
for name, fl := range f.flags {
if fl.Type == "bool" {
fdef := fl.DefaultValue.(bool)
f.flagsBool[name] = &fdef
flag.BoolVar(&fdef, name, fdef, fl.Description)
} else if fl.Type == "int" {
fdef := fl.DefaultValue.(int)
f.flagsInt[name] = &fdef
flag.IntVar(&fdef, name, fdef, fl.Description)
} else if fl.Type == "string" {
fdef := fl.DefaultValue.(string)
f.flagsString[name] = &fdef
flag.StringVar(&fdef, name, fdef, fl.Description)
}
}
logger.Println("Parsing CLI parameters...")
flag.Parse()
logger.Println("Parsing CLI parameters...")
flag.Parse()
}

View File

@ -1,125 +1,130 @@
// Flagger - arbitrary CLI flags parser.
//
// Copyright (c) 2017, Stanislav N. aka pztrn.
// All rights reserved.
// Copyright (c) 2017-2018, Stanislav N. aka pztrn.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package flagger
import (
// stdlib
"log"
"os"
"testing"
// stdlib
"log"
"os"
"testing"
)
var (
f *Flagger
f *Flagger
)
func TestFlaggerInitialization(t *testing.T) {
f = New(LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
if f == nil {
t.Fatal("Logger initialization failed!")
t.FailNow()
}
f.Initialize()
f = New(LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
if f == nil {
t.Fatal("Logger initialization failed!")
t.FailNow()
}
f.Initialize()
}
func TestFlaggerAddBoolFlag(t *testing.T) {
flag_testbool := Flag{
Name: "testboolflag",
Description: "Testing boolean flag",
Type: "bool",
DefaultValue: true,
}
err := f.AddFlag(&flag_testbool)
if err != nil {
t.Fatal("Failed to add boolean flag!")
t.FailNow()
}
flag_testbool := Flag{
Name: "testboolflag",
Description: "Testing boolean flag",
Type: "bool",
DefaultValue: true,
}
err := f.AddFlag(&flag_testbool)
if err != nil {
t.Fatal("Failed to add boolean flag!")
t.FailNow()
}
}
func TestFlaggerAddIntFlag(t *testing.T) {
flag_testint := Flag{
Name: "testintflag",
Description: "Testing integer flag",
Type: "int",
DefaultValue: 1,
}
err := f.AddFlag(&flag_testint)
if err != nil {
t.Fatal("Failed to add integer flag!")
t.FailNow()
}
flag_testint := Flag{
Name: "testintflag",
Description: "Testing integer flag",
Type: "int",
DefaultValue: 1,
}
err := f.AddFlag(&flag_testint)
if err != nil {
t.Fatal("Failed to add integer flag!")
t.FailNow()
}
}
func TestFlaggerAddStringFlag(t *testing.T) {
flag_teststring := Flag{
Name: "teststringflag",
Description: "Testing string flag",
Type: "string",
DefaultValue: "superstring",
}
err := f.AddFlag(&flag_teststring)
if err != nil {
t.Fatal("Failed to add string flag!")
t.FailNow()
}
flag_teststring := Flag{
Name: "teststringflag",
Description: "Testing string flag",
Type: "string",
DefaultValue: "superstring",
}
err := f.AddFlag(&flag_teststring)
if err != nil {
t.Fatal("Failed to add string flag!")
t.FailNow()
}
}
// This test doing nothing more but launching flags parsing.
func TestFlaggerParse(t *testing.T) {
f.Parse()
f.Parse()
}
func TestFlaggerGetBoolFlag(t *testing.T) {
val, err := f.GetBoolValue("testboolflag")
if err != nil {
t.Fatal("Failed to get boolean flag: " + err.Error())
t.FailNow()
}
val, err := f.GetBoolValue("testboolflag")
if err != nil {
t.Fatal("Failed to get boolean flag: " + err.Error())
t.FailNow()
}
if !val {
t.Fatal("Failed to get boolean flag - should be true, but false received")
t.FailNow()
}
if !val {
t.Fatal("Failed to get boolean flag - should be true, but false received")
t.FailNow()
}
}
func TestFlaggerGetIntFlag(t *testing.T) {
val, err := f.GetIntValue("testintflag")
if err != nil {
t.Fatal("Failed to get integer flag: " + err.Error())
t.FailNow()
}
val, err := f.GetIntValue("testintflag")
if err != nil {
t.Fatal("Failed to get integer flag: " + err.Error())
t.FailNow()
}
if val == 0 {
t.Fatal("Failed to get integer flag - should be 1, but 0 received")
t.FailNow()
}
if val == 0 {
t.Fatal("Failed to get integer flag - should be 1, but 0 received")
t.FailNow()
}
}
func TestFlaggerGetStringFlag(t *testing.T) {
val, err := f.GetStringValue("teststringflag")
if err != nil {
t.Fatal("Failed to get string flag: " + err.Error())
t.FailNow()
}
val, err := f.GetStringValue("teststringflag")
if err != nil {
t.Fatal("Failed to get string flag: " + err.Error())
t.FailNow()
}
if val == "" {
t.Fatal("Failed to get string flag - should be 'superstring', but nothing received")
t.FailNow()
}
if val == "" {
t.Fatal("Failed to get string flag - should be 'superstring', but nothing received")
t.FailNow()
}
}

View File

@ -1,26 +1,31 @@
// Flagger - arbitrary CLI flags parser.
//
// Copyright (c) 2017, Stanislav N. aka pztrn.
// All rights reserved.
// Copyright (c) 2017-2018, Stanislav N. aka pztrn.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package flagger
// LoggerInterface provide logging interface, so everyone can inject own
// logging handlers.
type LoggerInterface interface {
Fatalln(args ...interface{})
Println(v ...interface{})
Fatalln(args ...interface{})
Println(v ...interface{})
}