argbash icon indicating copy to clipboard operation
argbash copied to clipboard

Design better boolean arguments

Open matejak opened this issue 8 years ago • 6 comments
trafficstars

Currently, the ARG_OPTIONAL_BOOL does not behave optimally (see #2 ). It seems to be a good idea to have more macros for switch-on, switch-off and both (as the current ARG_OPTIONAL_BOOL somehow attempts to). Currently, ARG_OPTIONAL_BOOL assumes that one wants to switch something on (using long and short option) and autogenerating long option to switch something off. This falls on its head when one wants to switch something off. Proposed behavior:

  • ARG_OPTIONAL_BOOL will remain, it will autodetect whether it is in a switch-on or switch-off mode by examining the default (or the option whether it begins withno-..., I'm not yet decided). It will use the provided long and short options for the detected mode, but it will also generate a long option for the opposite mode.
    • Occurence of ARG_OPTIONAL_BOOL(no-video, v) would make the script accept --no-video, -v, that would set _arg_video (which would be on by default) to off, and --video, that would set _arg_video to on, overriding possible previous occurence of -v or --no-video.
    • Conversely, occurence of ARG_OPTIONAL_BOOL(video, v) would make the script accept --video, -v, that would set _arg_video (which would be off by default) to on, and --no-video, that would set _arg_video to off, overriding possible previous occurence of -v or --video.

New macros would be introduced:

  • ARG_OPTIONAL_SWITCH_ON will be introduced. It will accept long and short option (and no default since off will be assumed as default).
    • Occurence of ARG_OPTIONAL_SWITCH_ON(video, v) would make the script accept --video, -v, that would set _arg_video (which would be off by default) to on.
  • ARG_OPTIONAL_SWITCH_OFF will be introduced. It will accept long and short option (and no default since on will be assumed).
    • Occurence of ARG_OPTIONAL_SWITCH_OFF(no-video, v) would make the script accept --no-video, -v, that would set _arg_video (which would be on by default) to off. Everybody's comments are highly appreciated!

matejak avatar Jun 12 '17 20:06 matejak

After re-reading, so the only point of ARG_OPTIONAL_BOOL generating the negating option is overriding a possible previous occurence in the current args?

I can see the code being a bit cleaner using the proposed ARG_OPTIONAL_SWITCH_XXX behavior, but maybe not that intuitive to use. I would probably expect ARG_OPTIONAL_SWITCH_OFF(no-video, v) resulting in a _arg_no_video var.

edannenberg avatar Jul 04 '17 13:07 edannenberg

Good point, (any) code should read like a prose, so letting ARG_OPTIONAL_SWITCH_OFF(video, v) to create the --no-video option while saving the result to _arg_video should do the trick. There could be an option to set the prefix (default would be no-, people may be interested in disable- for --disable-video etc.).

matejak avatar Aug 07 '17 17:08 matejak

Hello! Is this still in the plans? The milestone is 2.7.0, but 2.8.0 got released a month ago. It's what would make argbash perfect for me.

0x5c avatar Feb 28 '19 13:02 0x5c

Hello, yes, it is deffinitelly planned. Stay tuned for updates!

matejak avatar Feb 28 '19 21:02 matejak

@matejak: Hi, will this issue be resolved? Seems like you got pretty far on your draft. Thanks

cconnert avatar Oct 24 '23 15:10 cconnert

It would also be very helpful to have a way to integrate with arithmetic condition (( expression )) in modern Bash, which returns successfully (status 0) when expression evaluates to nonzero, or else returns failure (status 1).

Something like ARG_OPTIONAL_BOOL_NUM, ARG_SWITCH_YES_NUM, ARG_SWITCH_NO_NUM would be great.

Here's the behavior of Bash arithmetic conditional construct:

$ if (( 1 )); then echo yes; else echo no; fi
yes
$ if (( 0 )); then echo yes; else echo no; fi
no
$ if (( "" )); then echo yes; else echo no; fi
no
$ if (( )); then echo yes; else echo no; fi
no
$ unset foo; if (( foo )); then echo yes; else echo no; fi
no
$ foo=2; if (( foo )); then echo yes; else echo no; fi
yes
$ foo=2; if (( $foo )); then echo yes; else echo no; fi
yes
$ foo=2; if (( "$foo" )); then echo yes; else echo no; fi
yes

ShamrockLee avatar Jan 12 '24 04:01 ShamrockLee