Support default args with action="append"
miker985 opened this issue · 2 comments
miker985 commented
I cannot figure out how to pass default args as a list - they always come through as a tuple.
What I can do in Python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--test-dim", dest="dims", action="append", default=['year', 'sex', 'age'])
args = parser.parse_args(["--test-dim", "race"])
assert args.dims == ['year', 'sex', 'age', 'race']
What I'm trying to do in R
test_that("we can action = 'append' with a default list", {
parser <- argparse::ArgumentParser()
parser$add_argument("--test-dim", dest = "dims", action = "append", default = c("year", "sex", "age"))
args <- parser$parse_args(c("--test-dim", "race"))
expect_equal(args$dims, c("year", "sex", "age", "race"))
})
this results in the following error:
test-argparse.R:34: error: we can action = 'append' with a default list
python error
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
File "/usr/lib/python3.7/argparse.py", line 1758, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python3.7/argparse.py", line 1790, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/usr/lib/python3.7/argparse.py", line 1996, in _parse_known_args
start_index = consume_optional(start_index)
File "/usr/lib/python3.7/argparse.py", line 1936, in consume_optional
take_action(action, args, option_string)
File "/usr/lib/python3.7/argparse.py", line 1864, in take_action
action(self, namespace, argument_values, option_string)
File "/usr/lib/python3.7/argparse.py", line 977, in __call__
items.append(values)
Attribute'tuple' object has no attribute 'append'
Backtrace:
1. parser$parse_args(c("--test-dim", "race")) tests/testthat/test-argparse.R:34:2
2. argparse:::.stop(output, "Error: python error")
Changing the default to default = list("year", "sex", "age")
results in a different error
test-argparse.R:34: error: we can action = 'append' with a default list
python error
Traceback (most recent call last):
File "<stdin>", line 9, in <module>
Namename 'year' is not defined
Backtrace:
1. parser$parse_args(c("--test-dim", "race")) tests/testthat/test-argparse.R:34:2
2. argparse:::.stop(output, "Error: python error")
trevorld commented
For now it seems you must manually add the default to dims
after parsing the args
test_that("we can action = 'append' with a default list", {
parser <- argparse::ArgumentParser()
parser$add_argument("--test-dim", dest = "dims", action = "append")
args <- parser$parse_args(c("--test-dim", "race"))
args$dims <- c("year", "sex", "age", args$dims)
expect_equal(args$dims, c("year", "sex", "age", "race"))
})
trevorld commented
- Converting all R vectors into Python lists instead of tuples causes errors in certain situations (in particular the
metavar
argument). - So we'd need to selectively convert to tuple or list based on context.