How To Pass Arguments From Powershell Into Python Scripts

1. positional arguments using sys.argv within python code

  • in the first one I use sys.argv to parse a argument to signify a non-default behavior
  • the example is from a script I use for in-place patching of Snowplow Analytics schemas (daily job/different story) — in edge cases, I am passing delete for the actual deletion via API
  • run patch_schema -delete to perform the deletion or patch-schema for the default patch
powershell wrapper
python script utilising the `delete` arg passed via powershell

2. optionals (keyword arguments) using argparse modul within python code

  • in the second, I am using argparse with named parameters to parse the arguments
  • not that also the argparse named parameters (-k, and -v) are part of the $<arg> variable
  • the usecase is my python script with which I update Hashicorp Consul from powershell terminal (daily job/different story)
  • for more on argparse, see Python Arguments With Argparse module: The Template I Use for CLI Scripting
powershell wrapper with named arguments for key and value to be modified
python script parsing arguments with argparse module

Links

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

63 Followers

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com