Clapping In Web

In the spirit of Sister Kiyou...
Announcing the cliw crate, Command Line In Web.
And we-clap (web enabled clap)

The motivation is I wanted my egui app to take come command line arguments, and wanted it to also work on the web.

Normal clap is broken, it doesn't work on the web. There have been some threads on the clap github in the past and at least a few times people on this group ask how to parse url query strings.

There are plenty of examples
Here is a screencast of a basic egui app running on a webpage and parsing the URL as a command line.

we-clap_egui_demo

Next steps I try to figure out how to do a proper pull request to the clap main repository. @epage

Thanks for your honest reviews.

1 Like

Mildly horrifying :sweat_smile:

I think it's about as good an solution as any to get this working, but the conventions are different enough I think it's pretty reasonable for clap to consider the web unsupported. For example, sight unseen I would expect --help to translate to ?help and --foo bar to be ?foo=bar, though I can guess as to why that might not work in the context of clap.

6 Likes

Everything is just like clap, just spaces are ? or &.

Looking over the commits, it seems this mostly changes the default source of argv and the default location to print to. I feel like this is specialized enough that we probably don't want baked in support for this but to use the integration points we offer (e.g. Command::get_matches_from, impl DIsplay for Error, Error::render) to integrate with something like this. If there are remaining points in clap that can cause problems, that would be worth opening an issue for us to explore how we should handle them.

1 Like

If in the web, maybe it's better to follow the web's convention, like embedding arguments in regular query parameters without leading --:

http://localhost:8080/?positional_argument&foo=bar&key=value

Which would be interpreted like:

./example positional_argument --foo=bar --key=value
2 Likes

Thank for looking.
I had a feeling you would not be too interested in including my patches. You have said in the past on clap discussion that you haven't used clap on wasm.

But I do feel that a way to clap on the web is not "specialized" The web may account for half of computer program use. (trump style statistic pulled from the air)

I may, if have time, go to the clap repository and bring up some discussion in the Designing Clap for Extensibiliy chat and link to some past issues.

Maybe there is interest in others, maybe not. I was thinking out doing it all outside clap, calling into clap and doing the work outside. But I could not think of a way to do it that would make it easy to use for others. My goal is to make it so users don't need to learn anything new to use it, just use clap like normal and it works on the web.

1 Like

Clap uses -- to tell if it is option or just an argument. I don't want to change behavior or how to use. Just replace the argument separator with ? and & instead of using spaces. There is a benefit that you don't need to use quotes around stings with spaces.

1 Like

While web applications are prolific, using CLI flags through query strings is the part that seemed niche.

7 Likes

Honestly, being able to Just Let Clap Handle It for a WASM port of a desktop app sounds like a really neat feature.

But then, I don't really feel it is all that common to use command line args for a GUI app.

That's because it was never so easy to get clap powers on the web.

Any way, thanks for the rejection. I needed the motivation to do it outside of clap. So I wrote we_clap

I thought it would be easier to just patch clap, but I think a separate crate is better. Of course I could use some help from a clap pro. I want to change the error/help output to use "?" and "&" instead of spaces.

Anyway, here is the announcement.

As I said elsewhere, it wasn't easy to get clap power on the web before.
I think after I submit a patch to official egui_demo_app, using we_clap to read the args, "--profile" for example, and adding args that pass values to the demo. After I write some examples with ratatui and ratzilla that can run on native or web, and take arguments. After the world sees the beauty, it will flourish.

Or maybe I will just use it for myself.

Just a few off the top of my head. VLC, Firefox, LibreOffice, Chrome, Stellarium, Blender; all have command line arguments.