Prioritizing command line over config file when specifying config file via command line (.. good grief)

271 views
Skip to first unread message

Shane Spencer

unread,
Feb 14, 2012, 2:26:22 PM2/14/12
to python-...@googlegroups.com
Heya all.

I'm in a situation where I'm dealing with parsing the command line
twice to resolve a problem where when I specify a config file to parse
through a command line option and then parse the config file it will
of course override the command line. To solve this I'm parsing the
command line again after parsing the config file.

Any thoughts on a proper method of handling this scenario that doesn't
seem redundant? I think I may be overlooking the standard flow of
this operation in favor of making things simple for the end user but
possibly mucking with a configuration management style that I'm not
100% indepth with at the moment.

- Shane

Андрей Григорьев

unread,
Feb 14, 2012, 4:05:08 PM2/14/12
to python-...@googlegroups.com
Hi.

In the same situation I wrote an ugly solution with my own settings.py
module:

https://gist.github.com/1830236

This is only the first part of it. Second part consists of blocks like that:

if '--parameter' in sys.argv:
PARAMETER = True
elif '--no-parameter' in sys.argv:
PARAMETER = False

It sucks, yeah, but works for me :-). The most unattractive features are
that it causes to:
- write --help content by hand;
- write 4 lines of code per parameter;
- doublecheck every of that lines to avoid mistakes.

Just decided to share my butthurt. Don't use it! :-)

14.02.2012 23:26, Shane Spencer пишет:

--
Andrew

spen...@gmail.com

unread,
Feb 14, 2012, 8:41:12 PM2/14/12
to Tornado Web Server
I think I suddenly don't mind the way I'm doing it. :)

Emilio Daniel González

unread,
Feb 15, 2012, 9:08:13 AM2/15/12
to python-...@googlegroups.com
Did you try the options module from tornado?


It can handle both config file and command line parameters pretty smoothly =)

spen...@gmail.com

unread,
Feb 15, 2012, 1:47:28 PM2/15/12
to Tornado Web Server
Yup.. That's what I'm using. The problem as stated is that I'm
specifying a configuration file via the command line (using
tornado.options to parse it). Then I load the config. In order to
give other command line options priority over the config file (port,
hostname, favorite color, etc...) I have to parse the command line
options again. Seems to work fine but looking for a more elegant
solution if possible.

On Feb 15, 5:08 am, Emilio Daniel González <emda...@gmail.com> wrote:
> Did you try the options module from tornado?
>
> http://www.tornadoweb.org/documentation/options.html
>
> It can handle both config file and command line parameters pretty smoothly
> =)
>
> Cheers!
>
> 2012/2/14 sh...@bogomip.com <spence...@gmail.com>

Ben Darnell

unread,
Feb 15, 2012, 2:24:43 PM2/15/12
to python-...@googlegroups.com
There's not currently a good solution for this; the options module
assumes that you'll use either command line or config files but not
mix the two (I think friendfeed only ever used the command-line side
of the interface). Note that calling parse_command_line() twice may
have undesirable side effects on the logging configuration.

I think what I'd like to do is add a standard --flagfile option that
would load options from the file as if they appeared in the command
line in place of the --flagfile. However, the config files
interpreted by parse_config_file are executable python and I'm a
little uneasy about enabling something like that by default. A more
limited flag-only format would be nice, although maybe not worth the
complexity.

-Ben

spen...@gmail.com

unread,
Feb 15, 2012, 6:06:42 PM2/15/12
to Tornado Web Server
On Feb 15, 10:24 am, Ben Darnell <b...@bendarnell.com> wrote:
> There's not currently a good solution for this; the options module
> assumes that you'll use either command line or config files but not
> mix the two (I think friendfeed only ever used the command-line side
> of the interface).  Note that calling parse_command_line() twice may
> have undesirable side effects on the logging configuration.
>

So far so good with logging.. it appears to function just fine with
2.2+

Joe Bowman

unread,
Feb 16, 2012, 8:59:46 AM2/16/12
to python-...@googlegroups.com
I've actually been considering moving to using config file and options. Basic plan being check for a config file to set variables. After check options. Options would over write any variables set by the config import.

Emilio Daniel González

unread,
Feb 16, 2012, 11:09:06 AM2/16/12
to python-...@googlegroups.com
There's not currently a good solution for this; the options module
assumes that you'll use either command line or config files but not
mix the two (I think friendfeed only ever used the command-line side
of the interface)...
-Ben

Ben, I actually using both config file and command line (for instance specific parameters).

My code look something like this:

        from tornado.options import options

        tornado.options.parse_config_file(config_file)
        tornado.options.parse_command_line()

        settings = {}
        for option in options:
            settings[option] = options[option].value()
        tornado.web.Application.__init__(self, routes, **settings)
 
This allows me for example, to set a default port into the config file but then change it for an specific instance by adding --port in the command.

Hope this can help someone!

Cheers!

Didip Kerabat

unread,
Feb 16, 2012, 6:52:44 PM2/16/12
to python-...@googlegroups.com
For me, I always use ConfigParser or YAML files and use tornado's options only for --port, so that Supervisord can use it.

- Didip -

Reply all
Reply to author
Forward
0 new messages