M Leonard
M Leonard

Reputation: 591

skip input() if arguments are passed into script

I can't quite put this all together... At the beginning of my script I have some user input to get basic information:

def get_provider():
    print("Operations are ... ", conversion_type )
    conversion = input('Enter conversion ')

This (and a few other user input sections) are what drive the rest of the script.

I would like to be able to just pass in the variables, and use argv to set the variables, rather that use the user input section. That is more for debugging purposes. I do not want to do-away with the user input section though. Just skip if args are passed in.

I think the solution revolves around argv parse and if __name__ == "__main__": ..., but as I said, I can't quite put it all together.

Here is an example of something I tried, which didn't quite work:

def get_provider():
    print("Operations are ... ", conversion_types )
    conversion_type = input('Enter conversion ')
conversion_type = get_provider()

def main():
    conversion_type = sys.argv[0]

if __name__ == '__main__':
    main()

... the rest of the script...

Upvotes: 0

Views: 2334

Answers (4)

Rishav
Rishav

Reputation: 4088

If someone comes here looking for a one-liner to do this in case of default params then

email = combo if combo else input("Enter e-mail or combo: ")

where combo is the default argument for the function

Upvotes: 0

Corey Goldberg
Corey Goldberg

Reputation: 60644

Here is an example of an optional positional argument using argparse.

This example will use a command line argument if given. Otherwise it will prompt the user for input. Either way, conversion_type gets populated.

#!/usr/bin/env python3

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('conversion_type', nargs='?')
args = parser.parse_args()

if args.conversion_type:
    conversion_type = args.conversion_type
else:
    conversion_type = input('conversion_type: ')
print(conversion_type)

Upvotes: 2

martianwars
martianwars

Reputation: 6500

A quick hacky way to do it (i'm writing this since you want to have quick debugging) is to check the length of argv. If the length is more than 1, then you have len(sys.argv) - 1 arguments as the first element is the file name. Let's say you have one number as your input.

This solution will work if you use python script.py to run your script. Beware if you are using something more complicated.

def get_provider():
    print("Operations are ... ", conversion_types )
    conversion_type = input('Enter conversion ')

def main():
    if len(sys.argv) == 1:
        # only your file name
        conversion_type = get_provider()
    else:
        conversion_type = sys.argv[1]

if __name__ == '__main__':
    main()

... the rest of the script...

For a more sustainable solution, please use the library argparse. This answer might get you started.

Upvotes: 1

Carles Mitjans
Carles Mitjans

Reputation: 4866

Take a look at module sys.

import sys

if len(sys.argv) > 1:  
    # There are arguments to the script
else: 
    # There aren't arguments to the script
    # use input to take arguments

Upvotes: 0

Related Questions