Hacker News new | ask | show | jobs
by shangxiao 1714 days ago
I don't have a startup but I did make a useful little Python library while at my previous job to help ease the pain with using argparse to make CLIs.

It's called bargeparse [1]. The idea is to define an entry point function and use its signature as a means to declare the CLI arguments & options. I was surprised at how well Python's syntax for function arguments works for this... especially the use of pos-only & keyword-only arguments to force a particular style.

[1] https://github.com/shangxiao/bargeparse

5 comments

This is a cool one. I once stumbled upon bargeparse and have used it several times. And, you're right: argparse can cause some pain ;)

I've taken the liberty to post your project to my Python news aggregator over here[1].

[1] https://news.python.sc/item/0d111c5d-0eff-41d5-8d5c-ecb18301...

Python-fire is a great package for this. Will look into bargeparse

https://github.com/google/python-fire

Wow, that looks extremely useful. I typically use docopt for all my CLI needs, but it looks like this could be really nice as well. I will have to try it the next time I'm building a CLI.
I love that! Argparse is such an old and boring solution to such a simple problem that we need more like this. And I have hope that one of the solutions will make it into the standard library at some point.

There are some others like yours:

Google's python-fire (1)

Docopt (2), which creates the cli from the documentation instead of the other way around

Click (3)

And my own commandeer (4)

Typer (5)

(1) https://github.com/google/python-fire (2) http://docopt.org/ (3) https://click.palletsprojects.com/en/8.0.x/ (4) https://pythonhosted.org/Commandeer/index.html (5) https://typer.tiangolo.com/

What's the benefit over well established solutions like typer ?
Typer looks neat. I don't think it was well-established when I started on this, there were quite a few offerings but the only one I managed to find was python-fire. It didn't quite do what my team wanted and there were a few behaviours that we didn't quite like so we decided to make this.

Having said that bargeparse was meant to rely solely on stdlib as the environment we were operating in was tightly controlled and any 3rd party packages had to go through a lengthy review process to be whitelisted.

Additionally, since our team (and others in our organisation) were mostly using argparse, the goal was to provide a way to declaratively interact with it using existing features in the language itself.

I used typer as an example because it has a modern twist, yet is based on click which has been the leader for 7 years.

If you want something older and with a similar api (wrapping argparse), using only the stdlib, I can ask the same question with argh, which already worked in python 2.6.

So why write another one?

I’ve done this with my own crappy code, optparse, argparse, hug, fire, click, and typer. For me typer is pretty much perfect…
Typer has the wonderful fast api paradigm and the battle tested click foundation. Pretty much a match made in heaven.
I can make a guess: GP does not know, he started working on the software and pointlessly duplicated existing projects, but with worse quality, because he neglected to survey the landscape.

I have noticed that happening regularly and out of curiousity interviewed those fellow programmers. I found out it's not because of lack of diligence, or because they attribute no value to a survey, but because of a risk-loving psychology trait that expresses itself in staking out ground and claiming it as one's own. (Armchair psychology warning, I am not qualified to make that conclusion; leave the real work confirming this hypothesis to the scientists in the field.)

IMO, this behaviour is harmful and we should work together to socially shun it like we already do for compulsive hoarding of physical objects. We have trouble finding a piece of software that works well because we spend so much effort to repeatedly sift through the mountains of published software that is mostly garbage.

Couldn't be further from the truth, see my reply.