Project

General

Profile

1
# Program options processing
2

    
3
import os
4

    
5
import util
6

    
7
def env_flag(name, default=False, env_names_ref=None):
8
    if env_names_ref != None: env_names_ref.append(name+'=1')
9
    return bool(os.getenv(name, default))
10

    
11
def get_env_var(name, default=None, env_names_ref=None):
12
    if env_names_ref != None: env_names_ref.append(name+'=...')
13
    return os.getenv(name, default)
14

    
15
def get_env_vars(names, prefix=None, env_names_ref=None, on_unset=util.noop):
16
    vars_ = {}
17
    for name in names:
18
        env_name = name
19
        if prefix != None: env_name = prefix+'_'+env_name
20
        if env_names_ref != None: env_names_ref.append(env_name+'=...')
21
        try: vars_[name] = os.environ[env_name]
22
        except KeyError, e: on_unset(e)
23
    return vars_
24

    
25
def env_usage(env_names, optional=False):
26
    str_ = 'env'
27
    for name in env_names:
28
        str_ += ' '
29
        if optional: str_ += '['
30
        str_ += name
31
        if optional: str_ += ']'
32
    return str_
(25-25/44)