From 0847fc91bf470efda7bbb3d5ffe949cb9f5fc58a Mon Sep 17 00:00:00 2001 From: gesh Date: Sun, 19 Dec 2021 23:52:23 +0200 Subject: [PATCH] Make doc-getting flags shortcut This saves on pointless validation/setup work when only getting help information. Moreover, these were the only actions that were in the middle of the parse/validate/setup/run core logic of main(), moving them out clears the way to cleanly breaking it up. Removes -v alias for --version. If this is undesirable, a trick similar to that for --sites-list can be used to shortcut it as well. --- fanficfare/cli.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/fanficfare/cli.py b/fanficfare/cli.py index 4329966a..76513e93 100644 --- a/fanficfare/cli.py +++ b/fanficfare/cli.py @@ -67,7 +67,8 @@ def write_story(config, adapter, writeformat, def mkParser(calibre, parser=None): # read in args, anything starting with -- will be treated as --= if not parser: - parser = OptionParser('usage: %prog [options] [STORYURL]...') + parser = OptionParser('usage: %prog [options] [STORYURL]...', + version="Version: %s" % version) parser.add_option('-f', '--format', dest='format', default='epub', help='Write story as FORMAT, epub(default), mobi, txt or html.', metavar='FORMAT') if calibre: @@ -141,8 +142,15 @@ def mkParser(calibre, parser=None): action='store_true', dest='downloadimap', help='Download valid story URLs from unread email from IMAP account configured in ini. Update existing EPUBs if used with --update-epub.', ) + def sitesList(*args): + for site, examples in adapters.getSiteExamples(): + print('\n#### %s\nExample URLs:' % site) + for u in examples: + print(' * %s' % u) + sys.exit() + parser.add_option('-s', '--sites-list', - action='store_true', dest='siteslist', default=False, + action='callback', callback=sitesList, help='Get list of valid story URLs examples.', ) parser.add_option('--non-interactive', action='store_false', dest='interactive', default=sys.stdin.isatty() and sys.stdout.isatty(), @@ -156,9 +164,6 @@ def mkParser(calibre, parser=None): parser.add_option('--color', action='store_true', dest='color', help='Display a errors and warnings in a contrasting color. Requires package colorama on Windows.', ) - parser.add_option('-v', '--version', - action='store_true', dest='version', - help='Display version and quit.', ) ## undocumented feature for development use. Save page cache and ## cookies between runs. Saves in PWD as files global_cache and @@ -194,10 +199,6 @@ def main(argv=None, logger.debug("Python Version:%s"%sys.version) logger.debug(" FFF Version:%s"%version) - if options.version: - print("Version: %s" % version) - sys.exit() - if options.color: if 'Windows' in platform.platform(): try: @@ -219,7 +220,6 @@ def main(argv=None, warn = fail = print list_only = any((options.imaplist, - options.siteslist, options.list, options.normalize, )) @@ -228,13 +228,6 @@ def main(argv=None, options.downloadlist))): parser.error('Incorrect arguments: Cannot download and list URLs at the same time.') - if options.siteslist: - for site, examples in adapters.getSiteExamples(): - print('\n#### %s\nExample URLs:' % site) - for u in examples: - print(' * %s' % u) - sys.exit() - # options.updatealways should also invoke most options.update logic. if options.updatealways: options.update = True