libelektra
libelektra copied to clipboard
libelektra-opts error codes
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:
https://github.com/ElektraInitiative/libelektra/blob/813f9c799cb94fc72e9e25ce69e1d1b2cf136053/src/tools/kdb/main.c#L135-L149
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