scopt解析引數例項

2021-10-08 03:39:59 字數 1351 閱讀 5118

scopt是github上有人開發的乙個用來解析引數的輕量級小工具,對於日常解析引數的場景基本夠用,專案中使用也比較多,現在稍微記錄一下用法。

在pom.xml檔案中加入以下依賴

com.github.scopt

scopt_2.10

3.2.0

直接上**先

object scopttest .text("default of input is input")

opt[string]('o', "output").required().action .text("default of output is output")

opt[int]("index").optional().action .text("default of index is 0")

opt[int]("partitionnum").optional().action .text("default of partitionnum is 2")

opt[boolean]("debug").optional().action .text("default of debug is false") }

def init(args: array[string]) =

def main(args: array[string]): unit =

}

首先我們先寫乙個case class,裡面加入各種我們需要的引數。

然後生成乙個parser物件,解析傳入的引數即可。

將上面的**run起來,如果不傳入input與output這兩必須引數,會有如下提示

error: missing option --input

error: missing option --output

this is a test for scopt params

usage: test [options]

-i | --input default of input is input

-o | --output default of output is output

--index default of index is 0

--partitionnum default of partitionnum is 2

--debug default of debug is false

傳入如下引數

-i input1 -o output2
**執行的結果為

input1

output202

false

C XML解析方式例項解析

c xml解析通過xpath的方式是如何辦到的呢?具體的操作步驟是什麼呢?那麼下面我們就向你介紹通過xpath的方式來實現c xml解析,希望對你了解c xml解析有所幫助。c xml解析通過xpath的方式的步驟 1 需要先載入文件,然後再讀取想要的節點值。xml文件 protected xmld...

C XML解析方式例項解析

c xml解析通過xpath的方式是如何辦到的呢?具體的操作步驟是什麼呢?那麼下面我們就向你介紹通過xpath的方式來實現c xml解析,希望對你了解c xml解析有所幫助。c xml解析通過xpath的方式的步驟 1 需要先載入文件,然後再讀取想要的節點值。xml文件 protected xmld...

pull解析例項

如,xml 檔案如下 大話西遊 id 10023 author 小不點 dlcount 13233 ranking 23 icon tags 玄幻,熱血 法寶 西遊記 id 10023 author 小不點 dlcount 13233 ranking 256 icon tags 玄幻,熱血 法寶 紅...