E
Eric O. Angell
Say I have
class Header:
def __init__(self):
self.foo = 0
# ... more fields
and I have some commandline options that can specify better values for things
in the header, such as foo. If I try the obvious (to me):
parser = optparse.OptionParser()
parser.add_option('--secret', type='string', dest='header', default=Header())
parser.add_option('--foo', type='int', dest='header.foo')
(options, args) = parser.parse_args()
when I run the program with "--foo 3", I'm sadly left with something like
this:<Values at 0xdeadbeef: {'header': <Header instance at 0xdeadbeef>,
'header.foo': 3}>
So when optparse first initializes the variables to None, it's creating a
'header.foo' that has nothing to do with header=Header(). Is there a way to
coerce optparse into populating an object for me? Yes, I could just do
something like this:
(options, args) = parser.parse_args()
header = Header()
header.foo = options.foo
# ... more population of header
but I'd rather just end up with options.header that I could then use somewhere
else.
Am I going to have any success with this, or am I just SOL?
Thanks.
-E
class Header:
def __init__(self):
self.foo = 0
# ... more fields
and I have some commandline options that can specify better values for things
in the header, such as foo. If I try the obvious (to me):
parser = optparse.OptionParser()
parser.add_option('--secret', type='string', dest='header', default=Header())
parser.add_option('--foo', type='int', dest='header.foo')
(options, args) = parser.parse_args()
when I run the program with "--foo 3", I'm sadly left with something like
this:<Values at 0xdeadbeef: {'header': <Header instance at 0xdeadbeef>,
'header.foo': 3}>
So when optparse first initializes the variables to None, it's creating a
'header.foo' that has nothing to do with header=Header(). Is there a way to
coerce optparse into populating an object for me? Yes, I could just do
something like this:
(options, args) = parser.parse_args()
header = Header()
header.foo = options.foo
# ... more population of header
but I'd rather just end up with options.header that I could then use somewhere
else.
Am I going to have any success with this, or am I just SOL?
Thanks.
-E