[Tutor] File-Fetcher (cmdline-parser)

Peter Otten __peter__ at web.de
Fri Mar 15 18:19:22 CET 2013


Christopher Emery wrote:

> Hello All,
> 
> OS = Raspbain Wheezy & Ubuntu 12.10 (both updated daily)
> 
> Python Version = 3.2 & 3.3
> Python Understanding = Beginner (very basic - just started)
> 
> See paste bin for code, has 44 lines, code does not give any errors.
> http://pastebin.com/2tLHvUym
> 
> Okay, I am writing to ask a few question and provide what I have done
> already.
> 
> When using argparse.argument is it wise to use dest="" even if you
> don't mind having it set automatic to the arg option such as -u will
> become u="None" if it no arg is passed?

dest determines the attribute name under which the option is stored. I 
rarely set it explicitly; instead I provide a --long-option:

     cmdline_parser.add_argument(
        "-u", "--url-source",
        help="the url of where the file can be downloaded.")

> Because there are options that can be added to each .add_argument such
> as help=, action= is it a good idea to set each one of them even if
> the default behavior is what you want?  I thinking future proofing
> wise.

To my eyes this is just noise.
 
> In my formatting of my function which will be one of many, am I laying
> the code out in a way that will become a good habit or should I do it
> different? If so how?
> 
> Based on my comments within the function am I understanding what is going
> on?
> 
> At the end I use a print() to see if all is being passed or not being
> passed through the cmdline, my question for you is if I want to access
> the varibles that are passed through the return args, do I just take
> the function like this:
> outside_of_functions_var = cmdline_parser()
> Does this make outside_of_functions_var a dict or list or other?
 
Neither a dict nor a list, it is an argparse.Namespace object. You can 
access commandline options as its attributes:

args = cmdline_parser()
print(args.url_source)




More information about the Tutor mailing list