unixscripts:2-argp.sh
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
argp.sh [2015/12/15 05:27] – admin | unixscripts:2-argp.sh [2020/10/17 03:03] (current) – admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== argp.sh ===== | + | ===== Unixscripts: |
- | **argp.sh**(1) is a **bash**(1) 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**(1) is a **bash**(1) wrapper around **getopt**(1) (( |
+ | See the [[https:// | ||
- | * {{scripts: | + | To use **argp.sh**(1), |
- | + | ||
- | To use **argp.sh**(1), | + | |
It buys you: | It buys you: | ||
- | * all the goodness of **getopt**(1) | + | * all the [[https:// |
* define options in one central place together with descriptions. | * define options in one central place together with descriptions. | ||
* fewer ' | * fewer ' | ||
- | * better consistency between the **getopt**(1) calling parameters, the case statement processing the user's options and the help/man pages | + | * better consistency between |
+ | - the **getopt**(1) calling parameters, | ||
+ | - the case statement processing the user's options and | ||
+ | - the help/man pages | ||
* less spaghetti in your own code | * less spaghetti in your own code | ||
* much, much easier to maintain | * much, much easier to maintain | ||
Line 23: | Line 25: | ||
There is also a ' | There is also a ' | ||
+ | |||
+ | === Download & repo === | ||
* Homepage, Releases and Repository: [[https:// | * Homepage, Releases and Repository: [[https:// | ||
Line 28: | Line 32: | ||
To get the latest version of the full source code: | To get the latest version of the full source code: | ||
svn checkout svn:// | svn checkout svn:// | ||
+ | |||
+ | === Sample usage === | ||
+ | |||
+ | This is a simple wrapper around **find**(1) but it has enough options to make an interesting sample: | ||
+ | |||
+ | # | ||
+ | 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>&1 | ||
+ | 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.1450182460.txt.gz · Last modified: 2015/12/15 05:27 by admin