kvz / bash3boilerplate

Templates to write better Bash scripts
http://bash3boilerplate.sh
MIT License
2.12k stars 198 forks source link

Default "on" for flags? #88

Closed gdevenyi closed 7 years ago

gdevenyi commented 7 years ago

I see examples for setting defaults for arguments with options, what about arguments without options?

zbeekman commented 7 years ago

If you pass it, it's on, if you don't it's off.... I don't get what you're asking

gdevenyi commented 7 years ago

I can define a argument like this:

 -t --temp  [arg] Location of tempfile. Default="/tmp/bar"

In which case it takes a default value.

Is it possible to default "enable" a flag and perhaps offer a "no" option to turn it off?

mstreuhofer commented 7 years ago

what stops you from assuming yes/on in your code and having a flag like this

-r --remove      Remove file after processing. This is the default.
-R --no-remove   Do not remove file after processing.

your code would then do something like

if [[ "${arg_R}" != "1" ]]
zbeekman commented 7 years ago

I still don't get it... Instead of --my-flag # off by default you can just negate it --not-my-flag # feature on by default How do you propose the parsing block would look, and what advantage use-case would there be over just creating a negated flag to be on by default?

gdevenyi commented 7 years ago

I'm dumb @mstreuhofer's solution is exactly what I should be doing.

Sorry.

zbeekman commented 7 years ago

no worries, I either already opened this request, or very nearly opened it before realizing that it's more or less a noop

kvz commented 7 years ago

Perhaps we can add this usecase to the FAQ or even right in main.sh, so that it's more apparent - what you do you all think?