0

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...
CodenameLambda
  • 1,486
  • 11
  • 23
M Leonard
  • 581
  • 1
  • 7
  • 23

4 Answers4

2

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)
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
1

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.

Community
  • 1
  • 1
martianwars
  • 6,380
  • 5
  • 35
  • 44
  • This is also a good solution. But I have a question... In this case, using `if __name__ == '__main__': main() ` would this work if I import this script, and send it the variables that would have otherwise come in through cli args? If so, do I have to indent everything below `if __name__ == '__main__': main()` or will it basically just pick up the script from there... – M Leonard Dec 22 '16 at 16:31
  • It's nearly the same as the answer you accepted, except for the extra detailing. – martianwars Dec 22 '16 at 17:18
0

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
Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
0

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

Rishav
  • 3,818
  • 1
  • 31
  • 49