Parsing user input list
I’m working on a software package that makes tuning hyperparameters for machine learning algorithms a little easier and more friendly. As part of this, I need a way to parse a list provided by a user from the command line. Argprarse is a standard for parsing command line arguments in Python, and it has a “list” type of argument. The problem is that argparse (with this option) tries to coerce the exact user input to a list. Instead, argparse needs to assemble each piece of the argument into a list, which you can do with the following code.
parser = argparse.ArgumentParser() parser.add_argument("--list", nargs="+", default=["a", "b"]) value = parser.parse_args() print(value.list) # see result