argparse - option with optional value

John O'Hagan research at johnohagan.com
Thu May 17 23:06:07 EDT 2012


On Thu, 17 May 2012 14:26:50 -0700 (PDT)
Miki Tebeka <miki.tebeka at gmail.com> wrote:

> Greetings,
> 
> I'd like to have an --edit option in my program. That if not specified will
> not open editor. If specified without value will open default editor
> ($EDITOR) and if specified with value, assume this value is the editor
> program to run.

This looks like a job for 'const':

parser.add_argument('-e', '--edit', help='open editor on log', nargs='?',
                         const="$EDITOR")

I think that does what you want.

HTH,

John




More information about the Python-list mailing list