adding code to handle various cli arguments for hawkgo.go
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
added hawk.Exec() for *Rtx
This commit is contained in:
@@ -38,8 +38,6 @@ hawk_LDFLAGS = $(LDFLAGS_COMMON)
|
||||
##
|
||||
hawk_LDADD = $(LIBADD_COMMON) $(LIBM)
|
||||
|
||||
|
||||
|
||||
if ENABLE_HAWKGO
|
||||
|
||||
## the attempt to compose a proper procedure using a regular compiler failed.
|
||||
@@ -52,9 +50,9 @@ if ENABLE_HAWKGO
|
||||
|
||||
bin_PROGRAMS += hawkgo
|
||||
hawkgo_SOURCES = \
|
||||
../go.mod \
|
||||
../hawk.go \
|
||||
../hawk-inst.go \
|
||||
getopt.go \
|
||||
hawkgo.go
|
||||
##hawkgo_DEPENDENCIES =
|
||||
|
||||
@@ -71,16 +69,16 @@ CGO_LDFLAGS_ADD=""
|
||||
endif
|
||||
|
||||
hawkgo$(EXEEXT): ../lib/libhawk.la $(hawkgo_OBJECTS)
|
||||
cp -pf $(top_srcdir)/go.mod $(builddir)/go.mod >/dev/null 2>&1 || true
|
||||
chmod u+w $(builddir)/go.mod ## with `make distcheck`, the echo's redirection to the file fails without this permission change
|
||||
##cp -pf $(top_srcdir)/go.mod $(builddir)/go.mod >/dev/null 2>&1 || true
|
||||
##chmod u+w $(builddir)/go.mod ## with `make distcheck`, the echo's redirection to the file fails without this permission change
|
||||
[ -f $(srcdir)/go.sum ] && cp -pf $(srcdir)/go.sum $(builddir)/go.sum >/dev/null 2>&1 || true
|
||||
## ---------------------------------------------------------------
|
||||
CC=$(CC) \
|
||||
CGO_CFLAGS="-I$(abs_top_srcdir)/lib -I$(abs_top_builddir)/lib $(CFLAGS) $(CGO_CFLAGS_EXTRA) $(CGO_CFLAGS_ADD)" \
|
||||
CGO_LDFLAGS="-L$(abs_top_builddir)/lib -L$(abs_top_builddir)/lib/.libs -lhawk -ldl $(LIBM) $(CGO_LDFLAGS_EXTRA) $(CGO_LDFLAGS_ADD)" \
|
||||
go build -C $(srcdir) -ldflags "-X 'main.BINDIR=$(bindir)' -X 'main.SBINDIR=$(sbindir)' -X 'main.LIBDIR=$(libdir)' -X 'main.SYSCONFDIR=$(sysconfdir)'" -x -o $(abs_builddir)/$@ -modfile $(abs_builddir)/go.mod $(abs_srcdir)/hawkgo.go
|
||||
go build -C $(srcdir) -ldflags "-X 'main.BINDIR=$(bindir)' -X 'main.SBINDIR=$(sbindir)' -X 'main.LIBDIR=$(libdir)' -X 'main.SYSCONFDIR=$(sysconfdir)'" -x -o $(abs_builddir)/$@ -modfile $(abs_top_srcdir)/go.mod $(abs_srcdir)/hawkgo.go $(abs_srcdir)/getopt.go
|
||||
## ---------------------------------------------------------------
|
||||
go clean -C $(srcdir) -x -modfile $(abs_builddir)/go.mod
|
||||
go clean -C $(srcdir) -x -modfile $(abs_top_srcdir)/go.mod
|
||||
|
||||
## the go to o recipe is fake to deceive make
|
||||
.go.o:
|
||||
|
||||
@@ -125,11 +125,11 @@ am__v_lt_1 =
|
||||
hawk_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
|
||||
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(hawk_CFLAGS) $(CFLAGS) \
|
||||
$(hawk_LDFLAGS) $(LDFLAGS) -o $@
|
||||
am__hawkgo_SOURCES_DIST = ../go.mod ../hawk.go ../hawk-inst.go \
|
||||
am__hawkgo_SOURCES_DIST = ../hawk.go ../hawk-inst.go getopt.go \
|
||||
hawkgo.go
|
||||
am__dirstamp = $(am__leading_dot)dirstamp
|
||||
@ENABLE_HAWKGO_TRUE@am_hawkgo_OBJECTS = ../go.$(OBJEXT) \
|
||||
@ENABLE_HAWKGO_TRUE@ ../hawk.$(OBJEXT) ../hawk-inst.$(OBJEXT) \
|
||||
@ENABLE_HAWKGO_TRUE@am_hawkgo_OBJECTS = ../hawk.$(OBJEXT) \
|
||||
@ENABLE_HAWKGO_TRUE@ ../hawk-inst.$(OBJEXT) getopt.$(OBJEXT) \
|
||||
@ENABLE_HAWKGO_TRUE@ hawkgo.$(OBJEXT)
|
||||
hawkgo_OBJECTS = $(am_hawkgo_OBJECTS)
|
||||
hawkgo_LDADD = $(LDADD)
|
||||
@@ -372,9 +372,9 @@ hawk_CFLAGS = $(CFLAGS_COMMON)
|
||||
hawk_LDFLAGS = $(LDFLAGS_COMMON)
|
||||
hawk_LDADD = $(LIBADD_COMMON) $(LIBM)
|
||||
@ENABLE_HAWKGO_TRUE@hawkgo_SOURCES = \
|
||||
@ENABLE_HAWKGO_TRUE@ ../go.mod \
|
||||
@ENABLE_HAWKGO_TRUE@ ../hawk.go \
|
||||
@ENABLE_HAWKGO_TRUE@ ../hawk-inst.go \
|
||||
@ENABLE_HAWKGO_TRUE@ getopt.go \
|
||||
@ENABLE_HAWKGO_TRUE@ hawkgo.go
|
||||
|
||||
@ENABLE_HAWKGO_TRUE@hawkgo_LINK =
|
||||
@@ -469,7 +469,6 @@ hawk$(EXEEXT): $(hawk_OBJECTS) $(hawk_DEPENDENCIES) $(EXTRA_hawk_DEPENDENCIES)
|
||||
../$(DEPDIR)/$(am__dirstamp):
|
||||
@$(MKDIR_P) ../$(DEPDIR)
|
||||
@: >>../$(DEPDIR)/$(am__dirstamp)
|
||||
../go.$(OBJEXT): ../$(am__dirstamp) ../$(DEPDIR)/$(am__dirstamp)
|
||||
../hawk.$(OBJEXT): ../$(am__dirstamp) ../$(DEPDIR)/$(am__dirstamp)
|
||||
../hawk-inst.$(OBJEXT): ../$(am__dirstamp) \
|
||||
../$(DEPDIR)/$(am__dirstamp)
|
||||
@@ -802,14 +801,12 @@ uninstall-am: uninstall-binPROGRAMS
|
||||
|
||||
|
||||
@ENABLE_HAWKGO_TRUE@hawkgo$(EXEEXT): ../lib/libhawk.la $(hawkgo_OBJECTS)
|
||||
@ENABLE_HAWKGO_TRUE@ cp -pf $(top_srcdir)/go.mod $(builddir)/go.mod >/dev/null 2>&1 || true
|
||||
@ENABLE_HAWKGO_TRUE@ chmod u+w $(builddir)/go.mod ## with `make distcheck`, the echo's redirection to the file fails without this permission change
|
||||
@ENABLE_HAWKGO_TRUE@ [ -f $(srcdir)/go.sum ] && cp -pf $(srcdir)/go.sum $(builddir)/go.sum >/dev/null 2>&1 || true
|
||||
@ENABLE_HAWKGO_TRUE@ CC=$(CC) \
|
||||
@ENABLE_HAWKGO_TRUE@ CGO_CFLAGS="-I$(abs_top_srcdir)/lib -I$(abs_top_builddir)/lib $(CFLAGS) $(CGO_CFLAGS_EXTRA) $(CGO_CFLAGS_ADD)" \
|
||||
@ENABLE_HAWKGO_TRUE@ CGO_LDFLAGS="-L$(abs_top_builddir)/lib -L$(abs_top_builddir)/lib/.libs -lhawk -ldl $(LIBM) $(CGO_LDFLAGS_EXTRA) $(CGO_LDFLAGS_ADD)" \
|
||||
@ENABLE_HAWKGO_TRUE@ go build -C $(srcdir) -ldflags "-X 'main.BINDIR=$(bindir)' -X 'main.SBINDIR=$(sbindir)' -X 'main.LIBDIR=$(libdir)' -X 'main.SYSCONFDIR=$(sysconfdir)'" -x -o $(abs_builddir)/$@ -modfile $(abs_builddir)/go.mod $(abs_srcdir)/hawkgo.go
|
||||
@ENABLE_HAWKGO_TRUE@ go clean -C $(srcdir) -x -modfile $(abs_builddir)/go.mod
|
||||
@ENABLE_HAWKGO_TRUE@ go build -C $(srcdir) -ldflags "-X 'main.BINDIR=$(bindir)' -X 'main.SBINDIR=$(sbindir)' -X 'main.LIBDIR=$(libdir)' -X 'main.SYSCONFDIR=$(sysconfdir)'" -x -o $(abs_builddir)/$@ -modfile $(abs_top_srcdir)/go.mod $(abs_srcdir)/hawkgo.go $(abs_srcdir)/getopt.go
|
||||
@ENABLE_HAWKGO_TRUE@ go clean -C $(srcdir) -x -modfile $(abs_top_srcdir)/go.mod
|
||||
|
||||
@ENABLE_HAWKGO_TRUE@.go.o:
|
||||
@ENABLE_HAWKGO_TRUE@ echo $< > $@
|
||||
|
||||
424
bin/getopt.go
Normal file
424
bin/getopt.go
Normal file
@@ -0,0 +1,424 @@
|
||||
// Copyright 2017 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package getopt parses command lines using getopt(3) syntax.
|
||||
// It is a replacement for flag.Parse but still expects flags themselves
|
||||
// to be defined in package flag.
|
||||
//
|
||||
// Flags defined with one-letter names are available as short flags
|
||||
// (invoked using one dash, as in -x) and all flags are available as
|
||||
// long flags (invoked using two dashes, as in --x or --xylophone).
|
||||
//
|
||||
// To use, define flags as usual with package flag. Then introduce
|
||||
// any aliases by calling getopt.Alias:
|
||||
//
|
||||
// getopt.Alias("n", "dry-run")
|
||||
// getopt.Alias("v", "verbose")
|
||||
//
|
||||
// Or call getopt.Aliases to define a list of aliases:
|
||||
//
|
||||
// getopt.Aliases(
|
||||
// "n", "dry-run",
|
||||
// "v", "verbose",
|
||||
// )
|
||||
//
|
||||
// One name in each pair must already be defined in package flag
|
||||
// (so either "n" or "dry-run", and also either "v" or "verbose").
|
||||
//
|
||||
// Then parse the command-line:
|
||||
//
|
||||
// getopt.Parse()
|
||||
//
|
||||
// If it encounters an error, Parse calls flag.Usage and then exits the program.
|
||||
//
|
||||
// When writing a custom flag.Usage function, call getopt.PrintDefaults
|
||||
// instead of flag.PrintDefaults to get a usage message that includes the
|
||||
// names of aliases in flag descriptions.
|
||||
//
|
||||
// At initialization time, this package installs a new flag.Usage that is the
|
||||
// same as the default flag.Usage except that it calls getopt.PrintDefaults
|
||||
// instead of flag.PrintDefaults.
|
||||
//
|
||||
// This package also defines a FlagSet wrapping the standard flag.FlagSet.
|
||||
//
|
||||
// Caveat
|
||||
//
|
||||
// In general Go flag parsing is preferred for new programs, because
|
||||
// it is not as pedantic about the number of dashes used to invoke
|
||||
// a flag (you can write -verbose or --verbose and the program
|
||||
// does not care). This package is meant to be used in situations
|
||||
// where, for legacy reasons, it is important to use exactly getopt(3)
|
||||
// syntax, such as when rewriting in Go an existing tool that already
|
||||
// uses getopt(3).
|
||||
package main // import "rsc.io/getopt"
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
||||
PrintDefaults() // ours not package flag's
|
||||
}
|
||||
|
||||
CommandLine.FlagSet = flag.CommandLine
|
||||
CommandLine.name = os.Args[0]
|
||||
CommandLine.errorHandling = flag.ExitOnError
|
||||
CommandLine.outw = os.Stderr
|
||||
CommandLine.Usage = func() { flag.Usage() }
|
||||
}
|
||||
|
||||
var CommandLine FlagSet
|
||||
|
||||
// A FlagSet is a set of defined flags.
|
||||
// It wraps and provides the same interface as flag.FlagSet
|
||||
// but parses command line arguments using getopt syntax.
|
||||
//
|
||||
// Note that "go doc" shows only the methods customized
|
||||
// by package getopt; FlagSet also provides all the methods
|
||||
// of the embedded flag.FlagSet, like Bool, Int, NArg, and so on.
|
||||
type FlagSet struct {
|
||||
*flag.FlagSet
|
||||
|
||||
alias map[string]string
|
||||
unalias map[string]string
|
||||
name string
|
||||
errorHandling flag.ErrorHandling
|
||||
outw io.Writer
|
||||
}
|
||||
|
||||
func (f *FlagSet) out() io.Writer {
|
||||
if f.outw == nil {
|
||||
return os.Stderr
|
||||
}
|
||||
return f.outw
|
||||
}
|
||||
|
||||
// SetOutput sets the destination for usage and error messages.
|
||||
// If output is nil, os.Stderr is used.
|
||||
func (f *FlagSet) SetOutput(output io.Writer) {
|
||||
f.FlagSet.SetOutput(output)
|
||||
f.outw = output
|
||||
}
|
||||
|
||||
// NewFlagSet returns a new, empty flag set with the specified name and error
|
||||
// handling property.
|
||||
func NewFlagSet(name string, errorHandling flag.ErrorHandling) *FlagSet {
|
||||
f := new(FlagSet)
|
||||
f.Init(name, errorHandling)
|
||||
return f
|
||||
}
|
||||
|
||||
// Init sets the name and error handling proprety for a flag set.
|
||||
func (f *FlagSet) Init(name string, errorHandling flag.ErrorHandling) {
|
||||
if f.FlagSet == nil {
|
||||
f.FlagSet = new(flag.FlagSet)
|
||||
}
|
||||
f.FlagSet.Init(name, errorHandling)
|
||||
f.name = name
|
||||
f.errorHandling = errorHandling
|
||||
f.FlagSet.Usage = f.defaultUsage
|
||||
}
|
||||
|
||||
func (f *FlagSet) init() {
|
||||
if f.alias == nil {
|
||||
f.alias = make(map[string]string)
|
||||
f.unalias = make(map[string]string)
|
||||
}
|
||||
}
|
||||
|
||||
// Lookup returns the Flag structure of the named flag,
|
||||
// returning nil if none exists.
|
||||
// If name is a defined alias for a defined flag,
|
||||
// Lookup returns the original flag; in this case
|
||||
// the Name field in the result will differ from the
|
||||
// name passed to Lookup.
|
||||
func (f *FlagSet) Lookup(name string) *flag.Flag {
|
||||
if x, ok := f.alias[name]; ok {
|
||||
name = x
|
||||
}
|
||||
return f.FlagSet.Lookup(name)
|
||||
}
|
||||
|
||||
// Alias introduces an alias for an existing flag name.
|
||||
// The short name must be a single letter, and the long name must be multiple letters.
|
||||
// Exactly one name must be defined as a flag already: the undefined name is introduced
|
||||
// as an alias for the defined name.
|
||||
// Alias panics if both names are already defined or if both are undefined.
|
||||
//
|
||||
// For example, if a flag named "v" is already defined using package flag,
|
||||
// then it is available as -v (or --v). Calling Alias("v", "verbose") makes the same
|
||||
// flag also available as --verbose.
|
||||
func Alias(short, long string) {
|
||||
CommandLine.Alias(short, long)
|
||||
}
|
||||
|
||||
// Alias introduces an alias for an existing flag name.
|
||||
// The short name must be a single letter, and the long name must be multiple letters.
|
||||
// Exactly one name must be defined as a flag already: the undefined name is introduced
|
||||
// as an alias for the defined name.
|
||||
// Alias panics if both names are already defined or if both are undefined.
|
||||
//
|
||||
// For example, if a flag named "v" is already defined using package flag,
|
||||
// then it is available as -v (or --v). Calling Alias("v", "verbose") makes the same
|
||||
// flag also available as --verbose.
|
||||
func (f *FlagSet) Alias(short, long string) {
|
||||
f.init()
|
||||
if short == "" || long == "" {
|
||||
panic("Alias: invalid empty flag name")
|
||||
}
|
||||
if utf8.RuneCountInString(short) != 1 {
|
||||
panic("Alias: invalid short flag name -" + short)
|
||||
}
|
||||
if utf8.RuneCountInString(long) == 1 {
|
||||
panic("Alias: invalid long flag name --" + long)
|
||||
}
|
||||
|
||||
f1 := f.Lookup(short)
|
||||
f2 := f.Lookup(long)
|
||||
if f1 == nil && f2 == nil {
|
||||
panic("Alias: neither -" + short + " nor -" + long + " is a defined flag")
|
||||
}
|
||||
if f1 != nil && f2 != nil {
|
||||
panic("Alias: both -" + short + " and -" + long + " are defined flags")
|
||||
}
|
||||
|
||||
if f1 != nil {
|
||||
f.alias[long] = short
|
||||
f.unalias[short] = long
|
||||
} else {
|
||||
f.alias[short] = long
|
||||
f.unalias[long] = short
|
||||
}
|
||||
}
|
||||
|
||||
// Aliases introduces zero or more aliases. The argument list must consist of an
|
||||
// even number of strings making up a sequence of short, long pairs to be passed
|
||||
// to Alias.
|
||||
func Aliases(list ...string) {
|
||||
CommandLine.Aliases(list...)
|
||||
}
|
||||
|
||||
// Aliases introduces zero or more aliases. The argument list must consist of an
|
||||
// even number of strings making up a sequence of short, long pairs to be passed
|
||||
// to Alias.
|
||||
func (f *FlagSet) Aliases(list ...string) {
|
||||
if len(list)%2 != 0 {
|
||||
panic("getopt: Aliases not invoked with pairs")
|
||||
}
|
||||
for i := 0; i < len(list); i += 2 {
|
||||
f.Alias(list[i], list[i+1])
|
||||
}
|
||||
}
|
||||
|
||||
type boolFlag interface {
|
||||
IsBoolFlag() bool
|
||||
}
|
||||
|
||||
func (f *FlagSet) failf(format string, args ...interface{}) error {
|
||||
err := fmt.Errorf(format, args...)
|
||||
fmt.Fprintln(f.out(), err)
|
||||
f.Usage()
|
||||
return err
|
||||
}
|
||||
|
||||
// defaultUsage is the default function to print a usage message.
|
||||
func (f *FlagSet) defaultUsage() {
|
||||
if f.name == "" {
|
||||
fmt.Fprintf(f.out(), "Usage:\n")
|
||||
} else {
|
||||
fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
|
||||
}
|
||||
f.PrintDefaults()
|
||||
}
|
||||
|
||||
// Parse parses the command-line flags from os.Args[1:].
|
||||
func Parse() {
|
||||
CommandLine.Parse(os.Args[1:])
|
||||
}
|
||||
|
||||
// Parse parses flag definitions from the argument list,
|
||||
// which should not include the command name.
|
||||
// Parse must be called after all flags and aliases in the FlagSet are defined
|
||||
// and before flags are accessed by the program.
|
||||
// The return value will be flag.ErrHelp if -h or --help were used but not defined.
|
||||
func (f *FlagSet) Parse(args []string) error {
|
||||
for len(args) > 0 {
|
||||
arg := args[0]
|
||||
if len(arg) < 2 || arg[0] != '-' {
|
||||
break
|
||||
}
|
||||
args = args[1:]
|
||||
if arg[:2] == "--" {
|
||||
// Process single long option.
|
||||
if arg == "--" {
|
||||
break
|
||||
}
|
||||
name := arg[2:]
|
||||
value := ""
|
||||
haveValue := false
|
||||
if i := strings.Index(name, "="); i >= 0 {
|
||||
name, value = name[:i], name[i+1:]
|
||||
haveValue = true
|
||||
}
|
||||
fg := f.Lookup(name)
|
||||
if fg == nil {
|
||||
if name == "h" || name == "help" {
|
||||
// TODO ErrHelp
|
||||
}
|
||||
return f.failf("flag provided but not defined: --%s", name)
|
||||
}
|
||||
if b, ok := fg.Value.(boolFlag); ok && b.IsBoolFlag() {
|
||||
if haveValue {
|
||||
if err := fg.Value.Set(value); err != nil {
|
||||
return f.failf("invalid boolean value %q for --%s: %v", value, name, err)
|
||||
}
|
||||
} else {
|
||||
if err := fg.Value.Set("true"); err != nil {
|
||||
return f.failf("invalid boolean flag %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !haveValue {
|
||||
if len(args) == 0 {
|
||||
return f.failf("missing argument for --%s", name)
|
||||
}
|
||||
value, args = args[0], args[1:]
|
||||
}
|
||||
if err := fg.Value.Set(value); err != nil {
|
||||
return f.failf("invalid value %q for flag --%s: %v", value, name, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Process one or more short options.
|
||||
for arg = arg[1:]; arg != ""; {
|
||||
r, size := utf8.DecodeRuneInString(arg)
|
||||
if r == utf8.RuneError && size == 1 {
|
||||
return f.failf("invalid UTF8 in command-line flags")
|
||||
}
|
||||
name := arg[:size]
|
||||
arg = arg[size:]
|
||||
fg := f.Lookup(name)
|
||||
if fg == nil {
|
||||
if name == "h" {
|
||||
// TODO ErrHelp
|
||||
}
|
||||
return f.failf("flag provided but not defined: -%s", name)
|
||||
}
|
||||
if b, ok := fg.Value.(boolFlag); ok && b.IsBoolFlag() {
|
||||
if err := fg.Value.Set("true"); err != nil {
|
||||
return f.failf("invalid boolean flag %s: %v", name, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if arg == "" {
|
||||
if len(args) == 0 {
|
||||
return f.failf("missing argument for -%s", name)
|
||||
}
|
||||
arg, args = args[0], args[1:]
|
||||
}
|
||||
if err := fg.Value.Set(arg); err != nil {
|
||||
return f.failf("invalid value %q for flag -%s: %v", arg, name, err)
|
||||
}
|
||||
break // consumed arg
|
||||
}
|
||||
}
|
||||
|
||||
// Arrange for flag.NArg, flag.Args, etc to work properly.
|
||||
f.FlagSet.Parse(append([]string{"--"}, args...))
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrintDefaults is like flag.PrintDefaults but includes information
|
||||
// about short/long alias pairs and prints the correct syntax for
|
||||
// long flags.
|
||||
func PrintDefaults() {
|
||||
CommandLine.PrintDefaults()
|
||||
}
|
||||
|
||||
// PrintDefaults is like flag.PrintDefaults but includes information
|
||||
// about short/long alias pairs and prints the correct syntax for
|
||||
// long flags.
|
||||
func (f *FlagSet) PrintDefaults() {
|
||||
f.FlagSet.VisitAll(func(fg *flag.Flag) {
|
||||
name := fg.Name
|
||||
short, long := "", ""
|
||||
other := f.unalias[name]
|
||||
if utf8.RuneCountInString(name) > 1 {
|
||||
long, short = name, other
|
||||
} else {
|
||||
short, long = name, other
|
||||
}
|
||||
var s string
|
||||
if short != "" {
|
||||
s = fmt.Sprintf(" -%s", short) // Two spaces before -; see next two comments.
|
||||
if long != "" {
|
||||
s += ", --" + long
|
||||
}
|
||||
} else {
|
||||
s = fmt.Sprintf(" --%s", long) // Two spaces before -; see next two comments.
|
||||
}
|
||||
name, usage := flag.UnquoteUsage(fg)
|
||||
if len(name) > 0 {
|
||||
s += " " + name
|
||||
}
|
||||
|
||||
// Boolean flags of one ASCII letter are so common we
|
||||
// treat them specially, putting their usage on the same line.
|
||||
if len(s) <= 4 { // space, space, '-', 'x'.
|
||||
s += "\t"
|
||||
} else {
|
||||
// Four spaces before the tab triggers good alignment
|
||||
// for both 4- and 8-space tab stops.
|
||||
s += "\n \t"
|
||||
}
|
||||
s += usage
|
||||
if !isZeroValue(fg, fg.DefValue) {
|
||||
if strings.HasSuffix(reflect.TypeOf(fg.Value).String(), "stringValue") {
|
||||
// put quotes on the value
|
||||
s += fmt.Sprintf(" (default %q)", fg.DefValue)
|
||||
} else {
|
||||
s += fmt.Sprintf(" (default %v)", fg.DefValue)
|
||||
}
|
||||
}
|
||||
fmt.Fprint(f.out(), s, "\n")
|
||||
})
|
||||
}
|
||||
|
||||
// isZeroValue guesses whether the string represents the zero
|
||||
// value for a flag. It is not accurate but in practice works OK.
|
||||
func isZeroValue(f *flag.Flag, value string) bool {
|
||||
// Build a zero value of the flag's Value type, and see if the
|
||||
// result of calling its String method equals the value passed in.
|
||||
// This works unless the Value type is itself an interface type.
|
||||
typ := reflect.TypeOf(f.Value)
|
||||
var z reflect.Value
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
z = reflect.New(typ.Elem())
|
||||
} else {
|
||||
z = reflect.Zero(typ)
|
||||
}
|
||||
if value == z.Interface().(flag.Value).String() {
|
||||
return true
|
||||
}
|
||||
|
||||
switch value {
|
||||
case "false":
|
||||
return true
|
||||
case "":
|
||||
return true
|
||||
case "0":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -5,31 +5,34 @@ import "flag"
|
||||
import "fmt"
|
||||
import "io"
|
||||
import "os"
|
||||
import "path/filepath"
|
||||
import "runtime"
|
||||
import "runtime/debug"
|
||||
//import "sync"
|
||||
import "time"
|
||||
//import "time"
|
||||
|
||||
type Config struct {
|
||||
assign string
|
||||
call string
|
||||
fs string
|
||||
srcstr string
|
||||
srcfile string
|
||||
show_extra_info bool
|
||||
|
||||
files []string
|
||||
srcstr string
|
||||
srcfiles []string
|
||||
datfiles []string
|
||||
}
|
||||
|
||||
func exit_with_error(msg string, err error) {
|
||||
fmt.Printf("ERROR: %s - %s\n", msg, err.Error())
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %s - %s\n", msg, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func parse_args_to_config(cfg *Config) bool {
|
||||
var flgs *flag.FlagSet
|
||||
//var flgs *flag.FlagSet
|
||||
var flgs *FlagSet
|
||||
var err error
|
||||
|
||||
flgs = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
//flgs = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
flgs = NewFlagSet("", flag.ContinueOnError)
|
||||
flgs.Func("assign", "set a global variable with a value", func(v string) error {
|
||||
cfg.assign = v
|
||||
return nil
|
||||
@@ -42,16 +45,33 @@ func parse_args_to_config(cfg *Config) bool {
|
||||
cfg.fs = v
|
||||
return nil
|
||||
})
|
||||
flgs.Func("file", "set the source file", func(v string) error {
|
||||
cfg.srcfiles = append(cfg.srcfiles, v)
|
||||
return nil
|
||||
})
|
||||
flgs.BoolVar(&cfg.show_extra_info, "show-extra-info", false, "show extra information")
|
||||
|
||||
flgs.Alias("c", "call")
|
||||
flgs.Alias("D", "show-extra-info")
|
||||
flgs.Alias("F", "field-separator")
|
||||
flgs.Alias("f", "file")
|
||||
flgs.Alias("v", "assign")
|
||||
|
||||
flgs.SetOutput(io.Discard) // prevent usage output
|
||||
//err = flgs.Parse(os.Args[1:])
|
||||
err = flgs.Parse(os.Args[1:])
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err.Error())
|
||||
goto wrong_usage
|
||||
}
|
||||
|
||||
if flgs.NArg() == 0 { goto wrong_usage}
|
||||
cfg.files = flgs.Args()
|
||||
//if flgs.NArg() == 0 { goto wrong_usage}
|
||||
cfg.datfiles = flgs.Args()
|
||||
if len(cfg.srcfiles) <= 0 {
|
||||
if len(cfg.datfiles) == 0 { goto wrong_usage }
|
||||
cfg.srcstr = cfg.datfiles[0]
|
||||
cfg.datfiles = cfg.datfiles[1:]
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
@@ -88,34 +108,46 @@ func main() {
|
||||
if parse_args_to_config(&cfg) == false { os.Exit(99) }
|
||||
fmt.Printf("config [%+v]\n", cfg)
|
||||
|
||||
h, err = make_hawk(`function main(s) {
|
||||
print enbase64(s, "hello", 1.289);
|
||||
print debase64(s);
|
||||
return x
|
||||
}`)
|
||||
h, err = hawk.New()
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR: failed to make hawk - %s\n", err.Error())
|
||||
fmt.Fprintf(os.Stderr, "ERROR: failed to make hawk - %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
rtx, err = h.NewRtx("test3")
|
||||
if (len(cfg.srcfiles) > 0) {
|
||||
err = h.ParseFiles(cfg.srcfiles)
|
||||
} else {
|
||||
err = h.ParseText(cfg.srcstr)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR: failed to make rtx - %s\n", err.Error())
|
||||
fmt.Fprintf(os.Stderr, "ERROR: failed to make hawk - %s\n", err.Error())
|
||||
h.Close()
|
||||
return
|
||||
}
|
||||
|
||||
rtx, err = h.NewRtx(filepath.Base(os.Args[0]), cfg.datfiles)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: failed to make rtx - %s\n", err.Error())
|
||||
} else {
|
||||
var v *hawk.Val
|
||||
v, err = rtx.Call("main", hawk.Must(rtx.NewValFromStr("this is a test3 string")))
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR: failed to make rtx - %s\n", err.Error())
|
||||
if cfg.call != "" {
|
||||
v, err = rtx.Call(cfg.call/*, cfg.datfiles*/) // TODO: pass arguments.
|
||||
} else {
|
||||
fmt.Printf("V=>[%v]\n", v.String())
|
||||
//v, err = rtx.Loop()
|
||||
v, err = rtx.Exec(cfg.datfiles)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: failed to run rtx - %s\n", err.Error())
|
||||
} else if cfg.show_extra_info {
|
||||
fmt.Printf("[RETURN] - [%v]\n", v.String())
|
||||
// TODO: print global variables and values
|
||||
}
|
||||
}
|
||||
|
||||
h.Close()
|
||||
fmt.Printf ("END OF TEST3\n")
|
||||
|
||||
runtime.GC()
|
||||
runtime.Gosched()
|
||||
time.Sleep(1000 * time.Millisecond) // give finalizer time to print
|
||||
// time.Sleep(1000 * time.Millisecond) // give finalizer time to print
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user