Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Store argparse input to use as variable

I am using argparse to require input from the user for a hardware id to then be called later on, I cannot work out how to get it so the user types

<command> --id <id>

Please help me see where I’m going wrong! Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

parser = argparse.ArgumentParser(description='Return a list of useful information after specifying a hardare/asset ID')
parser.add_argument('--id', type=str, required=True, help ='A hardware/asset id to provide information on')
args = vars(parser.parse_args())
args = parser.parse_args()

def main():
    hardware_id = hardware_id_input
    host = get_host_information(hardware_id)
    print(host["hostname"])
    print(host["hardware_id"])

>Solution :

Ditch the call to vars. You would have your argument stored as args.id after parsing. You would then call your main with the args.id as input.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading