4

I'm working on an SConstruct build file for a project and I'm trying to update from Options to Variables, since Options is being deprecated. I don't understand how to use Variables though. I have 0 python experience which is probably contributing to this.

For example, I have this:

opts = Variables()
opts.Add('fcgi',0)
print opts['fcgi']

But I get an error:

AttributeError: Variables instance has no attribute '__getitem__':

Not sure how this is supposed to work

Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
Geuis
  • 41,122
  • 56
  • 157
  • 219
  • Note for someone who ends up here totally confused like I was: You *must* `Add()` variables in order for them to be actually read from the file. You can't just add things to the file and expect them to show up in your `opts`. – Jonathon Reinhart Apr 16 '15 at 22:04

2 Answers2

5

Typically you would store the variables in your environment for later testing.

opts = Variables()
opts.Add('fcgi',0)
env = Environment(variables=opts, ...)

Then later you can test:

if env['fcgi'] == 0:
    # do something
Brian Neal
  • 31,821
  • 7
  • 55
  • 59
1

That specific error tells you that class Variables hasn't implemented python's __getitem__ interface which would allow you to use [ ...] on opts. If all you want to do is print out your keys, the Variables documentation seems to indicate that you can iterate over your keys:

for key in opts.keys():
    print key

Or you can print out the help text:

print opts.GenerateHelpText()
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164