Project

General

Profile

1 65 aaronmk
# Program options processing
2
3
import os
4
5 932 aaronmk
import util
6
7 945 aaronmk
def env_flag(name, default=False, env_names_ref=None):
8 943 aaronmk
    if env_names_ref != None: env_names_ref.append(name+'=1')
9 945 aaronmk
    return bool(os.getenv(name, default))
10 65 aaronmk
11 1672 aaronmk
def get_env_var(name, default=None, env_names_ref=None):
12 943 aaronmk
    if env_names_ref != None: env_names_ref.append(name+'=...')
13 135 aaronmk
    return os.getenv(name, default)
14
15 932 aaronmk
def get_env_vars(names, prefix=None, env_names_ref=None, on_unset=util.noop):
16 65 aaronmk
    vars_ = {}
17
    for name in names:
18
        env_name = name
19
        if prefix != None: env_name = prefix+'_'+env_name
20 943 aaronmk
        if env_names_ref != None: env_names_ref.append(env_name+'=...')
21 130 aaronmk
        try: vars_[name] = os.environ[env_name]
22 932 aaronmk
        except KeyError, e: on_unset(e)
23 130 aaronmk
    return vars_
24 67 aaronmk
25
def env_usage(env_names, optional=False):
26
    str_ = 'env'
27
    for name in env_names:
28
        str_ += ' '
29
        if optional: str_ += '['
30 943 aaronmk
        str_ += name
31 67 aaronmk
        if optional: str_ += ']'
32
    return str_