Action not invoked for default value

11 views
Skip to first unread message

Martin Pengelly-Phillips

unread,
Jun 10, 2011, 6:15:11 AM6/10/11
to argparse-users
Hi there,

Have been playing with a custom Action that automatically sets logging
verbosity based on a command line argument. Something that surprised
me was that if a default value is specified and the user does not
specify the flag then the action is never called. In my case, this
meant that the default logging level was never set.

To solve the issue I have added calls to the Action initialisation to
set the logging level to the default value as specified in the Action,
but this is of course untidy with unnecessary side effects.

Is there a reason an Action is not called with the default value?

cheers,


Martin

Steven Bethard

unread,
Jun 14, 2011, 6:14:12 AM6/14/11
to argpars...@googlegroups.com

I'm not 100% convinced that it should be called, though feel free to
try writing a patch, and see if it breaks any of the argparse test
suite. If it doesn't break anything, feel free to post it to
bugs.python.org.

Alternatively, you might try using a custom type instead of a custom
action. Unlike custom actions, custom types will be called on default
values (as long as the default value is a string). So you would do
something like:

parser.add_argument('--log-level', type=LogLevel, default='info')
...
args = parser.parse_args()
...
logger.setLevel(args.log_level)

Steve
--
Where did you get that preposterous hypothesis?
Did Steve tell you that?
        --- The Hiphopopotamus

Reply all
Reply to author
Forward
0 new messages