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
#!/usr/bin/env python
import re

RE_UNSAFE = re.compile(r'[^A-Z0-9_]')
RE_DEDUPE = re.compile(r'_+')

def make_env_var_name(*args):
    """
    Accept any number of string arguments. Return an ENV var name string.

    >>> make_env_var_name('cron-dbsnap', 'test-db')
    'CRON_DBSNAP_TEST_DB'

    >>> make_env_var_name('cron-dbsnap', 'test-db', 'to', 'us-west-1')
    'CRON_DBSNAP_TEST_DB_TO_US_WEST_1'

    >>> make_env_var_name('https://www.remarkbox.com/#beta')
    'HTTPS_WWW_REMARKBOX_COM_BETA'

    >>> make_env_var_name('the space', 'env var')
    'THE_SPACE_ENV_VAR'
    """
    # TODO: if len(args) == 0: exception?
    subject = '_'.join(args).upper()
    return RE_DEDUPE.sub('_', RE_UNSAFE.sub('_', subject))

if __name__ == '__main__':
    from sys import argv
    if len(argv) < 2:
        print("usage: ./env_namer <arg> [<arg>, ...]")
    else:
        print(make_env_var_name(*argv[1:]))