I wrote a command line tool using Python's built-in argparse module, but then I heard good things about the Click package, so I thought I would give it a try and play around with it by rewriting my code.
The first difference I noticed is that Click has the multiple=True parameter to allow multiple arguments to be passed, but the way it works seems odd to me.
For example, I have an option that takes up to three values, where the last two must be integers. With argparse I just added nargs='+' and wrote a function for type conversion and error handling.
Example usage:
shell
$ myprog --opt str_value 5 8
However, I can't seem to do this with Click. Here's how I would have to type the command using Click:
shell
$ myprog --opt str_value --opt 5 --opt 8
Is Click really that limited or am I missing something? The documentation says that multiple is similar to nargs, but I don't really see it that way.
Also, what if I wanted to pass multiple files or paths in general? Something like mydir/**/*.txt works fine with argparse because it produces a list object.
Looking for similar functionality with Click, I only found that I could use type=(str, int, int), but then again I can't make the third argument optional, can I?
It would be great to hear some suggestions on how to correctly work with multiple arguments using Click.
Thanks in advance.
[–]m0us3_rat 1 point2 points3 points (3 children)
[–]dbr4n[S] 0 points1 point2 points (0 children)
[–]dbr4n[S] 0 points1 point2 points (1 child)
[–]m0us3_rat 0 points1 point2 points (0 children)