cli.md

@include "au"
@include "strings"

# If we can remove a leading "<tt>-</tt>" for an argument, it is a flag.
# If we can find that flag in our list of pre-defined options, it is a legal flag.
# If the flag starts with an upper case letter, we assume that there is
# an argument to read at the current input position, plus one. Else,
# we just set that flag to "1".


function cli(opt,input,n0,               n,key,i,j,k,tmp) {
  for(i=1;i<=n0;i++)  {
    key = input[i]
    if (sub(/^[-]+/,"",key))  { # ........................ [1]
      if (key in opt)         # ........................ [2]
         opt[key] = (key ~ /^[A-Z]/) ? input[++i] : 1 # [3]
      else {print "-"key" unknown; try -h for help"}     # [4]
    } else { i--; break }       # ........................ [5]
    }
  n=0
  for(j=i+1;j <= n0;j++) {n++; tmp[n]=input[j]} #................... [6]
  split("",input,"")                    #................... [7]
  for(j=1;j<=n;j++) input[j] = tmp[j]       #................... [8]
  return n                             #................... [9]
}