3 ms·
> 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 someti
by masklinn 13y ago
> 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
?
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 http://click.pocoo.org/api/#click.File, argparse has something similar.
- timtadh 13y ago@masklinn good point. I haven't used argparse (mostly out of compatibility requirements with 2.6). So I may have mis-characterized the state of the art. It does do many things well. One note, is that the `prefix parsing` functionality could cause weird behavior when doing partial parsing (which I do a lot of). > 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 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.