R getopt()

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    标准化的判断值的结果

参考:https://www.cnblogs.com/timeisbiggestboss/p/7811009.html

猜你喜欢

转载自blog.csdn.net/rojyang/article/details/83786575