Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sokol_args: support single keys for native apps #876

Closed
edubart opened this issue Sep 3, 2023 · 4 comments
Closed

sokol_args: support single keys for native apps #876

edubart opened this issue Sep 3, 2023 · 4 comments

Comments

@edubart
Copy link
Contributor

edubart commented Sep 3, 2023

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
Copy link
Owner

floooh commented Sep 4, 2023

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
Copy link
Owner

floooh commented Sep 18, 2023

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

sokol/sokol_args.h

Lines 723 to 724 in 751fc4c

const key = p.value[0];
const val = p.value[1];

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
Copy link
Owner

floooh commented Sep 18, 2023

Ok, fixed via #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 (

static char* argv_11[] = { "exe_name", "kvp0 kvp1", "kvp2 = val2", "kvp3", "kvp4=val4" };
UTEST(sokol_args, key_only_args) {
sargs_setup(&(sargs_desc){
.argc = NUM_ARGS(argv_11),
.argv = argv_11,
});
T(sargs_isvalid());
T(sargs_num_args() == 5);
T(0 == sargs_find("kvp0"));
T(1 == sargs_find("kvp1"));
T(2 == sargs_find("kvp2"));
T(3 == sargs_find("kvp3"));
T(4 == sargs_find("kvp4"))
T(-1 == sargs_find("kvp5"));
T(-1 == sargs_find("val2"));
T(-1 == sargs_find("val4"));
T(sargs_exists("kvp0"));
T(sargs_exists("kvp1"));
T(sargs_exists("kvp2"));
T(sargs_exists("kvp3"));
T(sargs_exists("kvp4"));
T(!sargs_exists("kvp5"));
TSTR(sargs_value("kvp0"), "");
TSTR(sargs_value("kvp1"), "");
TSTR(sargs_value("kvp2"), "val2");
TSTR(sargs_value("kvp3"), "");
TSTR(sargs_value("kvp4"), "val4");
TSTR(sargs_value("kvp5"), "");
TSTR(sargs_value_def("kvp0", "bla0"), "bla0");
TSTR(sargs_value_def("kvp1", "bla1"), "bla1");
TSTR(sargs_value_def("kvp2", "bla2"), "val2");
TSTR(sargs_value_def("kvp3", "bla3"), "bla3");
TSTR(sargs_value_def("kvp4", "bla4"), "val4");
TSTR(sargs_value_def("kvp5", "bla5"), "bla5");
TSTR(sargs_key_at(0), "kvp0");
TSTR(sargs_key_at(1), "kvp1");
TSTR(sargs_key_at(2), "kvp2");
TSTR(sargs_key_at(3), "kvp3");
TSTR(sargs_key_at(4), "kvp4");
TSTR(sargs_key_at(5), "");
TSTR(sargs_value_at(0), "");
TSTR(sargs_value_at(1), "");
TSTR(sargs_value_at(2), "val2");
TSTR(sargs_value_at(3), "");
TSTR(sargs_value_at(4), "val4");
TSTR(sargs_value_at(5), "");
}
)

@edubart
Copy link
Contributor Author

edubart commented Sep 18, 2023

Thanks for improving this @floooh !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants