Skip to content

Commit

Permalink
Parse arguments without pflag
Browse files Browse the repository at this point in the history
  • Loading branch information
candid82 committed Dec 10, 2017
1 parent a27b954 commit 5da9fb3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 90 deletions.
1 change: 0 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- ln -s ${HOME}/${CIRCLE_PROJECT_REPONAME} ${HOME}/.go_workspace/src/github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}
- go get -d -v github.com/chzyer/readline
- go get -d -v gopkg.in/yaml.v2
- go get -d -v github.com/spf13/pflag
- go generate -v ./...
- go build -v
- ./linter-tests.sh
Expand Down
1 change: 1 addition & 0 deletions core/procs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
CLJS
JOKER
EDN
UNKNOWN
)

func ensureArrayMap(args []Object, index int) *ArrayMap {
Expand Down
9 changes: 1 addition & 8 deletions core/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,14 +1095,7 @@ func Read(reader *Reader) (Object, bool) {
func TryRead(reader *Reader) (obj Object, err error) {
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case *EvalError:
err = r.(error)
case ReadError:
err = r.(error)
default:
panic(r)
}
err = r.(error)
}
}()
eatWhitespace(reader)
Expand Down
124 changes: 60 additions & 64 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
_ "github.com/candid82/joker/std/time"
_ "github.com/candid82/joker/std/yaml"
"github.com/chzyer/readline"
"github.com/spf13/pflag"
)

type (
Expand Down Expand Up @@ -196,79 +195,76 @@ func lintFile(filename string, dialect Dialect, workingDir string) {
}
}

var versionFlag = pflag.BoolP("version", "v", false, "display version information")
var readFlag = pflag.Bool("read", false, "read the file")
var parseFlag = pflag.Bool("parse", false, "parse the file")
var lintFlag = pflag.Bool("lint", false, "lint the file")

var lintDialect = pflag.String("dialect", "", "dialect to lint as. Valid options are clj, cljs, joker and edn")
var lintWorkingDir = pflag.String("working-dir", "", "override the working directory for the linter")

// these flags are here to support the preexisting `--lint<dialect>` flags:
var lintCljFlag = pflag.Bool("lintclj", false, "lint as clojure")
var lintCljsFlag = pflag.Bool("lintcljs", false, "lint as clojurescript")
var lintJokerFlag = pflag.Bool("lintjoker", false, "lint as joker")
var lintEDNFlag = pflag.Bool("lintedn", false, "lint as edn")

func usage() {
fmt.Fprintf(os.Stderr, "Joker - %s\n\n", VERSION)
fmt.Fprintln(os.Stderr, "usage: joker starts a repl")
fmt.Fprintln(os.Stderr, " or: joker [arguments] <filename> execute a script")
fmt.Fprintln(os.Stderr, "\nArguments:")
pflag.PrintDefaults()
}

