diff options
Diffstat (limited to 'command.py')
-rw-r--r-- | command.py | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -22,6 +22,7 @@ import sys | |||
22 | from error import NoSuchProjectError | 22 | from error import NoSuchProjectError |
23 | from error import InvalidProjectGroupsError | 23 | from error import InvalidProjectGroupsError |
24 | 24 | ||
25 | |||
25 | class Command(object): | 26 | class Command(object): |
26 | """Base class for any command line action in repo. | 27 | """Base class for any command line action in repo. |
27 | """ | 28 | """ |
@@ -33,6 +34,27 @@ class Command(object): | |||
33 | def WantPager(self, opt): | 34 | def WantPager(self, opt): |
34 | return False | 35 | return False |
35 | 36 | ||
37 | def ReadEnvironmentOptions(self, opts): | ||
38 | """ Set options from environment variables. """ | ||
39 | |||
40 | env_options = self._RegisteredEnvironmentOptions() | ||
41 | |||
42 | for env_key, opt_key in env_options.items(): | ||
43 | # Get the user-set option value if any | ||
44 | opt_value = getattr(opts, opt_key) | ||
45 | |||
46 | # If the value is set, it means the user has passed it as a command | ||
47 | # line option, and we should use that. Otherwise we can try to set it | ||
48 | # with the value from the corresponding environment variable. | ||
49 | if opt_value is not None: | ||
50 | continue | ||
51 | |||
52 | env_value = os.environ.get(env_key) | ||
53 | if env_value is not None: | ||
54 | setattr(opts, opt_key, env_value) | ||
55 | |||
56 | return opts | ||
57 | |||
36 | @property | 58 | @property |
37 | def OptionParser(self): | 59 | def OptionParser(self): |
38 | if self._optparse is None: | 60 | if self._optparse is None: |
@@ -49,6 +71,24 @@ class Command(object): | |||
49 | """Initialize the option parser. | 71 | """Initialize the option parser. |
50 | """ | 72 | """ |
51 | 73 | ||
74 | def _RegisteredEnvironmentOptions(self): | ||
75 | """Get options that can be set from environment variables. | ||
76 | |||
77 | Return a dictionary mapping environment variable name | ||
78 | to option key name that it can override. | ||
79 | |||
80 | Example: {'REPO_MY_OPTION': 'my_option'} | ||
81 | |||
82 | Will allow the option with key value 'my_option' to be set | ||
83 | from the value in the environment variable named 'REPO_MY_OPTION'. | ||
84 | |||
85 | Note: This does not work properly for options that are explicitly | ||
86 | set to None by the user, or options that are defined with a | ||
87 | default value other than None. | ||
88 | |||
89 | """ | ||
90 | return {} | ||
91 | |||
52 | def Usage(self): | 92 | def Usage(self): |
53 | """Display usage and terminate. | 93 | """Display usage and terminate. |
54 | """ | 94 | """ |