Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I find it's much better to have a copy-pasted piece of code which turns all command line options into variables in a certain namespace (provided the variables are defined beforehand)

I.e. user runs:

  script --foo --bar=yes --no-xyzzy --uiuez"
the script then sets these existing variables:

  opt_foo=y
  opt_foo_given=y

  opt_bar=y
  opt_bar_given=y

  opt_xyzzy=
  opt_xyzzy_given=y
However, the variable opt_uiuez doesn't exist and so it bails:

  script: no such option: --uiuez
With such a piece of code, all you do is define the options you support via assignments like "opt_foo=". You can give them default values this way. Include that piece of boiler-plate code. Done.

To add a new option, just define the variable. Done.

Check its value wherever needed, and possibly the _given, if the code needs to know whether it's working with the default value, or an explicitly given value. That's it.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: