def parser():
    """Parse command line arguments, validate them, and return them."""
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument('-r', '--read-only', action='store_true',
                        help='open notmuch database in read-only mode')
    parser.add_argument('-c', '--config', metavar='FILENAME',
                        action=cargparse.ValidatedStoreAction,
                        validator=cargparse.require_file,
                        help='configuration file')
    parser.add_argument('-n', '--notmuch-config', metavar='FILENAME',
                        default=os.environ.get(
                            'NOTMUCH_CONFIG',
                            os.path.expanduser('~/.notmuch-config')),
                        action=cargparse.ValidatedStoreAction,
                        validator=cargparse.require_file,
                        help='notmuch configuration file')
    parser.add_argument('-C', '--colour-mode', metavar='COLOURS',
                        choices=(1, 16, 256), type=int,
                        help='number of colours to use')
    parser.add_argument('-p', '--mailindex-path', metavar='PATH',
                        action=cargparse.ValidatedStoreAction,
                        validator=cargparse.require_dir,
                        help='path to notmuch index')
    parser.add_argument('-d', '--debug-level', metavar='LEVEL', default='info',
                        choices=('debug', 'info', 'warning', 'error'),
                        help='debug level [default: %(default)s]')
    parser.add_argument('-l', '--logfile', metavar='FILENAME',
                        default='/dev/null',
                        action=cargparse.ValidatedStoreAction,
                        validator=cargparse.optional_file_like,
                        help='log file [default: %(default)s]')
    parser.add_argument('-h', '--help', action='help',
                        help='display this help and exit')
    parser.add_argument('-v', '--version', action='version',
                        version=alot.__version__,
                        help='output version information and exit')
    # We will handle the subcommands in a separate run of argparse as argparse
    # does not support optional subcommands until now.
    parser.add_argument('command', nargs=argparse.REMAINDER,
                        help='possible subcommands are {}'.format(
                            ', '.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:
            subparsers.add_parser(subcommand,
                                  parents=[COMMANDS['global'][subcommand][1]])
        command = parser.parse_args(options.command)
    else:
        command = None

    return options, command
def parser ( ) : parser = argparse . ArgumentParser ( add_help = False ) parser . add_argument ( '-r' , '--read-only' , action = 'store_true' , help = 'open notmuch database in read-only mode' ) parser . add_argument ( '-c' , '--config' , metavar = 'FILENAME' , action = cargparse . ValidatedStoreAction , validator = cargparse . require_file , help = 'configuration file' ) parser . add_argument ( '-n' , '--notmuch-config' , metavar = 'FILENAME' , default = os . environ . get ( 'NOTMUCH_CONFIG' , os . path . expanduser ( '~/.notmuch-config' ) ) , action = cargparse . ValidatedStoreAction , validator = cargparse . require_file , help = 'notmuch configuration file' ) parser . add_argument ( '-C' , '--colour-mode' , metavar = 'COLOURS' , choices = ( 1 , 16 , 256 ) , type = int , help = 'number of colours to use' ) parser . add_argument ( '-p' , '--mailindex-path' , metavar = 'PATH' , action = cargparse . ValidatedStoreAction , validator = cargparse . require_dir , help = 'path to notmuch index' ) parser . add_argument ( '-d' , '--debug-level' , metavar = 'LEVEL' , default = 'info' , choices = ( 'debug' , 'info' , 'warning' , 'error' ) , help = 'debug level [default: %(default)s]' ) parser . add_argument ( '-l' , '--logfile' , metavar = 'FILENAME' , default = '/dev/null' , action = cargparse . ValidatedStoreAction , validator = cargparse . optional_file_like , help = 'log file [default: %(default)s]' ) parser . add_argument ( '-h' , '--help' , action = 'help' , help = 'display this help and exit' ) parser . add_argument ( '-v' , '--version' , action = 'version' , version = alot . __version__ , help = 'output version information and exit' ) parser . add_argument ( 'command' , nargs = argparse . REMAINDER , help = 'possible subcommands are {}' . format ( ', ' . join ( _SUBCOMMANDS ) ) ) options = parser . parse_args ( ) if options . command : parser = argparse . ArgumentParser ( ) subparsers = parser . add_subparsers ( dest = 'subcommand' ) 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
Parse command line arguments, validate them, and return them.