2 Commits 6b6589dff5 ... 9f4bc3bb39

Author SHA1 Message Date
  Kevin Bloom 9f4bc3bb39 added a little help thingie 5 months ago
  Kevin Bloom d40819616d Closes #1 5 months ago
3 changed files with 76 additions and 61 deletions
  1. 14 0
      aledger
  2. 22 19
      balance.awk
  3. 40 42
      register.awk

+ 14 - 0
aledger

@@ -10,6 +10,20 @@ register_path=${AWK_PATH}register.awk
 reg=${AWK_PATH}reg_values.awk
 
 command=$1
+if test "$command" = ""; then
+   echo "Usage: aledger command [--start start_date] [--end end_date] file"
+   echo "Try \`aledger --help' for more information."
+   exit 1
+fi
+if test "$command" = "--help"; then
+  echo " "
+  echo "Usage: aledger command [--start start_date] [--end end_date] file"
+  echo " "
+  echo "command              balance,register"
+  echo "--start,--end        YYYY/MM/DD"
+  echo " "
+  exit 0
+fi
 shift
 
 case $command in

+ 22 - 19
balance.awk

@@ -19,27 +19,30 @@ $1 ~ "-" || $1 ~ "/", $1 == "\n" {
     isDate = 1;
     phase = "DATE"
     total = 0
+    skip = 0;
   }
-  if(phase == "DATE") {
-    if(isDate) {
-      if(start != "" && $1 < start)
-        next
-      if(end != "" && $1 > end)
-        next
+  if(!skip) {
+    if(phase == "DATE") {
+      if(isDate) {
+        if(start != "" && $1 < start)
+          skip = 1;
+        if(end != "" && $1 > end)
+          exit 0
+        isDate = 0;
+        entrytotal = 0;
+        phase = "ENTRIES";
+      } else {
+        print "aledger error, failed to parse date at on line " NR ": " $0
+        error = 1
+        exit 1
+      }
+    } else if(phase == "ENTRIES") {
       isDate = 0;
-      entrytotal = 0;
-      phase = "ENTRIES";
-    } else {
-      print "aledger error, failed to parse date at on line " NR ": " $0
-      error = 1
-      exit 1
-    }
-  } else if(phase == "ENTRIES") {
-    isDate = 0;
-    if($1 ~ ":") {
-      sub("\\$", "", $2)
-      entry[$1] += $2
-      total += $2
+      if($1 ~ ":") {
+        sub("\\$", "", $2)
+        entry[$1] += $2
+        total += $2
+      }
     }
   }
 }

+ 40 - 42
register.awk

@@ -46,53 +46,51 @@ $1 ~ "-" || $1 ~ "/", $1 == "\n" {
     total = 0
     skip = 0
   }
-  if(skip)
-    next
   split(cor, arr, "-")
-  if(phase == "DATE") {
-   if(isDate) {
-      if(start_date != "" && $1 < start_date) {
-        skip = 1
-        next
-      } else if(end_date != "" && $1 > end_date) {
-        skip = 1
-        next
-      } else {
-      if(type == "monthly") {
-        month = arr[1] "-" arr[2]
-        if(length(map[month]) == 0)
-          map[month] = month
-        entry = month
-      } else if(type == "weekly") {
-        for(i in weekly) {
-          split(weekly[i], dates, " -> ")
-          start = dates[1]
-          split(start, s, "-")
-          end = dates[2]
-          split(end, e, "-")
-          if(arr[2] >= s[1] &&
-             arr[2] <= e[1] &&
-             arr[3] >= s[2] &&
-             arr[3] <= e[2]) {
-            if(length(map[weekly[i]]) == 0)
-              map[weekly[i]] = weekly[i]
-            entry = weekly[i]
-            break;
+  if(!skip) {
+    if(phase == "DATE") {
+     if(isDate) {
+        if(start_date != "" && $1 < start_date) {
+          skip = 1
+        } else if(end_date != "" && $1 > end_date) {
+          exit 0
+        } else {
+        if(type == "monthly") {
+          month = arr[1] "-" arr[2]
+          if(length(map[month]) == 0)
+            map[month] = month
+          entry = month
+        } else if(type == "weekly") {
+          for(i in weekly) {
+            split(weekly[i], dates, " -> ")
+            start = dates[1]
+            split(start, s, "-")
+            end = dates[2]
+            split(end, e, "-")
+            if(arr[2] >= s[1] &&
+               arr[2] <= e[1] &&
+               arr[3] >= s[2] &&
+               arr[3] <= e[2]) {
+              if(length(map[weekly[i]]) == 0)
+                map[weekly[i]] = weekly[i]
+              entry = weekly[i]
+              break;
+            }
           }
+        } else {
+          map[$0] = cor " " substr($0, 12)
+          entry = $0
+        }
+        phase = "ENTRIES"
         }
       } else {
-        map[$0] = cor " " substr($0, 12)
-        entry = $0
-      }
-      phase = "ENTRIES"
-      }
-    } else {
-      print "Error with date on line " NR ": " $0
-      error = 1
+        print "Error with date on line " NR ": " $0
+        error = 1
+    }
+    } else if(!skip && phase == "ENTRIES") {
+      isDate = 0;
+      map[entry] = map[entry] "<<>>" $0
     }
-  } else if(!skip && phase == "ENTRIES") {
-    isDate = 0;
-    map[entry] = map[entry] "<<>>" $0
   }
 }
 END {