zenlotus / argparse

Automatically exported from code.google.com/p/argparse
Other
0 stars 0 forks source link

Request: Method for prompting for arguments let out #69

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
For programs requiring a large number of arguments I make my them optional 
and then do something like the code below. This (or a more integrated 
implementation) may be useful for argparse.

args = parser.parse_args()
prompt_for_arg(args.name, "Name: ")
prompt_for_arg(args.company, "Company: ")
prompt_for_arg(args.homedir, "Users Home Directory: ")

def prompt_for_arg(argfield, prompt):
    if argfield is not None:
        return False
    try:
        argfield = input(prompt)
        if len(argfield) > 0:
            return True
    except:
        pass
    print("Aborted")
    sys.exit()

Original issue reported on code.google.com by AndrewA...@gmail.com on 5 Apr 2010 at 12:26

GoogleCodeExporter commented 9 years ago
I was too rough in stripping out the code above, the function above does not 
work. 
This one should

def prompt_for_arg(arg, field, prompt):
    if getattr(arg, field) is not None:
        return False
    try:
        r = input(prompt)
        if len(r) > 0:
            setattr(arg, field, r)
            return True
    except:
        pass
    print("Aborted")
    sys.exit()

Original comment by AndrewA...@gmail.com on 5 Apr 2010 at 1:00

GoogleCodeExporter commented 9 years ago
Closing as WontFix, mainly because I didn't see any additional community 
requests for it. Feel free to open a new issue at bugs.python.org (argparse's 
new home) if you know of others asking for similar behavior.

Original comment by steven.b...@gmail.com on 23 Jul 2010 at 1:38