unixscripts:3-decentbash
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
decentbash [2015/12/31 02:44] – created admin | decentbash [2020/10/17 03:03] – ↷ Links adapted because of a move operation admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | Here's my five penn' | + | =====Decent Bash===== |
- | * use **getopt**(1) to process arguments. There are many good reasons for this. Just do it. Let me count (some of) the ways: | + | |
+ | Here's my five penn' | ||
+ | |||
+ | ====Use getopt(1)==== | ||
+ | Use **getopt**(1) to process | ||
* provides a rational, predictable user interface - compare the nasty old peculiarities of tar and find!! | * provides a rational, predictable user interface - compare the nasty old peculiarities of tar and find!! | ||
* supports short option merging eg '' | * supports short option merging eg '' | ||
* disambiguates option argument spacing eg foobar -z1 == foobar -z 1 | * disambiguates option argument spacing eg foobar -z1 == foobar -z 1 | ||
* re-orders options so they can be processed and then popped and forgotten | * re-orders options so they can be processed and then popped and forgotten | ||
- | * supports -- to terminate options | + | * supports |
- | * plain old simple works - why write bug-prone new code when getopt just works? | + | * plain old simple works - why write bug-prone new code when **getopt**(1) just works? |
- | | + | |
- | | + | ====Use argp.sh(1)==== |
- | * create getopt command line, | + | Even better, use my **getopt**(1) wrapper [[unixscripts: |
- | * process the options | + | * create |
- | * print help & man pages | + | * process the options |
- | * help page printing is automatically pretty - lines wrapped, nicely spaced out etc | + | * print help & man pages without any extra code |
- | | + | * honours GNU's [[http:// |
- | * is pretty similar to GNU's argp getopt wrapper in glibc | + | * is pretty similar to GNU' |
- | * further reduces the chance of bugs arising - particularly as the script is maintained | + | * further reduces the chance of bugs arising - particularly as the script is maintained |
- | * increase the chance that help and man pages are actually written | + | * increase the chance that help and man pages are actually written |
- | | + | |
- | | + | ====Support Long options==== |
- | | + | Always |
- | | + | |
- | | + | ====Provide help==== |
- | | + | respond to the **-h, --help** |
- | | + | |
- | | + | If there are specific dependencies, |
- | | + | |
+ | ====Provide version info==== | ||
+ | provide a way to get the program' | ||
+ | |||
+ | Note that **-h** and **-V** processing should happen before any other substantial processing or checking is done - make sure **-h** can always be done no matter what | ||
+ | |||
+ | ====Be disciplined with error messages==== | ||
+ | Error messages should go to **!!!STDERR!!!** - do **not** print the usage as it just fills the screen and hides the meat of the error. At the most, refer the user to the usage page with '' | ||
+ | |||
+ | ====Be disciplined with error exit codes==== | ||
+ | non-zero exit on any error | ||
+ | |||
+ | ====Silence is golden==== | ||
+ | execute as silently as possible so the user doesn' | ||
+ | |||
+ | The exception, of course, is if a verbosity command is given, generally with **-v, --verbose** - and the output goes to stdout, thank you very much (so we can distinguish bash's own -x output which goes to stderr) | ||
+ | |||
+ | ====man(1) page==== | ||
+ | include a man page if non-trivial | ||
+ | |||
+ | ====Use a crunchbang==== | ||
+ | always include the // | ||
+ | |||
+ | #! / | ||
+ | |||
+ | ====Structure the code==== | ||
+ | use subroutines heavily - only put the following in the mainline: | ||
+ | initialise | ||
+ | process_options " | ||
+ | main " |
unixscripts/3-decentbash.txt · Last modified: 2020/10/17 03:04 by admin