func init() {
pflag.Usage = usage
pflag.Parse()

switch {
case *lintCljFlag:
*lintFlag = true
*lintDialect = "clj"
case *lintCljsFlag:
*lintFlag = true
*lintDialect = "cljs"
case *lintJokerFlag:
*lintFlag = true
*lintDialect = "joker"
case *lintEDNFlag:
*lintFlag = true
*lintDialect = "edn"
func dialectFromArg(arg string) Dialect {
switch strings.ToLower(arg) {
case "clj":
return CLJ
case "cljs":
return CLJS
case "joker":
return JOKER
case "edn":
return EDN
}
return UNKNOWN
}

func main() {
GLOBAL_ENV.FindNamespace(MakeSymbol("user")).ReferAll(GLOBAL_ENV.CoreNamespace)
switch {
case *versionFlag:
println(VERSION)
case *readFlag:
processFile(pflag.Arg(0), READ)
case *parseFlag:
processFile(pflag.Arg(0), PARSE)
case *lintFlag:
var dialect Dialect
switch strings.ToLower(*lintDialect) {
case "clj":
if len(os.Args) == 1 {
repl(EVAL)
return
}
if len(os.Args) == 2 {
if os.Args[1] == "-v" || os.Args[1] == "--version" {
println(VERSION)
return
}
processFile(os.Args[1], EVAL)
return
}
workingDir := ""
phase := EVAL
lint := false
dialect := UNKNOWN
length := len(os.Args) - 1
for i := 1; i < length; i++ {
switch os.Args[i] {
case "--read":
phase = READ
case "--parse":
phase = PARSE
case "--working-dir":
if i < length-1 {
workingDir = os.Args[i+1]
}
case "--lint":
lint = true
case "--lintclj":
lint = true
dialect = CLJ
case "cljs":
case "--lintcljs":
lint = true
dialect = CLJS
case "joker":
case "--lintjoker":
lint = true
dialect = JOKER
case "edn":
case "--lintedn":
lint = true
dialect = EDN
default:
dialect = detectDialect(pflag.Arg(0))
case "--dialect":
if i < length-1 {
dialect = dialectFromArg(os.Args[i+1])
}
}
filename := pflag.Arg(0)
if filename == "" {
filename = "--"
}
filename := os.Args[length]
if lint {
if dialect == UNKNOWN {
dialect = detectDialect(filename)
}
lintFile(filename, dialect, *lintWorkingDir)
case len(pflag.Args()) > 0:
processFile(pflag.Arg(0), EVAL)
default:
repl(EVAL)
lintFile(filename, dialect, workingDir)
return
}
processFile(filename, phase)
}
34 changes: 17 additions & 17 deletions tests/run-flag-tests.joke
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

(defn clean
[output]
(let [output (if (nil? output) "" output)]
(let [output (if (nil? output) "" output)]
(->> output
(joker.string/split-lines)
(remove #(joker.string/starts-with? % " "))
(remove #(= % ""))
(joker.string/join "\n"))))
(joker.string/split-lines)
(remove #(joker.string/starts-with? % " "))
(remove #(= % ""))
(joker.string/join "\n"))))

(defn test-flags
[description flags expected]
Expand All @@ -31,8 +31,8 @@
(defn testing
[description & tests]
(let [tests (partition 2 tests)]
(doall (map (fn [[flags expected]]
(test-flags description flags expected)) tests))))
(doseq [[flags expected] tests]
(test-flags description flags expected))))

(testing "auto detect dialect from filename"
"--lint tests/flags/input.clj"
Expand Down Expand Up @@ -80,32 +80,32 @@
"tests/flags/input.clj:1:2: Parse error: Unable to resolve symbol: clojure.string/split")

(testing "reading from stdin"
"--lint --dialect edn < tests/flags/input.edn"
"--lint --dialect edn -- < tests/flags/input.edn"
""

"--lint --dialect clj < tests/flags/input.edn"
"--lint --dialect clj -- < tests/flags/input.edn"
"<stdin>:1:17: Read warning: No reader function for tag foo/bar"

"--lint --dialect clj < tests/flags/input.clj"
"--lint --dialect clj -- < tests/flags/input.clj"
""

"--lint --dialect cljs < tests/flags/input.clj"
"--lint --dialect cljs -- < tests/flags/input.clj"
"<stdin>:1:2: Parse error: Unable to resolve symbol: clojure.string/split"

"--lint --dialect cljs < tests/flags/input.cljs"
"--lint --dialect cljs -- < tests/flags/input.cljs"
""

"--lint --dialect joker < tests/flags/input.cljs"
"--lint --dialect joker -- < tests/flags/input.cljs"
"<stdin>:1:7: Parse error: Unable to resolve symbol: js/console"

"--lint --dialect joker < tests/flags/input.joke"
"--lint --dialect joker -- < tests/flags/input.joke"
"")

(testing "working direction override"
"--lint --dialect clj < tests/flags/macro.clj"
(testing "working directory override"
"--lint --dialect clj -- < tests/flags/macro.clj"
"<stdin>:4:11: Parse error: Unable to resolve symbol: something"

"--lint --dialect clj --working-dir tests/flags/config < tests/flags/macro.clj"
"--lint --dialect clj --working-dir tests/flags/config -- < tests/flags/macro.clj"
"")

(joker.os/exit exit-code)

0 comments on commit 5da9fb3

Please sign in to comment.