by the_mitsuhiko · 12y ago · 0 comments
The main idea behind it is make it easy to write a "getopt" style program with arbitrary command nesting. I have found that although argparse and sisters are nice libraries they don't allow me to do many of things I like to do in my interfaces. For instance sometimes like options such as
foo -x a -x y -x q ...
where I would process that into like so: extras = list()
for opt, arg in opts:
if opt in ('-h', '--help',):
util.usage()
...
elif opt in ('-x', '--extra'):
extras.append(validate_or_die(arg))
I also believe that you should have "fast fail" validators. So I have several in `optutils` which are like: util.assert_dir_exists(path)
which if a directory doesn't exist on the path it creates it. If there is already a file there and it isn't a directory it dies with an error. When it dies, I try and have unique exit codes for various errors (for testability) and provide usage information immediately. This style is nice because it provides immediate feedback to the user with no fuss. I think a lot of "option parser frameworks" miss the point in having lots of things for parsing ints and things. Most of the time I deal with files, directories, and "string" parameters which these libraries don't help with.In general, the standard libraries make it way to hard to write really nice command line tools. I like some things about your library, but I think that you need to increase the flexibitly for how options are processsed to you can do whatever you want with them. I also think that option parsing and configuration should be integrated. I am working to support that but I am not there yet. (see optutils/conf.py for my current ideas)
?
This is trivial to do with argparse (or optparse for that matter):
import argparse
def a_prefixed(string):
if not string.startswith('a'):
raise argparse.ArgumentTypeError("%r does not start with 'a'" % string)
return string
parser = argparse.ArgumentParser()
parser.add_argument('-x', '--extra', action='append', type=a_prefixed)
print parser.parse_args()
resulting in: > python test.py -x afoo -x abar
Namespace(extra=['afoo', 'abar'])
> python test.py -x afoo -x baz
usage: test.py [-h] [-x EXTRA]
test.py: error: argument -x/--extra: 'baz' does not start with 'a'
> I also believe that you should have "fast fail" validators.Isn't that what the callback parameter is for, especially with is_eager=True? Or ParamType if you need either something more reusable or something more extensive.
> Most of the time I deal with files, directories, and "string" parameters which these libraries don't help with.
http://click.pocoo.org/api/#click.File, argparse has something similar.
> Isn't that what the callback parameter is for, especially with is_eager=True? Or ParamType if you need either something more reusable or something more extensive.
yes. I think almost everything should work like this. I think the getopt style makes this a bit easier to understand.
> http://click.pocoo.org/api/#click.File, argparse has something similar.
Not at all the same. I never said my programs were going to open the files themselves. I often have to write automation scripts around other things. In these cases I need to make sure files and directories are sane but I don't open them. I just canonicalize them and pass them on.
The big thing is the lack of integration with configuration files which is something I am still working on myself.
https://github.com/seaneagan/unscripted
Checkout the github issues there for some features I want to add, like bash completion support for example. I think we can steal ideas from each other!
One nice thing about dart's annotations vs. python's decorators is they can be placed on a function's parameters as well, so it allows the option/flag/argument declarations to be a bit more DRY.