getopt(),是getopt包的函數,需要先按照getopt包
getopt(spec = NULL, opt = commandArgs(TRUE),command = get_Rscript_filename(), usage = FALSE,debug = FALSE)
spec:一個4或5列的矩陣,裏面包括了參數信息,前四列是必須的,第五列可選。
第一列:參數的longname,多個字符。
第二列:參數的shortname,一個字符。
第三列:參數是必須的,還是可選的,數字:0代表不接參數 ;1代表必須有參數;2代表參數可選。
第四列:參數的類型。logical;integer;double;complex;character;numeric
第五列:註釋信息,可選。
usage:默認爲FALSE,這時參數無實際意義,而是以用法的形式輸出。
#!/usr/bin/Rscript
# _*_ coding: utf-8 _*_
library('getopt')
command=matrix(c(
'help', 'h', 0,'loical', '幫助文檔',
'pdfinput', 'i', 2, 'character', '判斷值的結果',
'noloutput', 'o', 1, 'character', '標準化的判斷值的結果'),byrow=T,ncol=5)
args=getopt(command)
if (!is.null(args$help) || is.null(args$pdfinput) || is.null(args$noloutput) ) {
cat(paste(getopt(command, usage = T), "\n"))
q(status=1)
}
## 設置默認值
#if ( is.null(args$pdfinput) ) {
# args$pdfinput = 0
# }
print(args$pdfinput)
$ Rscript t.R -h
Usage: t.R [-[-help|h]] [-[-pdfinput|i] <character>] [-[-noloutput|o] <character>]
-h|--help 幫助文檔
-i|--pdfinput 判斷值的結果
-o|--noloutput 標準化的判斷值的結果