trevorld / r-argparse

command-line optional and positional argument parser
GNU General Public License v2.0
103 stars 11 forks source link

How to use nargs="+" with default list/vector? #38

Closed hermidalc closed 2 years ago

hermidalc commented 2 years ago

Sorry noob question here, in python's arparse you can do:

parser.add_argument("--foo", type=str, nargs="+", default=["A", "B", "C"])

but in r-argparse I get an error if I do:

parser$add_argument("--foo", type="character", nargs="+", default=c("A", "B", "C"))
trevorld commented 2 years ago

Please provide a minimal, reproducible example. The extended version of your example seems to work like the Python equivalent:

parser = argparse::ArgumentParser()
parser$add_argument("--foo", type="character", nargs="+", default=c("A", "B", "C"))
args = parser$parse_args()
print(args)
~/tmp$ Rscript test.R
$foo
[1] "A" "B" "C"
~/tmp$ Rscript test.R --foo bar boo
$foo
[1] "bar" "boo"
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--foo", type=str, nargs="+", default=["A", "B", "C"])
args = parser.parse_args()
print(args)
~/tmp$ python3 test.py 
Namespace(foo=['A', 'B', 'C'])
~/tmp$ python3 test.py --foo bar boo
Namespace(foo=['bar', 'boo'])
hermidalc commented 2 years ago

Very sorry @trevorld, I was getting an error that was something like "+" ... comparison only possible for atomic and list types ..., but I realize now my syntax checker didn't highlight that I was missing a comma after nargs="+" and before the next param, thank you for the response