How to use optparse without the command line?

Karim karim.liateni at free.fr
Fri Apr 8 17:58:26 EDT 2011


On 04/07/2011 10:37 AM, markolopa wrote:
> Hello,
>
> Is there support/idioms/suggestions for using optparse without a
> command line?
>
> I have a code which used to be called through subprocess. The whole
> flow of the code is based on what 'options' object from optparse
> contains.
>
> Now I want to call this code without subprocessing. What I did first
> was to build a "fake" command-line and use
>
> options, args = parser.parse_args(fake_cmdline)
>
> But I find it dumb to encode and decode a dictionary... So I would
> like to know how I if there is a good way of passing a dictionary to
> optparse and benefiting from its option management (check, error
> detection, etc).
>
> Thanks a lot!
> Marko
Move to the best module on args parsing: argparse it is way, way, way 
better no equivalent in any others language.
No tuple when parsing but a simple Namespace objects and very easy to 
port. Go have a look in std libs (>=v2.7).

Regards
Karim



More information about the Python-list mailing list