Added [-v|--verbose] and --version options to the command line.

This commit is contained in:
Fabio Manganiello 2022-12-11 10:54:03 +01:00
parent cf9d34d38e
commit a1d3724b8d
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -61,6 +61,7 @@ class Daemon:
no_capture_stdout=False, no_capture_stdout=False,
no_capture_stderr=False, no_capture_stderr=False,
redis_queue=None, redis_queue=None,
verbose=False,
): ):
""" """
Constructor Constructor
@ -76,6 +77,7 @@ class Daemon:
no_capture_stderr -- Set to true if you want to disable the stderr no_capture_stderr -- Set to true if you want to disable the stderr
capture by the logging system capture by the logging system
redis_queue -- Name of the (Redis) queue used for dispatching messages (default: platypush/bus). redis_queue -- Name of the (Redis) queue used for dispatching messages (default: platypush/bus).
verbose -- Enable debug/verbose logging, overriding the stored configuration (default: False).
""" """
if pidfile: if pidfile:
@ -86,7 +88,10 @@ class Daemon:
self.redis_queue = redis_queue or self._default_redis_queue self.redis_queue = redis_queue or self._default_redis_queue
self.config_file = config_file self.config_file = config_file
Config.init(self.config_file) Config.init(self.config_file)
logging.basicConfig(**(Config.get('logging') or {})) logging_conf = Config.get('logging') or {}
if verbose:
logging_conf['level'] = logging.DEBUG
logging.basicConfig(**logging_conf)
redis_conf = Config.get('backend.redis') or {} redis_conf = Config.get('backend.redis') or {}
self.bus = RedisBus( self.bus = RedisBus(
@ -119,6 +124,21 @@ class Daemon:
default=None, default=None,
help=cls.config_file.__doc__, help=cls.config_file.__doc__,
) )
parser.add_argument(
'--version',
dest='version',
required=False,
action='store_true',
help="Print the current version and exit",
)
parser.add_argument(
'--verbose',
'-v',
dest='verbose',
required=False,
action='store_true',
help="Enable verbose/debug logging",
)
parser.add_argument( parser.add_argument(
'--pidfile', '--pidfile',
'-P', '-P',
@ -157,12 +177,18 @@ class Daemon:
) )
opts, args = parser.parse_known_args(args) opts, args = parser.parse_known_args(args)
if opts.version:
print(__version__)
sys.exit(0)
return cls( return cls(
config_file=opts.config, config_file=opts.config,
pidfile=opts.pidfile, pidfile=opts.pidfile,
no_capture_stdout=opts.no_capture_stdout, no_capture_stdout=opts.no_capture_stdout,
no_capture_stderr=opts.no_capture_stderr, no_capture_stderr=opts.no_capture_stderr,
redis_queue=opts.redis_queue, redis_queue=opts.redis_queue,
verbose=opts.verbose,
) )
def on_message(self): def on_message(self):