User Tools

Site Tools


unixscripts:2-argp.sh

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
argp.sh [2017/03/20 18:46] adminunixscripts: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 ===== ===== argp.sh =====
  
-**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's just one essential file to install - the script itself. Put it somewhere on your path, typically in ''/usr/local/bin'':+**argp.sh**(1) is a **bash**(1) wrapper around **getopt**(1) (( 
 +See the [[https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html | rules ]] for processing options to see just how hard it is without **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's just one essential file to install - the script itself. Put it somewhere on your path, typically in ''/usr/local/bin''.
  
-  * {{scripts:argp.sh?linkonly|download}} +To use **argp.sh**(1), a script pipes in a description of its options (in XML or plain text) and gets back the variables as processed by **getopt**(1) - as a plus it also provides automatic help and man page production. 
- +
-To use **argp.sh**(1), a script pipes in a description of its options and gets back the variables as processed by **getopt**(1) - as a plus it also provides automatic help and man page production. +
  
 It buys you: It buys you:
Line 26: Line 25:
  
 There is also a 'c' version of **argp.sh**(1) in case you need speed. There is also a 'c' version of **argp.sh**(1) in case you need speed.
 +
 +=== Download & repo ===
  
   * Homepage, Releases and Repository: [[https://sourceforge.net/p/argpsh/wiki/Home/ | Sourceforge]]   * Homepage, Releases and Repository: [[https://sourceforge.net/p/argpsh/wiki/Home/ | Sourceforge]]
Line 31: 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.code.sf.net/p/argpsh/code/trunk argpsh-code   svn checkout svn://svn.code.sf.net/p/argpsh/code/trunk argpsh-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
      
-See the [[https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html rules ]] for processing options to see just how hard it is without **getopt**(1) !!!+      PROG=$(basename $0) 
 +      VERSION="1.2" 
 +      TYPE="f" 
 +      ARGUMENTS="[pattern]" 
 +      SHORT_DESC="Search for filenames in sub-directories." 
 +      USAGE="Presently this is just a shorthand for: 
 +   
 +  find . -follow -type $TYPE -name '*pattern*' -print 2>/dev/null |sort" 
 +   
 +      NEW_ARGS=( ) 
 +   
 +      ARGS=" 
 +  ARGP_DELETE=quiet 
 +  ARGP_VERSION=$VERSION 
 +  ARGP_PROG=$PROG 
 +  ARGP_PREFIX=FOO_ 
 +  ##############################################################    
 +  #OPTIONS: 
 +  #name=default sname arg       type range   description 
 +  ##############################################################    
 +  EXCLUDE=''    x     directory s    ''      exclude directory. 
 +  DIR='f'           ''        b    'd'     search for a directory rather than a file. 
 +  CD=''             directory s    ''      search from this directory rather than cwd. 
 +  ##############################################################    
 +  ARGP_ARGS=[--] $ARGUMENTS 
 +  ARGP_SHORT=$SHORT_DESC 
 +  ARGP_USAGE=$USAGE" 
 +   
 +      TYPE='f' 
 +   
 +      exec 4>&
 +      eval "$(echo "$ARGS" | argp.sh "$@" 3>&1 1>&|| echo exit $? )" 
 +      exec 4>&
 +   
 +      TYPE="$FOO_DIR" 
 +      [[ "$FOO_CD" ]] && { 
 +          cd "$FOO_CD" || exit 1 
 +      } 
 +   
 +      NEW_ARGS=( "$@"
 +      return 0 
 +  } 
 +   
 +  main() { 
 +      set -o noglob 
 +      SEP='' 
 +      for EXCLDIR in .svn .git nbproject .libs .deps CVS* $FOO_EXCLUDE; do 
 +          EXCLDIRS+="$SEP -path */$EXCLDIR " 
 +          SEP=" -o" 
 +      done 
 +   
 +      [[ "$FOO_VERBOSE" ]] && set -x 
 +      find . -follow \( $EXCLDIRS \) -prune -o -type $TYPE -name "*$1*" -print 2>/dev/null |sort 
 +  } 
 +   
 +  initialise "$@" && set -- "${NEW_ARGS[@]:-}" 
 +   
 +  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 '*pattern*' -print 2>/dev/null |sort 
 +  Options: 
 +   
 +    -C, --cd=directory         search from this directory rather than cwd. 
 +    -d, --dir                  search for a directory rather than a file. Default 
 +                               is 'f'
 +    -v, --verbose              be verbose 
 +    -x, --exclude=directory    exclude directory. 
 +    -h, --help                 print this help message 
 +    -V, --version              print version and exit 
 + 
  
unixscripts/2-argp.sh.txt · Last modified: 2020/10/17 03:03 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki