unixscripts:2-argp.sh
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
argp.sh [2015/12/15 01:58] – admin | unixscripts:2-argp.sh [2020/10/17 03:03] – ↷ Page moved and renamed from argp.sh to unixscripts:2-argp.sh admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | **argp.sh**(1) is a wrapper around **getopt**(1) to make life much easier for script developers. It's used in most of my scripts - look at them for examples of use. There' | + | ===== argp.sh |
- | | + | **argp.sh**(1) is a **bash**(1) wrapper around **getopt**(1) (( |
+ | See the [[https://www.gnu.org/software/libc/ | ||
- | Note that **argp.sh(1)** deprecates an older script | + | To use **argp.sh**(1), a script |
- | There is a ' | + | It buys you: |
+ | * all the [[https:// | ||
+ | * define options in one central place together with descriptions. | ||
+ | * fewer ' | ||
+ | * better consistency between | ||
+ | - the **getopt**(1) calling parameters, | ||
+ | - the case statement processing the user's options and | ||
+ | - the help/man pages | ||
+ | * less spaghetti | ||
+ | * much, much easier to maintain | ||
+ | * automatic help-page and man-page printing using the same data sources | ||
+ | * checking of option consistency at runtime | ||
+ | * range checking of option values at runtime | ||
+ | * pretty easy to use (just copy and paste from one of my example scripts) | ||
+ | * portable to OS's without long option support - the help page adapts too | ||
- | | + | Note that **argp.sh(1)** deprecates an older script that I wrote called **process-getopt**(1). |
- | [[https:// | + | There is also a ' |
- | | + | === Download & repo === |
- | <code> | + | |
+ | * Homepage, Releases and Repository: [[https:// | ||
+ | |||
+ | To get the latest | ||
svn checkout svn:// | svn checkout svn:// | ||
- | </code> | + | |
+ | === Sample usage === | ||
+ | |||
+ | This is a simple wrapper around **find**(1) but it has enough options to make an interesting sample: | ||
+ | |||
+ | #!/usr/bin/env bash | ||
+ | initialise() { | ||
+ | local TEMP | ||
+ | |||
+ | PROG=$(basename $0) | ||
+ | VERSION=" | ||
+ | TYPE=" | ||
+ | ARGUMENTS=" | ||
+ | SHORT_DESC=" | ||
+ | USAGE=" | ||
+ | |||
+ | find . -follow -type $TYPE -name ' | ||
+ | |||
+ | NEW_ARGS=( ) | ||
+ | |||
+ | ARGS=" | ||
+ | ARGP_DELETE=quiet | ||
+ | ARGP_VERSION=$VERSION | ||
+ | ARGP_PROG=$PROG | ||
+ | ARGP_PREFIX=FOO_ | ||
+ | ############################################################## | ||
+ | #OPTIONS: | ||
+ | # | ||
+ | ############################################################## | ||
+ | EXCLUDE='' | ||
+ | DIR=' | ||
+ | CD='' | ||
+ | ############################################################## | ||
+ | ARGP_ARGS=[--] $ARGUMENTS | ||
+ | ARGP_SHORT=$SHORT_DESC | ||
+ | ARGP_USAGE=$USAGE" | ||
+ | |||
+ | TYPE=' | ||
+ | |||
+ | exec 4>& | ||
+ | eval " | ||
+ | exec 4>& | ||
+ | |||
+ | TYPE=" | ||
+ | [[ " | ||
+ | cd " | ||
+ | } | ||
+ | |||
+ | NEW_ARGS=( " | ||
+ | return 0 | ||
+ | } | ||
+ | |||
+ | main() { | ||
+ | set -o noglob | ||
+ | SEP='' | ||
+ | for EXCLDIR in .svn .git nbproject .libs .deps CVS* $FOO_EXCLUDE; | ||
+ | EXCLDIRS+=" | ||
+ | SEP=" -o" | ||
+ | done | ||
+ | |||
+ | [[ " | ||
+ | find . -follow \( $EXCLDIRS \) -prune -o -type $TYPE -name " | ||
+ | } | ||
+ | |||
+ | initialise " | ||
+ | |||
+ | main " | ||
+ | |||
+ | Here is the output from the --help option auto-generated by **argps.sh**(1): | ||
+ | |||
+ | Usage: ff [OPTION...] [--] [pattern] | ||
+ | Presently this is just a shorthand for: | ||
+ | |||
+ | find . -follow -type f -name ' | ||
+ | Options: | ||
+ | |||
+ | -C, --cd=directory | ||
+ | -d, --dir search for a directory rather than a file. Default | ||
+ | is ' | ||
+ | -v, --verbose | ||
+ | -x, --exclude=directory | ||
+ | -h, --help | ||
+ | -V, --version | ||
+ | |||
unixscripts/2-argp.sh.txt · Last modified: 2020/10/17 03:03 by admin