From 228309c8de79f878a4c5db64d8908ea54360045c Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 10 Jan 2017 12:37:29 -0800 Subject: __main__: split parser into a helper function This simplifies the main function by pulling out the argument parsing and validating into a separate function. --- alot/__main__.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'alot/__main__.py') diff --git a/alot/__main__.py b/alot/__main__.py index 3bb71c47..637097e9 100644 --- a/alot/__main__.py +++ b/alot/__main__.py @@ -17,10 +17,11 @@ from alot.commands import * from alot.commands import CommandParseError, COMMANDS -def main(): - """The main entry point to alot. It parses the command line and prepares - for the user interface main loop to run.""" - # set up the parser to parse the command line options. +_SUBCOMMANDS = ['search', 'compose', 'bufferlist', 'taglist', 'pyshell'] + + +def parser(): + """Parse command line arguments, validate them, and return them.""" parser = argparse.ArgumentParser() parser.add_argument('-v', '--version', action='version', version=alot.__version__) @@ -46,24 +47,32 @@ def main(): help='logfile [default: %(default)s]') # We will handle the subcommands in a seperate run of argparse as argparse # does not support optional subcommands until now. - subcommands = ('search', 'compose', 'bufferlist', 'taglist', 'pyshell') parser.add_argument('command', nargs=argparse.REMAINDER, help='possible subcommands are {}'.format( - ', '.join(subcommands))) + ', '.join(_SUBCOMMANDS))) options = parser.parse_args() + if options.command: # We have a command after the initial options so we also parse that. # But we just use the parser that is already defined for the internal # command that will back this subcommand. parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='subcommand') - for subcommand in subcommands: + for subcommand in _SUBCOMMANDS: subparsers.add_parser(subcommand, parents=[COMMANDS['global'][subcommand][1]]) command = parser.parse_args(options.command) else: command = None + return options, command + + +def main(): + """The main entry point to alot. It parses the command line and prepares + for the user interface main loop to run.""" + options, command = parser() + # logging root_logger = logging.getLogger() for log_handler in root_logger.handlers: @@ -105,7 +114,7 @@ def main(): cmdstring = settings.get('initial_command') except CommandParseError as err: sys.exit(err) - elif command.subcommand in subcommands: + elif command.subcommand in _SUBCOMMANDS: cmdstring = ' '.join(options.command) # set up and start interface -- cgit v1.2.3