argparse python problem with initializing functions

68 Views Asked by At

why is my program jumping straight to the else block in the while loop (getting "You've chosen unavailable option! Exiting..." statement) instead of initializing the functions when user is prompted to choose an option? No matter if I type 'Q', 'q', 'Z', 'z', 'X', 'x' or any other letter. This is my first time working with argparse module so your understanding is appreciated.

import sys, argparse

from functions import get_expired_OINs, get_OINs, download_certs


def initialize(choice):

    print("What would you like to do today? \n"
        "Press X to check all expired certificates \n"
        "Press Z to only download certificates for selected OIN numbers \n"
        "or press Q to exit. \n"
        "")

    while True:
        choice = input("My choice: ")
    
        if choice.upper() == args.Q:
            sys.exit()
        elif choice.upper() == args.X:
            get_expired_OINs() 
            download_certs()
            break
        elif choice.upper() == args.Z:
            get_OINs()
            download_certs()
            break
        else:
            print("You've chosen unavailable option! Exiting...")
            sys.exit()

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
                                    
    parser.add_argument(
        '-q',
        type=str, 
        dest='Q',
        help='To quit the program'
        )    
    parser.add_argument(
        '-x',
        type=str, 
        dest='X',
        help='To check for expired AS certificates'
        )     
    parser.add_argument(
        '-z',
        type=str, 
        dest='Z',
        help='To check AS certificates as needed'
        ) 
    args = parser.parse_args() 
    # print(args)  < -- here I get object: Namespace(Q=None, X=None, Z=None)
    initialize(args)

When the while loop is changed to the below:

while True:
        choice = input("My choice: ")
        
        if choice.upper() == 'Q':
            sys.exit()
        elif choice.upper() == 'X':
            get_expired_OINs() 
            download_certs()
            break
        elif choice.upper() == 'Z':
            get_OINs()
            download_certs()
            break
        else:
            print("You've chosen unavailable option! Exiting...")
            sys.exit()

the program works as expected, but I would like to tie it with the argparse module.

1

There are 1 best solutions below

3
chepner On

I assume you want to use argparse to replace the call to input. Your three options should all set the same destination, just with a different constant. When you run the program, you must supply one of the three options. (You probably want to supply exactly one of three; you should look at creating a mutually exclusive group. See the argparse documentation for details.)

(Note: there is no reason to have a -q option, as opposed to simply not running the program in the first place.)

import sys, argparse

from functions import get_expired_OINs, get_OINs, download_certs


def initialize(choice):
    
    print("What would you like to do today? \n"
          "Press X to check all expired certificates \n"
          "Press Z to only download certificates for selected OIN numbers \n"
          "or press Q to exit. \n"
          "")
    
    choice = args.choice
        
    if choice == 'Q':
        sys.exit()
    elif choice = 'X':
        get_expired_OINs() 
        download_certs()
    elif choice == 'Z':
        get_OINs()
        download_certs()
    else:
        print("You've chosen unavailable option! Exiting...")
        sys.exit()

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
                                    
    parser.add_argument(
        '-q',
        action='store_const',
        const='Q',
        dest='choice',
        help='To quit the program'
        )    
    parser.add_argument(
        '-x',
        action='store_const',
        const='X',
        dest='choice',
        help='To check for expired AS certificates'
        )     
    parser.add_argument(
        '-z',
        action='store_const',
        const='Z',
        dest='choice',
        help='To check AS certificates as needed'
        ) 
    args = parser.parse_args() 
    initialize(args)