How to use optparse without the command line?

markolopa marko.loparic at gmail.com
Wed Apr 13 09:18:53 EDT 2011


On Apr 8, 11:58 pm, Karim <karim.liat... at free.fr> wrote:
> On 04/07/2011 10:37 AM, markolopa wrote:
>
> > Is there support/idioms/suggestions for usingoptparsewithout a
> >commandline?
>
> > I have a code which used to be called through subprocess. The whole
> > flow of the code is based on what 'options' object fromoptparse
> > 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
> >optparseand benefiting from its option management (check, error
> > detection, etc).
>
> 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).

argparse seems great for my needs, in particular because the usage of
namespace objects. Do you know if there is a way to use it with Python
2.6? My colleagues won't accept to upgrade the Python version anytime
soon.

Thanks a lot!
Marko



More information about the Python-list mailing list