1

this is a question about argparse in python, it is probably very easy

import argparse

parser=argparse.ArgumentParser()

parser.add_argument('--lib')

args = parser.parse_known_args()

if args.lib == 'lib':
    print 'aa'

this would work, but instead of calling args.lib, i only want to say 'lib' (i dont want to type more), is there a way to export all the args variable out of the module (ie changing scope). so that i can directly check the value of lib not by specifying name of the module at the front

PS: i have a lot of variables, i do not want to reassign every single one

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
cakester
  • 421
  • 4
  • 15

3 Answers3

2

First, I'm going to recommend using the args specifier. It makes it very clear where lib is coming from. That said, if you find you're referring to an argument a lot, you can assign it to a shorter name:

lib = args.lib

There's a way to dump all the attributes into the global namespace at once, but it won't work for a function's local namespace, and using globals without a very good reason is a bad idea. I wouldn't consider saving a few instances of args. to be a good enough reason. That said, here it is:

globals().update(args.__dict__)
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Just curious, is there really any difference in this specific scenario of using `globals()` as opposed to `locals()`? Would it be better/worse to do so? I'm aware of the difference or the two namespaces but would it affect anything in this scenario? – Bob Jun 09 '14 at 19:31
  • 1
    I agree that using `args` specifier should be preferred. Imagine there is an `sys` module imported that used after the args are parsed and one of the args is called `sys`..shadowing. – alecxe Jun 09 '14 at 19:33
  • @Bob: At module level, `globals()` and `locals()` do the same thing. Inside a function, `locals()` returns the local variable dict, but modifying that dict won't actually change the local variables. – user2357112 Jun 09 '14 at 19:33
  • 1
    Moreover, if the argument was named `from`, then you could not use the variable named `from` since it is a Python keyword. (So `globals().update(args.__dict__)` will silently fail to define the desired variable.) – unutbu Jun 09 '14 at 19:36
  • @unutbu: You couldn't use the attribute name `from` either, so you'd probably specify a `dest` you could use. – user2357112 Jun 09 '14 at 19:39
  • That `globals update` works, but is a good way to confuse other users of your code (including your future self). 'Where did that `lib` variable come from?`. – hpaulj Jun 09 '14 at 21:38
1

Sure, just add a line which assigns args.lib to lib.

import argparse

parser=argparse.ArgumentParser()    
parser.add_argument('-lib')    
args = parser.parse_known_args()

lib = args.lib 

if lib == 'lib':
    print 'aa'
wnnmaw
  • 5,444
  • 3
  • 38
  • 63
  • i have a lot of variables, do i ahve to do this for all of them? is there a faster way? – cakester Jun 09 '14 at 19:26
  • Anyway that does it automatically is probably going to be hacky and unclear, as suggested in the other answers. Depending on how many variables it may be worth it, but that's up to you – wnnmaw Jun 09 '14 at 19:49
0

of coarse ... but just be cause you can doesnt mean you should ... but just for kicks globals().update(dict(args._get_kwargs()))

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179