简体   繁体   中英

Accepting user input for Argparse arguments

How would I go about defining the float value of each argument with a user prompt? Currently I'm using default = x, but would rather allow a user to define a few variables before collecting them together and passing them to a function.

I thought I could do something like:

first = input("first float") 
second = input("second float")
third = input("third float")

...but I get: TypeError("unsupported operand type(s) for *=: 'float' and 'NoneType'",)

Here's code that works fine, but uses default = x vs. user prompt.

import sys
import argparse

if __name__ == "__main__":
    args = sys.argv
    args.pop(0)
    arg_parser = argparse.ArgumentParser("Help")

    arg_parser.add_argument("--first", type=float, help="first value", default=1)
    arg_parser.add_argument("--second", type=float, help="second value", default=2)
    arg_parser.add_argument("--third", type=float, help="third value", default=3)
    args = arg_parser.parse_args(args)    
    whatever = WhateverWhatever(args.first, args.second, args.third)
    whatever.start()

class WhateverWhatever:
    def __init__(self, first = 1, second = 2, third = 3):
        self.first = first
        self.second = second
        self.third = third

You can run your code in the console by writing

python <PythonFileName.py> --first=<first input> --second=<second input> --third=<third input>

you don't need to write an input method inside your code.

The default parsing uses sys.argv[1:]

args = arg_parser.parse_args() 

You could also provide a similar list

args = arg_parser.parse_args('--first 1 --second 23 --third 42'.split()) 

It's a good idea when testing argparse to

print(args)

to see what the parser produced.

A matching Namespace object can be produced with:

 args = argparse.Namespace(first=1, second=43, third=23)

Or args can be any object with the relevant attributes, args.first , etc.

Well that turned out to be extremely obvious.

import sys
import argparse

if __name__ == "__main__":
    args = sys.argv
    args.pop(0)
    arg_parser = argparse.ArgumentParser("Help")

    arg_parser.add_argument("--first", type=float, help="first value", default=1)
    arg_parser.add_argument("--second", type=float, help="second value", default=2)
    arg_parser.add_argument("--third", type=float, help="third value", default=3)
    args = arg_parser.parse_args(args)  

    args.first = float(input("First value"))
    args.second = float(input("Second value"))
    args.third = float(input("Third value"))

    whatever = WhateverWhatever(args.first, args.second, args.third)
    whatever.start()

class WhateverWhatever:
    def __init__(self, first = 1, second = 2, third = 3):
        self.first = first
        self.second = second
        self.third = third   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM