使用 docopt 解析函数中的参数, 在命令行中运行函数
remotes::install_github('wydty/docf')
定义main 函数
#' 描述。
#'
main <- function(opt, help=F, ...){ "
\```docopt
#$ <f> [options]
<f> 输入的 tsv 文件名称
#$ abc subcmd [options]
--pats=<> [default: /path].
options:
--th=<threads> Num of threads [default: 10].
\```
other text
" -> helptxt; if(help) return(helptxt)
if(F){ ## test ----
setwd("~/")
opt <- list(f='')
} ## start ----
pacman::p_load(cli, tidyverse)
s('开始分析') #-
cli::cli_alert_success('to: {}')
s('结束') #-
}
在函数文件的最后一行加上
类似 python 中的 if __name__ == 'main' 语句。
if (!interactive() && sys.nframe() == 0) docf::runfun(main) else main