Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > argparse - option with optional value

Reply
Thread Tools

argparse - option with optional value

 
 
Miki Tebeka
Guest
Posts: n/a
 
      05-17-2012
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.

The way I'm doing it currently is:
...
no_edit = 'no-edit'
parser.add_argument('-e', '--edit', help='open editor on log', nargs='?',
default=no_edit)
...
if args.edit != no_edit:
editor = args.edit or environ.get('EDITOR', 'vim')


However I get a feeling there's a better way to do that.
Any ideas?

Thanks,
--
Miki
 
Reply With Quote
 
 
 
 
Miki Tebeka
Guest
Posts: n/a
 
      05-18-2012
> There is a built-in “no value specified” value in Python: the None
> singleton. The ‘argparse’ library uses this for the argument default
> already, so you don't need to fuss with your own special handling
> <URL:http://docs.python.org/library/argparse.html#default>.

The problem with this approach is that in both cases of not specifying -e and with -e with no argument the value of args.edit is None.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
argparse: combine current option value with positional argument Peter Otten Python 1 02-01-2011 04:23 PM
Value into Optional Attribute? ReGenesis0@aol.com XML 2 06-15-2006 02:58 PM
Q: Adding Optional option to expression match Martin Daser Perl Misc 2 05-18-2006 08:35 AM
macro with optional function calls and return value Capstar C Programming 1 08-18-2004 09:30 AM
Optional and have default value ruca ASP .Net 3 04-12-2004 01:30 PM



Advertisments