Hacker News .hnnew | past | comments | ask | show | jobs | submitlogin

And if you want to have both command line args and config files, check out my project: https://github.com/ipartola/groper. It even supports creating a sample config file out of the options you have defined.


This might just be your examples, but it seems a bit verbose.

For example for:

define_opt('server', 'daemon', type=bool, cmd_name='daemon', cmd_short_name='d')

Can I just write define_opt('daemon') ?


It is a bit verbose, you are right. Let me explain why all of these are necessary. (BTW, argparse is almost as verbose [1]).

server - this is the section/module. I could grab this from the name of the current module, but that means you must use unique module names, and not change them. Otherwise, your config files would stop working.

daemon - required, obviously, since this is the name of the option.

type - I can't assume a type for you. By default it is a unicode instance, so you can omit this parameter if that's your use case.

cmd_name/cmd_short_name - I can try to assume that cmd_name is the same as the second positional argument, but once again, there could be a conflict. For example, if you want to have options.db.filename and options.log.filename, you can't use cmd_name = 'filename' for both. cmd_short_name is even worse, since here you may want a specific letter to be used (such as an upper case D instead of d). Note that these parameters are optional, since most of your values will likely go in the config file, not on the command line.

An alternative API might look like this:

  define_opt_int('server', 'shutdown_timeout')

  define_opt_bool('server', 'deameon')

  define_opt_cmd_unicode('server', 'pid_file', cmd_name='pid', cmd_short_name='p')
With a fallback to:

  define_opt('log', 'level', type=lambda x: x if x in LOG_LEVELS else 'DEBUG', cmd_name='log-level', cmd_short_name='L')
Any feedback is greatly appreciated.

[1] http://docs.python.org/dev/library/argparse.html


So maybe just define_opt('server.daemon'), with the rest given sane defaults and customizable as needed?

Interesting module nonetheless!


Dang, wished known about that a week ago when I was kludging it for a couple of utilities (thinking "someone must have a library for this but I can't find it!"). Thank you.


You are welcome. I hope you do end up using groper in other projects. Any feedback is greatly appreciated!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: