# val_type_dc.py import dataclasses import sys from typing import List, Any USAGE = f"Usage: python {sys.argv[0]} [--help] | firstname lastname age]" @dataclasses.dataclass class Arguments: firstname: str lastname: str age: int = 0 def check_type(obj): for field in dataclasses.fields(obj): value = getattr(obj, field.name) print( f"Value: {value}, " f"Expected type {field.type} for {field.name}, " f"got {type(value)}" ) if type(value) != field.type: print("Type Error") else: print("Type Ok") def validate(args: List[str]): # If passed to the command line, need to convert # the optional 3rd argument from string to int if len(args) > 2 and args[2].isdigit(): args[2] = int(args[2]) try: arguments = Arguments(*args) except TypeError: raise SystemExit(USAGE) check_type(arguments) def main() -> None: args = sys.argv[1:] if not args: raise SystemExit(USAGE) if args[0] == "--help": print(USAGE) else: validate(args) if __name__ == "__main__": main() # ejecutar # python3 val_type_dc.py Guido "Van Rossum" 25 # python3 val_type_dc.py Guido "Van Rossum" # python3 val_type_dc.py Guido Van Rossum