lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
399 stars 49 forks source link

Allow parsing comma-separated list arguments #142

Closed andrey-klochkov-liftoff closed 2 years ago

andrey-klochkov-liftoff commented 2 years ago

Is it possible to parse comma-separated lists like e.g. --listarg 1,2,3?

I attempted to do the following, but I get a list with a nested list as a result.

from typing import List
from dataclasses import dataclass
from simple_parsing.helpers import field

@dataclass
class Options:
    listarg: List[int] = field(default_factory=lambda: [], type=lambda v: v.split(","))
andrey-klochkov-liftoff commented 2 years ago

This example works if I set nargs='?' in addition to type. I'm closing the issue.

lebrice commented 2 years ago

Yeah, this is one of the weird things with argparse: When you pass a type to argparse (which is essentially what is happening here, the field just passes the type to the parser.add_argument method), it gets called with every value that is passed, when nargs=="*" or > 1.