-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cmdparseR.R
57 lines (48 loc) · 1.66 KB
/
test_cmdparseR.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
library(cmdparseR)
main <- function() {
init_command_line_parser('test_cmdparseR','Test cmdparseR package','0.1.0')
cmds <- list(
list('add', 'Add something'),
list('delete', 'Delete something')
)
reg_command_list(cmds)
subcmds <- list(
list('name','add','Add a name'),
list('file','add','Add a file'),
list('name','delete','Delete a name'),
list('file','delete','Delete a file')
)
reg_subcmd_list(subcmds)
args <- list(
list('--config','-c','config','~/myconfigfile.txt',argsType$TypeValue,'Configuration file'),
list('--debug','-d','debug',FALSE,argsType$TypeBool,'Display debug messages'),
list('--keywords','-k','keywords',NA,argsType$TypeMultiVal,'Search keywords'),
list('--daterange','-r','daterange',NA,argsType$TypeRange,'Date range'),
list('--username','-u','username',NA,argsType$TypeValue,'User name'),
list('--verbose','-v','verbose',0,argsType$TypeCount,'Verbosity level')
)
reg_argument_list(args)
pos <- list(
list('outfile','Output filename'),
list('infiles','Input filename(s)')
)
reg_positionals_list(pos)
args <- commandArgs(trailingOnly = TRUE)
mydata <- parse_command_line(args, default_help = FALSE)
if (mydata$help) {
writeLines(paste0("Command: ", mydata$command))
writeLines(paste0("Subcmd: ", mydata$subcmd))
usage()
stop(call. = FALSE)
}
if (is.na(mydata$outfile)) stop("Error: no outfile provided.")
# read input from stdin if no input files provided on the cmd line.
if (is.na(mydata$infiles[1])) {
stdin <- file("stdin")
lines <- readLines(stdin)
close(stdin)
mydata$infiles <- lines
}
print(mydata)
}
if (!interactive()) main()