summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUriel <u@berlinblue.org>2012-08-24 17:00:53 +0200
committerUriel <u@berlinblue.org>2012-08-24 17:00:53 +0200
commitc087fa15289e5821dd802a66564937abea7247d2 (patch)
treec378cc38a6e42b52c5f4f1fc35cffaeb63d16359
parent6e9eaed6b2db772f5343f7b1105142d553c4562c (diff)
Initial version of seq(1), -w is missing, still not sure how it should
work exactly.
-rw-r--r--seq/main.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/seq/main.go b/seq/main.go
new file mode 100644
index 0000000..111d070
--- /dev/null
+++ b/seq/main.go
@@ -0,0 +1,87 @@
+package main
+
+import (
+ "os"
+ "flag"
+ "fmt"
+ "strconv"
+)
+
+
+var pad = flag.Bool("w", false, "Equalize the widths of all numbers by padding with leading zeros as necessary.")
+var format = flag.String("f", "%g", "Use the print(3)-style format for printing each number.")
+
+
+func main() {
+
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+
+ firsts := "1"
+ incrs := "1"
+ var lasts string
+ switch len(args) {
+ case 1:
+ lasts = args[0]
+ case 2:
+ incrs = args[0]
+ lasts = args[1]
+ case 3:
+ firsts = args[0]
+ incrs = args[1]
+ lasts = args[2]
+ default:
+ usage()
+ }
+
+ first, err := strconv.ParseFloat(firsts, 64)
+ if err != nil {
+ fail(err)
+ }
+ incr, err := strconv.ParseFloat(incrs, 64)
+ if err != nil {
+ fail(err)
+ }
+ last, err := strconv.ParseFloat(lasts, 64)
+ if err != nil {
+ fail(err)
+ }
+
+ if incr == 0 {
+ fmt.Fprintf(os.Stderr, "seq: a zero increment will take you nowhere!\n")
+ }
+
+
+ f := *format+"\n"
+
+ /* TODO
+ if pad {
+ maxi := 0
+ for c := first; c <= last; c += incr {
+ maxl = len(fmt.Sprintf("%g", c))
+ }
+ } */
+ if incr > 0 {
+ for c := first; c <= last; c += incr {
+ fmt.Printf(f, c)
+ }
+ } else {
+ for c := first; c >= last; c += incr {
+ fmt.Printf(f, c)
+ }
+ }
+
+}
+
+
+func fail(err error) {
+ fmt.Fprintf(os.Stderr, "%s\n", err)
+ os.Exit(2)
+}
+
+func usage() {
+ fmt.Print("usage: seq [-fformat] [-w] [first [incr]] last")
+ flag.PrintDefaults()
+ os.Exit(2)
+}