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

libelektra-opts error codes #4953

Open
kodebach opened this issue May 28, 2023 · 1 comment
Open

libelektra-opts error codes #4953

kodebach opened this issue May 28, 2023 · 1 comment
Labels

Comments

@kodebach
Copy link
Member

Noticed in #4943

libelektra-opts uses our standard errorKey system to report errors. That's sufficient, if the errors are just displayed to the user, but if you want to do additional error handling for some cases you have to attempt parsing the human-readable error message.
For example:

const char * errorMessage = GET_ERR (parentKey);
if (elektraStrNCmp (errorMessage, "Unknown sub-command:", 20) == 0)
{
goto cpp;
}
if (elektraStrNCmp (errorMessage, "Unknown short option:", 21) == 0 ||
elektraStrNCmp (errorMessage, "Unknown long option:", 20) == 0)
{
result = 1;
}
else
{
result = 5;
}
fprintf (stderr, "ERROR: %s\n", errorMessage);

It would be nice, if elektraGetOpts set additional meta:/ keys to provide machine-readable details for the error.

For example:

# Unknown sub-command: foo
meta:/opts/error = missing_command
meta:/opts/error/command = foo

# Unknown short option: -x
meta:/opts/error = missing_option
meta:/opts/error/opt = -x

# Unknown long option: --xyz
meta:/opts/error = missing_option
meta:/opts/error/opt = --xyz

# This option cannot be repeated: -x
meta:/opts/error = repeated_option
meta:/opts/error/opt = -x

# Missing argument for short option: -x
meta:/opts/error = missing_argument
meta:/opts/error/opt = -x

# Missing argument for short option: --xyz
meta:/opts/error = missing_argument
meta:/opts/error/opt = --xyz

# This option cannot have an argument: -x
meta:/opts/error = illegal_argument
meta:/opts/error/opt = -x

# Expected at least 4  non-option arguments, but only got 3
meta:/opts/error = missing_parameter
meta:/opts/error/args/min = 4
meta:/opts/error/args/got = 3

# [All other errors]
meta:/opts/error = invalid_spec
@kodebach kodebach mentioned this issue May 28, 2023
19 tasks
Copy link

I mark this stale as it did not have any activity for one year. I'll close it in two weeks if no further activity occurs. If you want it to be alive again, ping by writing a message here or create a new issue with the remainder of this issue.
Thank you for your contributions 💖

@github-actions github-actions bot added the stale label May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant