Python: using command-line option to access a tuple

  command-line-arguments, python, tuples

I want to pass a short product code when running my script:
./myscript.py --productcode r|u|c
Then use the short product code to look up data stored in a tuple in the python code:

# create tuples for each product
r=("Redhat","7.2")
u=("Ubuntu","7.5")
c=("Centos","8.1") 

# parse the command line
parser = argparse.ArgumentParser()
parser.add_argument("--productcode", help="Short code for product")
options=parser.parse_args()
# get the product code
product_code=options.productcode

# Access elements in the relevant tuple
product_name=product_code[0]
product_version=product_code[1]

Source: Python Questions

LEAVE A COMMENT