Project

General

Profile

1
# Program options processing
2

    
3
import os
4

    
5
def env_flag(name): return name in os.environ and os.environ[name] != ''
6

    
7
def get_env_vars(names, prefix=None, env_names_ref=None):
8
    # Requires that all env vars are set; otherwise returns None
9
    has_all = True
10
    vars_ = {}
11
    for name in names:
12
        env_name = name
13
        if prefix != None: env_name = prefix+'_'+env_name
14
        if env_names_ref != None: env_names_ref.append(env_name)
15
        if env_name in os.environ: vars_[name] = os.environ[env_name]
16
        else: has_all = False
17
    if has_all: return vars_
18
    else: return None
19

    
20
def env_usage(env_names, optional=False):
21
    str_ = 'env'
22
    for name in env_names:
23
        str_ += ' '
24
        if optional: str_ += '['
25
        str_ += name+'=...'
26
        if optional: str_ += ']'
27
    return str_
(4-4/9)