1#!/usr/bin/env python 2 3"""Constructs command line argument parser for tvnamer 4""" 5 6import sys 7import optparse 8 9 10class Group(object): 11 """Simple helper context manager to add a group to an OptionParser 12 """ 13 14 def __init__(self, parser, name): 15 self.parser = parser 16 self.name = name 17 self.group = optparse.OptionGroup(self.parser, name) 18 19 def __enter__(self): 20 return self.group 21 22 def __exit__(self, *k, **kw): 23 self.parser.add_option_group(self.group) 24 25 26def getCommandlineParser(defaults): 27 parser = optparse.OptionParser(usage = "%prog [options] <files>", add_help_option = False) 28 29 parser.set_defaults(**defaults) 30 31 # Console output 32 with Group(parser, "Console output") as g: 33 g.add_option("-v", "--verbose", action="store_true", dest="verbose", help = "show debugging info") 34 g.add_option("-q", "--not-verbose", action="store_false", dest="verbose", help = "no verbose output (useful to override 'verbose':true in config file)") 35 g.add_option("--dry-run", action="store_true", dest="dry_run", help = "Only tell what script is going to do") 36 37 # Batch options 38 with Group(parser, "Batch options") as g: 39 g.add_option("-a", "--always", action="store_true", dest="always_rename", help = "Always renames files (but prompt for correct series)") 40 g.add_option("--not-always", action="store_true", dest="always_rename", help = "Overrides --always") 41 42 g.add_option("-f", "--selectfirst", action="store_true", dest="select_first", help = "Select first series search result automatically") 43 g.add_option("--not-selectfirst", action="store_false", dest="select_first", help = "Overrides --selectfirst") 44 45 g.add_option("-b", "--batch", action="store_true", dest = "batch", help = "Rename without human intervention, same as --always and --selectfirst combined") 46 g.add_option("--not-batch", action="store_false", dest = "batch", help = "Overrides --batch") 47 48 # Config options 49 with Group(parser, "Config options") as g: 50 g.add_option("-c", "--config", action = "store", dest = "loadconfig", help = "Load config from this file") 51 g.add_option("-s", "--save", action = "store", dest = "saveconfig", help = "Save configuration to this file and exit") 52 g.add_option("-p", "--preview-config", action = "store_true", dest = "showconfig", help = "Show current config values and exit") 53 54 # Override values 55 with Group(parser, "Override values") as g: 56 g.add_option("-n", "--name", action="store", dest = "force_name", help = "override the parsed series name with this (applies to all files)") 57 g.add_option("--series-id", action="store", dest = "series_id", help = "explicitly set the show id for TVdb to use (applies to all files)") 58 g.add_option("--order", action = "store", dest = "order", help = "set the TvDB episode order ('aired' [default] or 'dvd')") 59 g.add_option("-l", "--lang", action = "store", dest = "language", help = "set the language used to retrieve data") 60 61 # Misc 62 with Group(parser, "Misc") as g: 63 g.add_option("-r", "--recursive", action="store_true", dest = "recursive", help = "Descend more than one level directories supplied as arguments") 64 g.add_option("--not-recursive", action="store_false", dest = "recursive", help = "Only descend one level into directories") 65 66 g.add_option("-m", "--move", action="store_true", dest="move_files_enable", help = "Move files to destination specified in config or with --movedestination argument") 67 g.add_option("--not-move", action="store_false", dest="move_files_enable", help = "Files will remain in current directory") 68 69 g.add_option("--force-move", action="store_true", dest = "overwrite_destination_on_move", help = "Force move and potentially overwrite existing files in destination folder") 70 g.add_option("--force-rename", action="store_true", dest = "overwrite_destination_on_rename", help = "Force rename source file") 71 72 g.add_option("-d", "--movedestination", action="store", dest = "move_files_destination", help = "Destination to move files to. Variables: %(seriesname)s %(seasonnumber)d %(episodenumbers)s") 73 74 g.add_option("-h", "--help", action="help", help = "show this help message and exit") 75 g.add_option("--version", action="store_true", dest = "show_version", help = "show verison number and exit") 76 77 78 return parser 79