argparse python pass numeric argument code example
Example 1: argeparse can it take a type list
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--default')
parser.add_argument('--list-type', type=list)
parser.add_argument('--list-type-nargs', type=list, nargs='+')
parser.add_argument('--nargs', nargs='+')
parser.add_argument('--nargs-int-type', nargs='+', type=int)
parser.add_argument('--append-action', action='append')
for _, value in parser.parse_args()._get_kwargs():
if value is not None:
print(value)
Example 2: argparse python
def create_parser(arguments):
"""Returns an instance of argparse.ArgumentParser"""
parser = argparse.ArgumentParser(
description="Description of your code")
parser.add_argument("argument", help="mandatory or positional argument")
parser.add_argument("-o", "--optional",
help="Will take an optional argument after the flag")
namespace = parser.parse_args(arguments)
return namespace