floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
6.63k stars 472 forks source link

sokol_args: support single keys for native apps #876

Closed edubart closed 10 months ago

edubart commented 10 months ago

I am using sokol_args.h as a library to handle command line arguments in an apps that can be compiled to WebAssembly or native (Linux in my case). When I use emrun myapp.html -- -myarg, while the argument -myarg is listed for WebAssembly, it's omitted in native, so there is a different behavior between native and WebAssembly.

I noticed this comment in sokol_args.h:

Currently 'single keys' without values are not allowed, but may be in the future.

Actually single keys works in WebAssembly, but not in native yet, I guess this behavior was not really defined in the library. I think the library should start defining this, so native could be improved to handle single key arguments like WebAssembly is.

floooh commented 10 months ago

Agreed, I was actually missing it too a couple of times. It should essentially be a shortcut for boolean args (bla=true vs just bla). I guess the reason why I didn't implement this to begin with is that it complicates the parser (also because sokol-args doesn't expect args to start with '-')

floooh commented 10 months ago

Hmm ok, on the web it works by accident, because the URLSearchParams object fill missing values with empty strings, e.g. here:

https://github.com/floooh/sokol/blob/751fc4c14a0cb80130ad8014f965ac62c7e89d34/sokol_args.h#L723-L724

const val will be an empty string.

I'll see if I can fix the 'native' arg parser function to work the same way.

PS: I probably still need to change behaviour a bit, because currently sargs_value() for a non-existing arg returns an empty string, so standalone args can't have an empty value string too.

floooh commented 10 months ago

Ok, fixed via https://github.com/floooh/sokol/pull/896

See the updated documentation for caveats (key-only args have an empty value string, but can be checked with sargs_exists() or sargs_boolean().

Also check the updated tests (https://github.com/floooh/sokol/blob/10f2c9c06699a47b910de370deea11e709df97fe/tests/functional/sokol_args_test.c#L256-L302)

edubart commented 10 months ago

Thanks for improving this @floooh !