コマンドライン引数を解析し、パラメーターの抽出が成功したときにパラメーターを受け取る関数を実行しようとしています。main メソッドがある CurrencyExchangeRunner というオブジェクトがあります。クラスの構造を次のように想定しました。
object CurrencyExtractionRunner {
def main(args:Array[String]){
parseArgs(args){
(currencyType,currencyTypeArgs) =>
CurrencyExchanger(curencyType,currencyTypeArgs){
(exchanger) => exchanger.startExchange
}
}
}
}
}
上記で達成したいのは、 を使用して引数を解析し、 as パラメータをparseArgs(args)
取得してそれらをファクトリ オブジェクトに渡すことです。これにより、メソッドを実行する適切なオブジェクトが返されます。これは私が思い描いたものですが、このフローをどのように作成するかについては少しわかりません. 最初に試したのは、次のようにコマンドライン引数を解析する特性を作成することでした (コマンドライン解析には jcommander ライブラリを使用しています)。(currencyType,currencyTypeArgs)
CurrencyExchanger
exchanger
startExchange
object Args {
@Parameter(
names = Array("-h", "--help"), help = true)
var help = false
@Parameter(
names = Array("-c", "--currency-type"),
description = "Type of currency exchange that needs to be performed",
required = true)
var currencyType: String = null
@Parameter(
names = Array("-d", "--denominations"),
description = "Specific denominations to be used during the exchage")
var exchangeDenomination: String = null
@Parameter(
names = Array("-s", "--someotheroptionalarg"),
description = "Additional argument for a specific currency exchange")
var someOtherOptionalArg: String = null
}
trait ParseUtils {
//How do I do this, take the args and return a function.
def parseArgs(args: Array[String]){
val jCommander = new JCommander(Args, args.toArray: _*)
if (Args.help) {
jCommander.usage()
System.exit(0)
}
//What do I do now? How do I proceed with executing the function with
//the specific arguments?
//What do I need to do to wrap the commandline arguments so that it could
//be passed to the next function
}
}
コマンドライン引数の任意のシーケンスを取得し、これらの引数を取得して正しいエクスチェンジャーを返すファクトリである次のステップを実行するのに十分なほどコードを柔軟にする方法がわからないため、ここでかなり行き詰まっています。
誰かが私を正しい方向に向けることができれば、それは素晴らしいことです.