X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sima%2Futils%2Fstartopt.py;h=bf9fcf4120ce95d1e227036d7388c42fddbe5bb1;hb=3636f8f37183e1b1f1c581b4edfff1b1abd70462;hp=51319f60afc7360b3a5d64d8993ec6e29c1cdeee;hpb=1481a05d7060c8cfd615850b3df9d66a52fb0ff4;p=mpd-sima.git diff --git a/sima/utils/startopt.py b/sima/utils/startopt.py index 51319f6..bf9fcf4 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, 2014, 2015 Jack Kaliko +# Copyright (c) 2009-2015, 2021 kaliko # # This file is part of sima # @@ -19,14 +19,16 @@ # # -from argparse import (ArgumentParser, SUPPRESS) - +from argparse import ArgumentParser, RawDescriptionHelpFormatter, SUPPRESS from .utils import Wfile, Rfile, Wdir DESCRIPTION = """ MPD_sima automagicaly queue new tracks in MPD playlist. -Command line options override their equivalent in configuration file.""" + +Command line options override their equivalent in configuration file. +If a positional arguments is provided MPD_sima execute the command and returns. +""" def clean_dict(to_clean): @@ -35,7 +37,6 @@ def clean_dict(to_clean): if not to_clean.get(k): to_clean.pop(k) - # OPTIONS LIST # pop out 'sw' value before creating Parser object. # PAY ATTENTION: @@ -46,67 +47,77 @@ 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': 'Log messages verbosity, default is info'}, + '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'}, { - 'sw':['--var_dir'], + 'sw': ['--var-dir', '--var_dir'], 'dest': 'var_dir', 'action': Wdir, - 'help': 'Directory to store var content (ie. database, cache)'}, - { - '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}, + 'help': 'directory to store var content (ie. database, cache)'}, +] +# Commands +CMDS = [ + {'config-test': [{}], 'help': 'Test configuration (MPD connection and Tags plugin only)'}, + {'create-db': [{}], 'help': 'Create the database'}, + {'generate-config': [{}], 'help': 'Generate a configuration file to stdout'}, + {'purge-history': [{}], 'help': 'Remove play history'}, + {'bl-view': [{}], 'help': 'List blocklist IDs'}, + {'bl-add-artist': [ + {'name': 'artist', 'type': str, 'nargs': '?', + 'help': 'If artist is provided use it else use currently playing value'} + ], 'help': 'Add artist to the blocklist'}, + {'bl-add-album': [ + {'name': 'album', 'type': str, 'nargs': '?', + 'help': 'If album is provided use it else use currently playing value'} + ], 'help': 'Add album to the blocklist'}, + {'bl-add-track': [ + {'name': 'track', 'type': str, 'nargs': '?', + 'help': 'If track is provided use it else use currently playing value'} + ], 'help': 'Add track to the blocklist'}, + {'bl-delete': [ + {'name': 'id', 'type': int, 'nargs': '?', + 'help': 'blocklist ID to suppress (use bl-view list IDs)'} + ], 'help': 'Remove entries from the blocklist'}, ] -class StartOpt(object): +class StartOpt: """Command line management. """ @@ -123,13 +134,27 @@ class StartOpt(object): self.parser = ArgumentParser(description=DESCRIPTION, 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)) # Add all options declare in OPTS for opt in OPTS: opt_names = opt.pop('sw') self.parser.add_argument(*opt_names, **opt) + # Add sub commands + sp = self.parser.add_subparsers( + title=f'{self.info["prog"]} commands as positional arguments', + description=f"""Use them after optionnal arguments.\n"{self.info["prog"]} command -h" for more info.""", + metavar='', dest='command') + for cmd in CMDS: + helpmsg = cmd.pop('help') + cmd, args = cmd.popitem() + _ = sp.add_parser(cmd, description=helpmsg, help=helpmsg) + for arg in args: + name = arg.pop('name', None) + if name: + _.add_argument(name, **arg) def main(self): """