Skip to content

Commit 730945c

Browse files
authored
Merge pull request #2 from decaf-lang/dev
fix CLI option validators
2 parents 994b52e + 07dafa4 commit 730945c

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/main/scala/decaf/driver/OptParser.scala

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ object OptParser extends OptionParser[Config]("decaf") {
1919
.text("input Decaf source")
2020
.action { case (f, config) => config.copy(source = f) }
2121
.validate { f =>
22-
if (!f.isFile) {
23-
Left("not a file")
24-
} else if (!f.exists) {
25-
Left("not exist")
22+
if (!f.exists) {
23+
Left("not exist: " + f)
24+
} else if (!f.isFile) {
25+
Left("not a file: " + f)
2626
} else {
2727
Right()
2828
}
@@ -33,10 +33,8 @@ object OptParser extends OptionParser[Config]("decaf") {
3333
.text("output file for result, available except PA5 (default stdout)")
3434
.action { case (o, config) => config.copy(output = new FileOutputStream(o)) }
3535
.validate { f =>
36-
if (!f.isFile) {
37-
Left("not a file")
38-
} else if (!f.getParentFile.exists) {
39-
Left("directory not exist")
36+
if (f.getParentFile != null && !f.getParentFile.exists) {
37+
Left("parent directory not exist: " + f.getParentFile)
4038
} else {
4139
Right()
4240
}
@@ -47,10 +45,10 @@ object OptParser extends OptionParser[Config]("decaf") {
4745
.text("output directory for low-level code, available >= PA3 (default .)")
4846
.action { case (d, config) => config.copy(dstDir = d) }
4947
.validate { d =>
50-
if (!d.isDirectory) {
51-
Left("not a directory")
52-
} else if (!d.exists) {
53-
Left("not exist")
48+
if (!d.exists) {
49+
Left("not exist: " + d)
50+
} else if (!d.isDirectory) {
51+
Left("not a directory: " + d)
5452
} else {
5553
Right()
5654
}
@@ -74,6 +72,13 @@ object OptParser extends OptionParser[Config]("decaf") {
7472
.valueName("file")
7573
.text("also dump log to a file")
7674
.action { case (f, config) => config.copy(logFile = f) }
75+
.validate { f =>
76+
if (f.getParentFile != null && !f.getParentFile.exists) {
77+
Left("parent directory not exist: " + f.getParentFile)
78+
} else {
79+
Right()
80+
}
81+
}
7782

7883
help('h', "help")
7984
.text("prints this usage text\n")

0 commit comments

Comments
 (0)