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

Allow checking whether optionals provided explicitly #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kstrafe
Copy link

@kstrafe kstrafe commented Mar 14, 2024

Since boolean optionals default to "off" we have no way to check if a boolean optional was actually specified on the command line.

The ability to check if a boolean optional has been given is useful in cases where we source values from a configuration file where we want any explicitly given arguments to override the configuration.

An example that mostly works is (for plain optionals with an empty default):

    test -f ~/.standard-values-for-this-script && source "$_"
    MYOPT="${MYOPT:-$_arg_myopt}"
    ... # Code using MYOPT

This works since optionals can default to an empty string. We still are not entirely sure if the optional was actually passed here, but it tends to be "good enough" in most cases.

In the case where myopt is boolean, the above case would always use "off" if the argument was not specified, thus overriding the sourced configuration file.

This patch introduces _optionals, an array where each optional inserts itself if they were actually given.

This allows us to do the following:

    test -f ~/.standard-values-for-this-script && source "$_"
    if [[ ${_optionals[@]} =~ _arg_myopt ]]; then
            MYOPT="$_arg_myopt" # We override
    fi
    ... # Code using MYOPT

Since boolean optionals default to "off" we have no way to
check if a boolean optional was actually specified on the command line.

The ability to check if a boolean optional has been given is useful in
cases where we source values from a configuration file where we want any
explicitly given arguments to override the configuration.

An example that mostly works is (for plain optionals with an empty default):

        test -f ~/.standard-values-for-this-script && source "$_"
        MYOPT="${MYOPT:-$_arg_myopt}"
        ... # Code using MYOPT

This works since optionals can default to an empty string. We still are
not entirely sure if the optional was actually passed here, but it tends
to be "good enough" in most cases.

In the case where `myopt` is boolean, the above case would always use
"off" if the argument was not specified, thus overriding the sourced
configuration file.

This patch introduces `_optionals`, an array where each
optional inserts itself if they were actually given.

This allows us to do the following:

        test -f ~/.standard-values-for-this-script && source "$_"
        if [[ ${_optionals[@]} =~ _arg_myopt ]]; then
                MYOPT="$_arg_myopt" # We override
        fi
        ... # Code using MYOPT
@kstrafe
Copy link
Author

kstrafe commented Mar 14, 2024

Posting this PR to get some feedback, it's why I haven't written any tests. Let me know if this is a better way to implement this. I think this feature is really useful.

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

Successfully merging this pull request may close these issues.

None yet

1 participant