File: args.janet

package info (click to toggle)
cloc 2.06-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,064 kB
  • sloc: perl: 30,146; cpp: 1,219; python: 623; ansic: 334; asm: 267; makefile: 244; sh: 186; sql: 144; java: 136; ruby: 111; cs: 104; pascal: 52; lisp: 50; haskell: 35; f90: 35; cobol: 35; objc: 25; php: 22; javascript: 15; fortran: 9; ml: 8; xml: 7; tcl: 2
file content (55 lines) | stat: -rw-r--r-- 1,455 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# https://github.com/MorganPeterson/args/blob/main/args.janet

# https://github.com/AlDanial/cloc/issues/802
(defn- to-byte
  "convert chars to byte and only return first result"
  [x]
  (get (string/bytes x) 0))

(defn- parse
  "parse command line arguments"
  [args]
  (def f
    (fiber/new 
      (fn []
        (loop [x :in args]
          (yield x)))))
  (def dash (to-byte "-"))
  (def spce (to-byte " "))

  (var results @[])
  (var kpwh false) # keep flag whole if no dash
  
  (while (fiber/can-resume? f)
    (var args0 (resume f))

    (cond
      (= args0 dash) (set args0 (resume f))
      (set kpwh true))
    
    (if (and (= args0 dash) (not (fiber/can-resume? f))) (break))
    
    (cond
      (= args0 dash)
      (do
        (var a @[])
        (loop [x :in f :until (or (= x spce) (not (fiber/can-resume? f)))]
          (array/push a (string/from-bytes x)))
        (array/push results (string/join a)))
      (do
        (var b @[])
        (when (not (nil? args0))
          (array/push b (string/from-bytes args0))
          (loop [x :in f :until (or (= x spce) (not (fiber/can-resume? f)))]
            (array/push b (string/from-bytes x)))
          (cond
            (true? kpwh) (array/push results (string/join b))
            (do
              (array/concat results b)
              (set kpwh false)))))))
  results)

(defn flags
  "take in raw commandline string"
  [args]
  (parse (string/join (tuple/slice args 1) " ")))