May 18, 2022

Choosing Singular or Plural for Argument Names in Argparse

Naming is hard, right? Today I was extending an argparse based CLI application. I wanted to add an optional argument so that I can pass in environment variables. For this argparse has a special “action”, called append. From the argparse documentation: >>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', action='append') >>> parser.parse_args('--foo 1 --foo 2'.split()) Namespace(foo=['1', '2']) Awesome! Now I can access all the provided values as foo. Wait a moment Wait! This does not match....