X-Git-Url: http://git.kaliko.me/?a=blobdiff_plain;f=sima%2Futils%2Fstartopt.py;h=cdcfcecc3cdce09314b2a7adcfc98e31a45cbf5c;hb=faf4a4dcf4d8772fd64209c507d5959478e053f5;hp=fa69d8cd357e737e011ca8c7da85c9fc921dd777;hpb=8495a82088dba0b58662338b8ddef870894c9e34;p=mpd-sima.git diff --git a/sima/utils/startopt.py b/sima/utils/startopt.py index fa69d8c..cdcfcec 100644 --- a/sima/utils/startopt.py +++ b/sima/utils/startopt.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2009, 2010, 2011, 2012, 2013 Jack Kaliko +# Copyright (c) 2009-2015, 2021 kaliko # # This file is part of sima # @@ -19,17 +19,18 @@ # # -import sys -from argparse import (ArgumentParser, SUPPRESS) +from argparse import ArgumentParser, RawDescriptionHelpFormatter, SUPPRESS -from .utils import Obsolete, Wfile, Rfile, Wdir +from .utils import Wfile, Rfile, Wdir -USAGE = """USAGE: %prog [--help] [options]""" DESCRIPTION = """ -sima automagicaly queue new tracks in MPD playlist. -All command line options will override their equivalent in configuration -file. +MPD_sima automagicaly queue new tracks in MPD playlist. + +Command line options override their equivalent in configuration file. +If a positional arguments is provided MPD_sima execute the command and returns. +Commands available: +{} """ @@ -39,7 +40,11 @@ def clean_dict(to_clean): if not to_clean.get(k): to_clean.pop(k) - +# COMMANDS LIST +CMDS = {'config-test': 'Test configuration (MPD connection and Tags plugin only)', + 'create-db': 'Create the database', + 'generate-config': 'Generate a configuration file to stdout', + } # OPTIONS LIST # pop out 'sw' value before creating Parser object. # PAY ATTENTION: @@ -50,71 +55,73 @@ def clean_dict(to_clean): # name it is meant to override. OPTS = [ { - 'sw':['-l', '--log'], + 'sw': ['-l', '--log'], 'type': str, 'dest': 'logfile', 'action': Wfile, + 'metavar': 'LOG', 'help': 'file to log message to, default is stdout/stderr'}, { - 'sw':['-v', '--log-level'], + 'sw': ['-v', '--log-level'], 'type': str, 'dest': 'verbosity', 'choices': ['debug', 'info', 'warning', 'error'], - 'help': 'file to log message to, default is stdout/stderr'}, + 'help': 'log messages verbosity, default is info'}, { 'sw': ['-p', '--pid'], 'dest': 'pidfile', 'action': Wfile, + 'metavar': 'FILE', 'help': 'file to save PID to, default is not to store pid'}, { 'sw': ['-d', '--daemon'], 'dest': 'daemon', 'action': 'store_true', - 'help': 'Daemonize process.'}, + 'help': 'daemonize process'}, { 'sw': ['-S', '--host'], 'dest': 'host', - 'help': 'Host MPD in running on (IP or FQDN)'}, + 'help': 'host MPD in running on (IP or FQDN)'}, { 'sw': ['-P', '--port'], 'type': int, 'dest': 'port', - 'help': 'Port MPD in listening on'}, + 'help': 'port MPD in listening on'}, { - 'sw':['-c', '--config'], + 'sw': ['-c', '--config'], 'dest': 'conf_file', 'action': Rfile, - 'help': 'Configuration file to load'}, + 'metavar': 'CONFIG', + 'help': 'configuration file to load'}, + { # TODO: To remove eventually in next major realese v0.18 + 'sw': ['--generate-config'], + 'dest': 'generate_config', + 'action': 'store_true', + 'help': SUPPRESS}, { - 'sw':['--var_dir'], + 'sw': ['--var-dir', '--var_dir'], 'dest': 'var_dir', 'action': Wdir, - 'help': 'Directory to store var content (ie. database, cache)'}, - { + 'help': 'directory to store var content (ie. database, cache)'}, + { # TODO: To remove eventually in next major realese v0.18 'sw': ['--create-db'], 'action': 'store_true', 'dest': 'create_db', - 'help': '''Create database and exit, use destination - specified in --var_dir or standard location.'''}, - { - 'sw':['--queue-mode', '-q'], - 'dest': 'queue_mode', - 'choices': ['track', 'top', 'album'], - #'help': 'Queue mode in [track, top, album]', - 'help': SUPPRESS, }, - { - 'sw':['--purge_history'], - 'action': 'store_true', - 'dest': 'do_purge_history', 'help': SUPPRESS}, + { + 'sw': ['command'], + 'nargs': '?', + 'choices': CMDS.keys(), + 'help': 'command to run (cf. description or unix manual for more)'}, ] -class StartOpt(object): +class StartOpt: """Command line management. """ def __init__(self, script_info,): + self.parser = None self.info = dict(script_info) self.options = dict() self.main() @@ -123,13 +130,14 @@ class StartOpt(object): """ Declare options in ArgumentParser object. """ - self.parser = ArgumentParser(description=DESCRIPTION, - usage='%(prog)s [options]', - prog=self.info.get('prog') - epilog='Happy Listening', - ) + cmds = '\n'.join([f' * {k}: {v}' for k, v in CMDS.items()]) + self.parser = ArgumentParser(description=DESCRIPTION.format(cmds), + prog=self.info.get('prog'), + epilog='Happy Listening', + formatter_class=RawDescriptionHelpFormatter, + ) self.parser.add_argument('--version', action='version', - version='%(prog)s {version}'.format(**self.info)) + version='%(prog)s {version}'.format(**self.info)) # Add all options declare in OPTS for opt in OPTS: opt_names = opt.pop('sw')