Defaults with Python Cmd interpreter

57 Views Asked by At

I'm working with an existing Apache Open Source python code base (Cassandra CQLSH) using the cmd.Cmd class for an interpreter shell. Like many Shells, you can configure defaults in a dot-rc file (.cqlshrc), pass them as arguments on the command line to the shell, or, use default in a __init__ constructor.

The problem is each phase attempts to set the defaults with global constants. This means each level needs to both know the defaults and once set, bypass the constructor (init). It has become a Big Ball of Mud. Separation of responsibilities is not straightforward.

I'm looking for a better Design Pattern here. Since Cmd is a core Python library, someone must have implemented it well with cmd line args and .rc defaults. Example:

1. Global constant
DEFAULT_REQUEST_TIMEOUT_SECONDS = 10

2. Cmd line arg
parser.add_argument("--connect-timeout", default=DEFAULT_CONNECT_TIMEOUT_SECONDS, dest='connect_timeout',
                    help='Specify the connection timeout in seconds (default: %(default)s seconds).')

3. read config file (.cqlshrc)
argvalues.connect_timeout = option_with_default(configs.getint, 'connection', 'timeout', DEFAULT_CONNECT_TIMEOUT_SECONDS)

4. Constructor default
def __init__(self, ... connect_timeout=DEFAULT_CONNECT_TIMEOUT_SECONDS, ...):

.

Collaborators on CASSANDRA-18914 welcome.

0

There are 0 best solutions below