mirror of
https://github.com/coreos/coreos-assembler.git
synced 2026-02-06 03:45:08 +01:00
cosalib/cli.py: Cli/BuildCli class for reusability
Cli:
- Base class which can be used by other Cli classes
- Sets up the logger
- Provides an environment variable driven argument method
- Appends env variables to env args help text
BuildCli:
- Provides common arguments for build commands
Code:
from cosalib.cli import BuildCli
b = BuildCli(usage='test')
b.add_argument('-t', '--test', help='test')
b.add_argument('-e', '--env-test', env_var='ENV_VAR', default='default')
print(b.parse_args())
b.print_help()
Output:
usage: test
optional arguments:
-h, --help show this help message and exit
--log-level {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}
Set the log level (Env: COSA_LOG_LEVEL)
--build BUILD Override build id, defaults to latest
--buildroot BUILDROOT
Build diretory
--dump Dump the manfiest and exit
-t TEST, --test TEST test
-e ENV_TEST, --env-test ENV_TEST
(Env: ENV_VAR)
Signed-off-by: Steve Milner <smilner@redhat.com>
This commit is contained in:
committed by
Colin Walters
parent
5916b1f73e
commit
ea00b56f33
93
src/cosalib/cli.py
Normal file
93
src/cosalib/cli.py
Normal file
@@ -0,0 +1,93 @@
|
||||
# NOTE: PYTHONUNBUFFERED is set in cmdlib.sh for unbuffered output
|
||||
# pylint: disable=C0103
|
||||
|
||||
import argparse
|
||||
import logging as log
|
||||
import os
|
||||
|
||||
|
||||
class Cli(argparse.ArgumentParser):
|
||||
"""
|
||||
Abstraction for executing commands from the cli.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""
|
||||
Initializes the Cli instance.
|
||||
|
||||
:param kwargs: All keyword arguments which will pass to ArgumentParser
|
||||
:type kwargs: dict
|
||||
"""
|
||||
argparse.ArgumentParser.__init__(self, *args, **kwargs)
|
||||
self.add_argument(
|
||||
'--log-level', env_var='COSA_LOG_LEVEL', default='info',
|
||||
choices=log._nameToLevel.keys(), help='Set the log level')
|
||||
|
||||
def add_argument(self, *args, **kwargs):
|
||||
"""
|
||||
Overloads the add_argument to be able to also read from
|
||||
the environment. To read from the environment provide
|
||||
the keyword arugment env_var.
|
||||
|
||||
:param args: Non keyword arguments to pass to add_argument
|
||||
:type args: list
|
||||
:param kwargs: Keyword arguments to pass to add_argument
|
||||
:type kwargs: dict
|
||||
"""
|
||||
env_var = kwargs.pop('env_var', None)
|
||||
if env_var is not None:
|
||||
if kwargs.get('help') is None:
|
||||
kwargs['help'] = ''
|
||||
kwargs['help'] = kwargs['help'] + ' (Env: {})'.format(env_var)
|
||||
default = kwargs.pop('default', None)
|
||||
super().add_argument(
|
||||
*args, default=os.environ.get(env_var, default), **kwargs)
|
||||
else:
|
||||
super().add_argument(*args, **kwargs)
|
||||
|
||||
def parse_args(self):
|
||||
"""
|
||||
Parses the arguments passed in, verifies inputs, sets the logger,
|
||||
and returns the arguments.
|
||||
|
||||
:returns: The parsed arguments
|
||||
:rtype: argparse.Namepsace
|
||||
"""
|
||||
args = super().parse_args()
|
||||
self._set_logger(args.log_level)
|
||||
return args
|
||||
|
||||
def _set_logger(self, level):
|
||||
"""
|
||||
Set the log level
|
||||
|
||||
:param level: set the log level
|
||||
:type level: str
|
||||
"""
|
||||
log.basicConfig(
|
||||
format='[%(asctime)s %(levelname)s]: %(message)s',
|
||||
level=log._nameToLevel.get(level.upper(), log.DEBUG))
|
||||
|
||||
|
||||
class BuildCli(Cli):
|
||||
"""
|
||||
Cli class that adds in reusable build specific arguments.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""
|
||||
Initializes the BuildCli instance.
|
||||
|
||||
:param kwargs: All keyword arguments which will pass to ArgumentParser
|
||||
:type kwargs: dict
|
||||
"""
|
||||
Cli.__init__(self, *args, **kwargs)
|
||||
# Set common arguments
|
||||
self.add_argument(
|
||||
'--build', default='latest',
|
||||
help='Override build id, defaults to latest')
|
||||
self.add_argument(
|
||||
'--buildroot', default='builds', help='Build diretory')
|
||||
self.add_argument(
|
||||
'--dump', default=False, action='store_true',
|
||||
help='Dump the manfiest and exit')
|
||||
Reference in New Issue
Block a user