simplified command line parsing

This commit is contained in:
flopp 2016-02-28 20:46:33 +01:00
parent ef877907d4
commit e8930d65bb
6 changed files with 47 additions and 30 deletions

View File

@ -91,7 +91,7 @@ The `--marker` option defines one or more map markers of the same style. Use mul
`MARKER_STYLES` consists of a set of style descriptors separated by the pipe character `|`:
- `color:COLOR` - where `COLOR` is either of the form `0xRRGGBB`, `0xRRGGBBAA`, or one of `black`, `blue`, `brown`, `green`, `orange`, `purple`, `red`, `yellow`, `white` (default: `red`)
- `size:SIZE` - where `SIZE` is one of `mid`, `small`, `tiny` (default: `mid`)
- `size:SIZE` - where `SIZE` is one of `mid`, `small`, `tiny`, or some number > 0 (default: `mid`)
- `label:LABEL` - where `LABEL` is an alpha numeric character, i.e. `A`-`Z`, `a`-`z`, `0`-`9`; (default: no label)
### Paths

15
area.go
View File

@ -32,21 +32,21 @@ func ParseAreaString(s string) (*Area, error) {
area.Weight = 5.0
for _, ss := range strings.Split(s, "|") {
if strings.HasPrefix(ss, "color:") {
if ok, suffix := hasPrefix(ss, "color:"); ok {
var err error
area.Color, err = ParseColorString(strings.TrimPrefix(ss, "color:"))
area.Color, err = ParseColorString(suffix)
if err != nil {
return nil, err
}
} else if strings.HasPrefix(ss, "fill:") {
} else if ok, suffix := hasPrefix(ss, "fill:"); ok {
var err error
area.Fill, err = ParseColorString(strings.TrimPrefix(ss, "fill:"))
area.Fill, err = ParseColorString(suffix)
if err != nil {
return nil, err
}
} else if strings.HasPrefix(ss, "weight:") {
} else if ok, suffix := hasPrefix(ss, "weight:"); ok {
var err error
area.Weight, err = strconv.ParseFloat(strings.TrimPrefix(ss, "weight:"), 64)
area.Weight, err = strconv.ParseFloat(suffix, 64)
if err != nil {
return nil, err
}
@ -80,15 +80,12 @@ func (p *Area) draw(gc *gg.Context, trans *transformer) {
}
gc.ClearPath()
gc.SetLineWidth(p.Weight)
gc.SetLineCap(gg.LineCapRound)
gc.SetLineJoin(gg.LineJoinRound)
for _, ll := range p.Positions {
gc.LineTo(trans.ll2p(ll))
}
gc.ClosePath()
gc.SetColor(p.Fill)
gc.FillPreserve()

View File

@ -9,6 +9,7 @@ import (
"fmt"
"image/color"
"math"
"strconv"
"strings"
"github.com/flopp/go-coordsparser"
@ -36,14 +37,19 @@ func NewMarker(pos s2.LatLng, col color.Color, size float64) *Marker {
}
func parseSizeString(s string) (float64, error) {
if s == "mid" {
switch {
case s == "mid":
return 16.0, nil
} else if s == "small" {
case s == "small":
return 12.0, nil
} else if s == "tiny" {
case s == "tiny":
return 8.0, nil
}
if ss, err := strconv.ParseFloat(s, 64); err != nil && ss > 0 {
return ss, nil
}
return 0.0, fmt.Errorf("Cannot parse size string: %s", s)
}
@ -56,17 +62,17 @@ func ParseMarkerString(s string) ([]*Marker, error) {
label := ""
for _, ss := range strings.Split(s, "|") {
if strings.HasPrefix(ss, "color:") {
if ok, suffix := hasPrefix(ss, "color:"); ok {
var err error
color, err = ParseColorString(strings.TrimPrefix(ss, "color:"))
color, err = ParseColorString(suffix)
if err != nil {
return nil, err
}
} else if strings.HasPrefix(ss, "label:") {
label = strings.TrimPrefix(ss, "label:")
} else if strings.HasPrefix(ss, "size:") {
} else if ok, suffix := hasPrefix(ss, "label:"); ok {
label = suffix
} else if ok, suffix := hasPrefix(ss, "size:"); ok {
var err error
size, err = parseSizeString(strings.TrimPrefix(ss, "size:"))
size, err = parseSizeString(suffix)
if err != nil {
return nil, err
}
@ -95,7 +101,6 @@ func (m *Marker) bounds() s2.Rect {
func (m *Marker) draw(gc *gg.Context, trans *transformer) {
gc.ClearPath()
gc.SetLineJoin(gg.LineJoinRound)
gc.SetLineWidth(1.0)

11
path.go
View File

@ -30,15 +30,15 @@ func ParsePathString(s string) (*Path, error) {
path.Weight = 5.0
for _, ss := range strings.Split(s, "|") {
if strings.HasPrefix(ss, "color:") {
if ok, suffix := hasPrefix(ss, "color:"); ok {
var err error
path.Color, err = ParseColorString(strings.TrimPrefix(ss, "color:"))
path.Color, err = ParseColorString(suffix)
if err != nil {
return nil, err
}
} else if strings.HasPrefix(ss, "weight:") {
} else if ok, suffix := hasPrefix(ss, "weight:"); ok {
var err error
path.Weight, err = strconv.ParseFloat(strings.TrimPrefix(ss, "weight:"), 64)
path.Weight, err = strconv.ParseFloat(suffix, 64)
if err != nil {
return nil, err
}
@ -72,15 +72,12 @@ func (p *Path) draw(gc *gg.Context, trans *transformer) {
}
gc.ClearPath()
gc.SetLineWidth(p.Weight)
gc.SetLineCap(gg.LineCapRound)
gc.SetLineJoin(gg.LineJoinRound)
for _, ll := range p.Positions {
gc.LineTo(trans.ll2p(ll))
}
gc.SetColor(p.Color)
gc.Stroke()
}

View File

@ -13,6 +13,7 @@ import (
_ "image/png" // to be able to decode pngs
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
@ -78,9 +79,8 @@ func (t *TileFetcher) Fetch(zoom, x, y int) (image.Image, error) {
if t.useCaching {
fileName := t.cacheFileName(zoom, x, y)
if err = t.storeCache(fileName, data); err != nil {
fmt.Println("Failed to store image as", fileName)
fmt.Println(err)
if err := t.storeCache(fileName, data); err != nil {
log.Printf("Failed to store map tile as '%s': %s", fileName, err)
}
}

18
util.go Normal file
View File

@ -0,0 +1,18 @@
// Copyright 2016 Florian Pigorsch. All rights reserved.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package sm
import (
"strings"
)
// hasPrefix checks if 's' has prefix 'prefix'; returns 'true' and the remainder on success, and 'false', 's' otherwise.
func hasPrefix(s string, prefix string) (bool, string) {
if strings.HasPrefix(s, prefix) {
return true, strings.TrimPrefix(s, prefix)
}
return false, s
}