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

Getopts or Case for options? #784

Closed
tschettervictor opened this issue Jan 1, 2025 · 6 comments
Closed

Getopts or Case for options? #784

tschettervictor opened this issue Jan 1, 2025 · 6 comments

Comments

@tschettervictor
Copy link
Collaborator

@bmac2 @yaazkal @JRGTH

I have read many programs that use "while getopts" for their option handling. I notice also that bastille uses mostly "case" for its options.

With the former, we are able to bunch options together like "-fsm" wile with the latter this is not possible unless we add more case blocks.

With the latter, we have the ability to add "long options" like "--force" in addition to "-f".

I'd like to know if we should stay with "case" or perhaps consider integrating "getopt" as the option handler.

Thanks.

@bmac2
Copy link
Collaborator

bmac2 commented Jan 1, 2025

I vote to stay with CASE as we are already with case all over the place. Changing now could make it confusing. I like consistency

@tschettervictor
Copy link
Collaborator Author

That's where I'm leaning towards. I like getopts for what it allows, but it's not a necessity.

@JRGTH
Copy link
Collaborator

JRGTH commented Jan 2, 2025

I also vote to stay with case/esac too, while it may require more bits it is way more flexible, better error handling, portable etc, also be aware that getopts(built-in shell command) only handles short options unlike getopt(program), between I personally use case/esac for full POSIX-compliant as most of the already built-in FreeBSD shell commands.

Regards!

@tschettervictor
Copy link
Collaborator Author

tschettervictor commented Jan 2, 2025

Cool.

Here is a snippet I built to work for things like "-fsr" or any number of arts with case.

# Handle options.
BRIDGE_VNET_JAIL=0
FORCE=0
STATIC_MAC=0
START=0
VNET_JAIL=0
while [ "$#" -gt 0 ]; do
    case "${1}" in
        -h|--help|help)
            usage
            ;;
        -b|-B|--bridge)
            BRIDGE_VNET_JAIL=1
            shift
            ;;
        -f|--force)
            FORCE=1
            shift
            ;;
        -m|-M|--static-mac)
            STATIC_MAC=1
            shift
            ;;
        -s|--start)
            START=1
            shift
            ;;
        -v|-V|--vnet)
            VNET_JAIL=1
            shift
            ;;
        -*)
            for _o in $(echo ${1} | sed 's/-//g' | fold -w1); do
                case ${_o} in
                    b|B) BRIDGE_VNET_JAIL=1 ;;
                    f) FORCE=1 ;;
                    m|M) STATIC_MAC=1 ;;
                    s) START=1 ;;
                    v|V) VNET_JAIL=1 ;;
                    *) error_exit "Unknown Option: \"${1}\"" ;; 
                esac
            done
            shift
            ;;
        *)
            break
            ;;
    esac
done

@JRGTH
Copy link
Collaborator

JRGTH commented Jan 2, 2025

The above snippet seems to handle short options well and looks consistent with the existing code, just a little cosmetic change I would make is to change the variable _o for someting like _opt to keep the code as readable as possible for future contributors ease, between nice work here.

Regards!

@tschettervictor
Copy link
Collaborator Author

Will do. I've update PR #783 with this now.

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

No branches or pull requests

3 participants