Switch Parameters In Python

1. steps/notes

  • you want to run python run.py <job> -t -s
  • -t and -s should get no arguments
  • <job> should not get no flag
  • this is like a switch parameter function foo ([switch]$var) in PowerShell

2. store-true and store false parameter

  • where adding an argument within def init_argparse() -> argparse.ArgumentParser:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-w', action='store_true')
  • action='store_true' implies default=False implies argument is False when not passed
  • action='store_false' implies default=True implies argument is True when not passed

3. what about the first argument being positional

  • this seems to be working
parser.add_argument("job")

4. CODE

def xo(pipeline, cluster_type):
#CODE

def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument("job")
parser.add_argument("-t", action="store_true")
parser.add_argument("-p", action="store_true")
return parser

def main() -> None:
parser = init_argparse()
args = parser.parse_args()
pipeline = args.job

if args.t:
cluster_type = "transient"
if args.p:
cluster_type = "persistent"

xo(pipeline, cluster_type)


if __name__ == "__main__":
main()

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

SQL Compression

The Two Greatest Software Engineering Design Principles For Living Life

Coding for Government Electricity

Postmortem Report

Using “Let’s Encrypt” Certificate Authority

API3 Operations Report, August 2021

Ghost Browser Review — Improved Productivity for Developers + Others

Why C++ Fans Should Look at Julia

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

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

More from Medium

Positional Only Arguments In Python

6 Ways You Can Get Keys From a Python Dictionary

10 Useful String Operations in Python

Map and a filter function in python