我想在程序中实现命令行,以便它们可以是可选的,但到目前为止,我可以运行程序的唯一方法是在命令行中运行程序时输入3个参数。有什么方法可以让我只输入一个参数的最小值,而另两个参数是可选的吗?
这是我的代码的开头:
import scala.collection.mutable.ListBuffer
import util.control.Breaks._
object part3 {
var newStartTime = 0
var newEndTime = 0
var csvStartTime = 0
var csvEndTime = 0
def main(args: Array[String]): Unit = {
var csV = args(0)
var daY = args(1)
var time1 = args(2)
var time2 = args(3)
newEndTime = checkLengths(time1,time2)
val bufferedSource = io.Source.fromFile(csV)
var z = new ListBuffer[String]()
var z2 = new ListBuffer[String]()
var i = 0
更新的代码:
val fileName = args(0)
courseCode1 = args(1)
val courseCode2: Option[String] = Try(args(2)).toOption
val courseCode3: Option[String] = Try(args(3)).toOption
val extracourseSelection = for {
c2 <- courseCode2
c3 <- courseCode3
}
courseCodeFixed1 = courseCode1.toUpperCase.patch(4," ",0)
if(c2!=null)
{
courseCodeFixed2 = c2.toUpperCase.patch(4,0)
}
if(c3!=null)
{
courseCodeFixed3 = c3.toUpperCase.patch(4,0)
}
print(courseCodeFixed2)
print(courseCodeFixed3)
我尝试实现此解决方案,但现在它根本不想读取任何命令行参数