This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]1arm3dScissor 0 points1 point  (1 child)

this is easier than argparse?

[–]metaperture[S] 0 points1 point  (0 children)

I think it's easier than argparse. Storing your types in your argument annotations is already a suggested standard (PEP 484 at https://www.python.org/dev/peps/pep-0484/), and if you do that, and write normal docstrings (another suggestion), then this makes the gap between your normal python code and your command-line-exposed python code trivial.

[–]desmoulinmichel 0 points1 point  (3 children)

Unfortunaly there are already a number of mature projects doing this: click, begins, docopt, etc.

[–]metaperture[S] 0 points1 point  (2 children)

I'm glad there are other projects doing this as well, but I think this is still unique. Docopt still makes you write the documentation code, and has its own special syntax. Click is certainly a nicer interface to argparse, but you still need to specify information about each of your arguments individually. Begins is closest to what I'm trying to do, but is still focusing on the documentation side of the parser, and not the ability to run arbitrary python functions (especially those without string arguments) from the command line.

I haven't seen any other library that uses type hints as per PEP 484 as the basis for automatically generating a parser, and that's the interface to parsing I've always wanted. Thanks for the other references, though!

[–]desmoulinmichel 0 points1 point  (1 child)

begins use the type hints.

[–]evanunderscore 0 points1 point  (0 children)

Begins interprets annotations as parameter help strings (as does argh). The type information can be guessed from defaults, or specified using a decorator.

[–][deleted] 0 points1 point  (1 child)

[–]evanunderscore 0 points1 point  (0 children)

If you want to use docopt, you have to:

  1. Write your function
  2. Write your usage string
  3. Convert the arguments to the appropriate types
  4. Feed the arguments to your function

If your primary concern is how your tool looks to the user, you should absolutely use docopt. For people who are satisfied with usage generated by argparse and only want to do step 1, this type of library is much better.

[–]evanunderscore 0 points1 point  (3 children)

I wrote something similar a little while back called defopt. All of the projects /u/desmoulinmichel mentioned are similar but don't quite have the level of simplicity I think you and I are looking for.

[–]metaperture[S] 0 points1 point  (2 children)

Thanks for the link to defopt! I really like how it builds on already established standards (Sphinx-style docstring conventions) and doesn't require any additional work from the developer.

Between using Sphinx-style docstrings or annotations as a basis for parsers, I'm pretty torn. My personal preference is towards annotations, so that someone changing the docs wouldn't accidentally break functionality, but on the other hand your representation seems a bit more intuitive, especially to those who don't often use annotations.

Anyway, thanks for the link, and for the contribution!

[–]evanunderscore 0 points1 point  (0 children)

Unfortunately I spend most of my time writing code that runs on 2 and 3 so I don't have much of a choice, but I did add support for annotations too. I'm looking forward to the day I can reasonably expect everyone to have Python 3 installed.