Having options in argparse with a dash

Cemre picture Cemre · Oct 11, 2012 · Viewed 55.4k times · Source

I want to have some options in argparse module such as --pm-export however when I try to use it like args.pm-export I get the error that there is not attribute pm. How can I get around this issue? Is it possible to have - in command line options?

Answer

Thomas Orozco picture Thomas Orozco · Oct 11, 2012

As indicated in the argparse docs:

For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name

So you should be using args.pm_export.