Skip to content

Commit 8a7ba5c

Browse files
committed
fix CLI option validation
1. require input file and output dir exist 2. an output file may NOT exist, but its parent dir (if has) must exist
1 parent 1201aca commit 8a7ba5c

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.exists) {
77+
Left("file not exist: " + f)
78+
} else {
79+
Right()
80+
}
81+
}
7782

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

0 commit comments

Comments
 (0)