vx.vaparse
vx.vaparse (ParseParameters,[pname=True],[args=- ]
Parse command line arguments for a standalone python program. The command line arguments are taken from sys.argv by default. The second positional argument can specify an alternative list of parameters to parse. The v4 protocol allows for options (-abc) and prefixed (if=<value>) parameters. Positional non-prefix parameters are associated with the prefix parameters in the order specified. A '-' parameter is used to print the usage list.
Parameters
ParseParameters: | str: parameter list in v4 style list: (optional)List of arguments (e.g.,as specified by sys.argv) |
---|---|
pname=: | may be set to True: include the first argument (program-name). The default it to omit the program name |
args=: | list: specify the argument list to be parse (by default the
argument list is set to sys.argv).
in the argument list (default is to omit) |
Examples
In python standalone programs the command line arguments are provided as a list called sys.argv; vaparse uses this by default. vaparse can also be explicitly given the argument list with the optional args= parameter; this feature is used in the following examples.
V4 argument specification
V4 argument parsing supports prefix matching for values and
exact matching for options; arguments may be in any order.
Arguments that do not have a prefix are matched to the next available
prefix in the argument specification. Any unmatched arguments
are returned in a list called umatch.
For v4 argument parsing, the argument specification consists of a string containing space-separated argument prefixes which end in "=" and options which start with "-". The prefix may be used in the argument list: for example, "if=fred" would be matched to "if=" in the argument specification.
1. prefix arguments
import vx as vx
alist = ['testag.py', 'c=first', 'a=second', 'b=third']
print ('1. ', vx.vaparse( 'a= b= c=', args=alist))
print ('2. ', vx.vaparse( 'a= b= c=', args=alist, pname=True))
print ('3. ', vx.vaparse( 'a= b= c= d= f=', args=alist, pname=True))
print ('4. ', vx.vaparse( 'f= a= b= c= d=', args=alist, pname=True))
1. {'c': 'first', 'a': 'second', 'b': 'third'}
2. {'a': 'testag.py', 'c': 'first', 'b': 'third', 'umatch': ['a=second']}
3. {'a': 'testag.py', 'c': 'first', 'b': 'third', 'umatch': ['a=second']}
4. {'f': 'testag.py', 'c': 'first', 'a': 'second', 'b': 'third'}
2. Option arguments
list = ['testag.py','-c', '-abc', '-d']
print ('1. ', vx.vaparse( '-a -abc -ab -d -qq pf=', args=blist))
print ('2. ', vx.vaparse( '-a -abc -ab -d -qq pf=', args=blist, pname=True))
print ('3. ', vx.vaparse( 'a= b= c=', args=blist))
1. {'-abc': '-abc', '-d': '-d', 'umatch': ['-c']}
2. {'pf': 'testag.py', '-abc': '-abc', '-d': '-d', 'umatch': ['-c']}
3. {'umatch': ['-c', '-abc', '-d']}
3. Both argument types
clist = ['testag.py', '-x', 'c=first', '-zz', 'a=second', 'b=third']
print ('1. ', vx.vaparse( 'a= b= -x cat= -yy -zz', args=clist))
print ('2. ', vx.vaparse( 'a= b= c=', args=clist))
print ('3. ', vx.vaparse( 'a= -x -xx b= c= -y -yy -abc -zz', args=clist))
1. {'-x': '-x', '-zz': '-zz', 'a': 'second', 'b': 'third', 'umatch': ['c=first']}
2. {'c': 'first', 'a': 'second', 'b': 'third', 'umatch': ['-x', '-zz']}
3. {'-x': '-x', 'c': 'first', '-zz': '-zz', 'a': 'second', 'b': 'third'}