Python Arguments With Argparse

The aim of this playbook🏁 is to try to standardize a simple use of argparse module in Python for passing named arguments from CLI (PowerShell in my case). This is used mostly within PowerShell scripts to improve readability.

1. steps/notes

  1. import argparse module
  • e.g. python foobar.py --param1="foo" --param2=="bar"
#file: foobar.py
import argparse

def function_proper(param1, param2) -> None:
#CODE...

def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument("--param1")
parser.add_argument("--param2")
return parser


def main() -> None:
parser = init_argparse()
args = parser.parse_args()
function_proper(param1=args.param1, param2=args.param2)


if __name__ == "__main__":
main()

Technical Support Engineer of Behavioural Data Platform (Snowplow Analytics). On comparison of programming languages, history of computing, and raw playbooks.