File: parseargs.py

package info (click to toggle)
petsc 3.7.5%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 163,864 kB
  • ctags: 618,438
  • sloc: ansic: 515,133; python: 29,793; makefile: 20,458; fortran: 18,998; cpp: 6,515; f90: 3,914; sh: 1,012; xml: 621; objc: 445; csh: 240; java: 13
file content (46 lines) | stat: -rwxr-xr-x 1,481 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
#!/bin/env python
# $Id: parseargs.py,v 1.1 1999/11/12 22:30:17 balay Exp $
#
#  Parses the argument list, and searches for the specified argument.
#  It returns the argument list minus the parsed argument.
#
#  Calling sequence:
#      ret_flag,ret_val = parseargs(search_arg,nvals,arg_list)
#  Input:
#     search_arg - argument to search for
#     nvals      - number of values associated with this argument.
#                  currently supports nval = 0, 1
#      arg_list  - the list of arguments to parse
#  Output:
#      ret_flag  - 0 => the search_arg is not present in arg_list
#                  1 => search_arg is present in the arg_list
#      ret_val   - None if nvals = 0
#                  Value corresponding to search_arg, if nvals = 1
#      arg_list  - initialial arg_list minus the search_arg and ret_val
#

def parseargs(search_arg,return_nargs,arg_list):
    import string
    import sys
    try:
        index = arg_list.index(search_arg)
    except:
        # Argument not found in list, hence return flag = 0,return val = None
        return 0,None

    if return_nargs == 0:
        arg_list.remove(search_arg)
        return 1,None

    if index+1 == len(arg_list):
        print 'Error! Option has no value!'
        print 'Expecting value with option: ' + search_arg
        sys.exit()
    else:
        ret_arg = arg_list[index+1]
        arg_list.remove(search_arg)
        arg_list.remove(ret_arg)
        return 1,ret_arg