First working code
parent
a8fd764e17
commit
d5589c8423
@ -0,0 +1,6 @@
|
||||
// ebkTools/cli project doc.go
|
||||
|
||||
/*
|
||||
cli document
|
||||
*/
|
||||
package cli
|
@ -0,0 +1,28 @@
|
||||
// ebkTools/cli project ebkTools.go
|
||||
package cli
|
||||
|
||||
import "os"
|
||||
|
||||
//type universal interface{}
|
||||
|
||||
func MapCLIArguments() *map[string]string {
|
||||
cliAguments := os.Args[1:] //without the program path itself
|
||||
|
||||
//var cliAgumentsMapped map[string]universal = make(map[string]universal)
|
||||
var cliAgumentsMapped map[string]string = make(map[string]string)
|
||||
|
||||
//Dismantle list of command line arguments
|
||||
for key, value := range cliAguments {
|
||||
if value[0:1] == "-" {
|
||||
if len(cliAguments) > key+1 && cliAguments[key+1][0:1] != "-" {
|
||||
cliAgumentsMapped[cliAguments[key]] = cliAguments[key+1] //; fmt.Printf("Argument mit Wert %v = %v\n", cliAguments[key], cliAguments[key+1])
|
||||
|
||||
} else {
|
||||
//cliAgumentsMapped[cliAguments[key]] = true
|
||||
cliAgumentsMapped[cliAguments[key]] = "" //; fmt.Printf("Argument ohne Wert %v\n", cliAguments[key])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return &cliAgumentsMapped // test it with: fmt.Println((*cli.MapCLIArguments())["-a"])
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMapCLIArguments(t *testing.T) {
|
||||
var ergebnisSoll1, ergebnisSoll2, ergebnisSoll3 map[string]string
|
||||
ergebnisSoll1 = make(map[string]string)
|
||||
ergebnisSoll2 = make(map[string]string)
|
||||
ergebnisSoll3 = map[string]string{
|
||||
"-test.cpuprofile=cpu.prof": "",
|
||||
"-test.memprofile=mem.prof": "",
|
||||
"-test.bench=.": "",
|
||||
"-test.outputdir": "./",
|
||||
}
|
||||
ergebnisIst := *MapCLIArguments()
|
||||
ergebnisSoll1["-test.v=true"] = ""
|
||||
ergebnisSoll2["-test.bench=.*"] = ""
|
||||
|
||||
if !reflect.DeepEqual(ergebnisIst, ergebnisSoll1) && !reflect.DeepEqual(ergebnisIst, ergebnisSoll2) && !reflect.DeepEqual(ergebnisIst, ergebnisSoll3) {
|
||||
t.Error("Erwartung:", ergebnisSoll1, "Ergebnis:", ergebnisIst)
|
||||
}
|
||||
}
|
||||
|
||||
//Namenskonvention: Benchmark…
|
||||
func BenchmarkMapCLIArguments(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = *MapCLIArguments()
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
// ebkStrings project doc.go
|
||||
|
||||
/*
|
||||
ebkStrings document
|
||||
*/
|
||||
package ebkStrings
|
@ -0,0 +1,40 @@
|
||||
// ebkStrings project strings.go
|
||||
package ebkStrings
|
||||
|
||||
import "bytes"
|
||||
|
||||
func Concatstring(args ...string) string {
|
||||
var buffer bytes.Buffer
|
||||
for _, v := range args {
|
||||
buffer.WriteString(v)
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
type Concatablestring string
|
||||
|
||||
func (f *Concatablestring) Join(args ...string) string {
|
||||
var buffer bytes.Buffer
|
||||
buffer.WriteString(string(*f))
|
||||
for _, v := range args {
|
||||
buffer.WriteString(v)
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
/*
|
||||
package main
|
||||
|
||||
import (
|
||||
"ebkTools/ebkStrings"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(ebkStrings.Concatstring("a", "b", "c"))
|
||||
|
||||
teststring := (ebkStrings.Concatablestring)("Anfangswert ")
|
||||
|
||||
fmt.Println(teststring.Join("gefolgt ", "von ", "meinem ", "Text"))
|
||||
}
|
||||
*/
|
Loading…
Reference in New Issue