Monday, August 8, 2022

How to take string from argparse input and use it as object

 (.env) boris@boris-All-Series:~/ARGPARSE$ cat argParse3.py

import argparse

class HondaCar:

    def __init__(self, color, year, type):

        self.color = color

        self.year = year

        self.type = type

parser = argparse.ArgumentParser()

parser.add_argument("--hondaType") # Accept the argument as vehicle type.

args = parser.parse_args()

args.hondaType = 'Honda Civic (x)6'

# Create instance of class 

Model = HondaCar("plump", 2017, args.hondaType)  

print("Model " + Model.type + " is good a purchase") 

print("It was assembled ",2022 - Model.year, " years ago.")


(.env) boris@boris-All-Series:~/ARGPARSE$ python3 argParse3.py

Model Honda Civic (x)6 is a good purchase

It was assembled  5  years ago.
















No comments:

Post a Comment