Tools I use: argparse builder
blog.rfox.eu
blog.rfox.eu
Like, a Python program, a Ruby program, an online form, a REST API, and so on, all specify the input they need in ways specific to each tool (selecting from a predefined list is `CHOICES` param for argparse, a dropdown for a web form, etc). Why can't we just have some JSON spec for "input"? A list of params, each with a name, a description, a type, perhaps a list of possible choices -- the same sort of thing `argparse` exposes as its API.
Then, depending on the need, we can render that into a web page, or a parser, or whatever, and we can build libraries that can always expect to get input the same way without needing to redefine it all for each interface. Does something like this exist already and I just haven't seen it?
OpenAPI (Swagger) kind of, although AFAIK it doesn't lend itself to specifying how to map things onto a CLI. A good next step for the project?
* CUI
* TUI
* Web UI
* REST API
* Config files
* Environment Variables
* GUI
There are pieces of this in various places. There are a number of command line libraries that integrate config files, command line options, and environment variables, including Traitlets (used in Jupyter). The original Enthought Traits did this for GUIs (declare a set of of classes with datatypes, call .configure_traits() on them, and voila, an autogenerated GUI for them. The Django admin interface is very similar for the web. What's needed is to bring this into a single package.
In fact, I also implemented a similar idea to automatic GUI generation for my music player: https://github.com/albertz/music-player
I tried to summarize the idea here: https://github.com/albertz/wiki/blob/master/automatic-user-i...
Maybe you have some further good references?
But seriously though, the reason why this kind of standardised interface doesn't exist is because there's no one-size-fits-all solution. Everyone will "need" an extension to the spec, or start adding metasyntax to fit their specific use case.
The docs are open source, you can fix it and send a request a pull!
Also, your first sentence explains why I don't do what you suggest in your second sentence :-) Some people may not agree with my reasoning (which is obviously their right), not worth the trouble to figure out how to submit a patch and then risk having it rejected.
Also it avoids a pallets package, which is good because I prefer to avoid any unnecessary exposure to its maintainer.
Nearly every script I write only requires something like the following:
for arg in sys.argv[1:]:
if arg == '-v':
...
if ...:
print(f"Usage: {sys.argv[0]} -v ...")
In the very rare occasion that I have an actually large command line project, argparse is built in and works fine.or -- -v (-v is forced to be interpreted as positional in this case since it comes after --).
To be honest, the extent of my command line parsing is almost always just this:
try:
infile = sys.argv[1]
outfile = sys.argv[2]
except IndexError:
print(f"Usage: {sys.argv[0]} <infile> <outfile>")Since most systems have Python, distributing your argparse driven file means curling it down and making it executable instead of having to pip install things.
https://github.com/google/python-fire
Just comment your code and get pretty arguments.
It's a great example of a project that only works (without codegen / build toolchain-support) for a dynamic language.
https://github.com/docopt/docopt
It is a simple DSL with implementations available in multiple languages. I've used it in python, GoLang and in Java too. Lots of examples too.
https://github.com/erikhuda/thor
Because it's great that you have a library to parse args... but that's only about half of what you actually want when you're building a snazzy CLI tool.
New argparse params are either uncommented and/or cut and pasted from existing ones. It works pretty well.
Now that I look closer, I actually have to use the File menu, it doesn't have a default shortcut assigned.
https://github.com/Woile/decli
But nowadays I would recommend something like typer: https://typer.tiangolo.com/ it's quite nice and has autocomplete built in.
Admittedly, the way it works is kind of black magic, so it's probably not for everyone, but it's so simple: just annotate an existing variable with Option and it becomes a command-line argument. You can define options where they are used in the program instead of defining them in one spot and then using them somewhere else.
Isn't there anything like structopt https://docs.rs/structopt/0.3.19/structopt/ for Python?
The thing that makes argparse particularly useful is because it is in stdlib, so if you use you can create a Python script without external dependencies.