Skip to content

Commit

Permalink
use gotgo for string slices.
Browse files Browse the repository at this point in the history
Tested-on: morland i686 GNU/Linux
  • Loading branch information
droundy committed Mar 8, 2010
1 parent 829fae9 commit 2d1252d
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 12 deletions.
6 changes: 0 additions & 6 deletions .build
Expand Up @@ -2,12 +2,6 @@

set -ev

# first check that the Makefile will actually run.
make

# now let's just compile things by hand.
8g goopt.go
8g testit.go
8l -o testit testit.8

echo build successful!
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
*.[8]
*.[865a]
*~
testit
_obj
2 changes: 2 additions & 0 deletions .test
Expand Up @@ -2,6 +2,8 @@

set -ev

make test

./testit | grep BOO
./testit | grep 'name is anonymous'
./testit | grep 'I am unhappy'
Expand Down
59 changes: 55 additions & 4 deletions Makefile
@@ -1,7 +1,58 @@
# Copyright 2010 David Roundy, roundyd@physics.oregonstate.edu.
# All rights reserved.

all: Makefile packages

Makefile: scripts/make.header $(wildcard *.go)
cp -f scripts/make.header $@
gotmake >> $@

test: install testit

install: installbins installpkgs


include $(GOROOT)/src/Make.$(GOARCH)

TARG=goopt
GOFILES=\
goopt.go\
binaries:
packages: pkg/goopt.a

ifndef GOBIN
GOBIN=$(HOME)/bin
endif

# ugly hack to deal with whitespaces in $GOBIN
nullstring :=
space := $(nullstring) # a space at the end
bindir=$(subst $(space),\ ,$(GOBIN))
pkgdir=$(subst $(space),\ ,$(GOROOT)/pkg/$(GOOS)_$(GOARCH))

.PHONY: test binaries packages install installbins installpkgs
.SUFFIXES: .$(O) .go .got .gotgo

.go.$(O):
cd `dirname "$<"`; $(GC) `basename "$<"`
.got.gotgo:
gotgo "$<"

# looks like we require gotgo/slice.got as installed package...
gotgo/slice(string).go: $(pkgdir)/./gotgo/slice.gotgo
mkdir -p gotgo/
$< 'string' > "$@"
goopt.$(O): goopt.go gotgo/slice(string).$(O)

pkg/goopt.$(O): pkg/goopt.go
pkg/goopt.a: pkg/goopt.$(O)
gopack grc $@ $<
$(pkgdir)/goopt.a: pkg/goopt.a
mkdir -p $(pkgdir)/
cp $< $@


testit: testit.$(O)
@mkdir -p bin
$(LD) -o $@ $<
testit.$(O): testit.go

include $(GOROOT)/src/Make.pkg
installbins:
installpkgs: $(pkgdir)/goopt.a
18 changes: 18 additions & 0 deletions goopt.go → pkg/goopt.go
Expand Up @@ -173,6 +173,16 @@ func String(name string, d string, help string) *string {
return s
}

func Strings(names []string, d string, help string) []string {
s := make([]string,0,100)
f := func(ss string) os.Error {
addString(ss, &s)
return nil
}
ReqArg(names, d, help, f)
return s
}

func Flag(yes []string, no []string, helpyes, helpno string) *bool {
b := new(bool)
y := func() os.Error {
Expand All @@ -196,12 +206,17 @@ func failnoting(s string, e os.Error) {
}
}

var Args []string

func Parse() {
addOpt(opt{[]string{"--help"}, "", "show usage message", false, "",
func(string) (e os.Error) { _,e = fmt.Println(Usage()); os.Exit(0); return }})
for i:=0; i<len(os.Args);i++ {
a := os.Args[i]
if a == "--" {
for _,aa := range os.Args[i:len(Args)] {
addString(aa,&Args)
}
break
}
if len(a) > 1 && a[0] == '-' && a[1] != '-' {
Expand Down Expand Up @@ -260,6 +275,9 @@ func Parse() {
if !foundone && len(a) > 2 && a[0] == '-' && a[1] == '-' {
failnoting("Bad flag:", os.NewError(a))
}
if !foundone {
addString(a,&Args)
}
}
}
}
12 changes: 12 additions & 0 deletions scripts/make.header
@@ -0,0 +1,12 @@
# Copyright 2010 David Roundy, roundyd@physics.oregonstate.edu.
# All rights reserved.

all: Makefile packages

Makefile: scripts/make.header $(wildcard *.go)
cp -f scripts/make.header $@
gotmake >> $@

test: install testit

install: installbins installpkgs
2 changes: 1 addition & 1 deletion testit.go
Expand Up @@ -4,7 +4,7 @@ package main

import (
"fmt"
"./goopt"
"goopt"
)

var amVerbose = goopt.Bool("--verbose", false, "output verbosely")
Expand Down

0 comments on commit 2d1252d

Please sign in to comment.