mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
Allow common CLI arguments to work both before and after the subcommand
This commit is contained in:
parent
59d8a790d1
commit
dd6630368d
1 changed files with 35 additions and 13 deletions
|
@ -30,44 +30,58 @@ def setuptools_entry():
|
||||||
def create_parser():
|
def create_parser():
|
||||||
"""Build and return an ArgumentParser.
|
"""Build and return an ArgumentParser.
|
||||||
"""
|
"""
|
||||||
parser = argparse.ArgumentParser(prog='pokedex', description=u'A command-line Pokédex interface')
|
# Slightly clumsy workaround to make both `setup -v` and `-v setup` work
|
||||||
parser.add_argument(
|
common_parser = argparse.ArgumentParser(add_help=False)
|
||||||
|
common_parser.add_argument(
|
||||||
'-e', '--engine', dest='engine_uri', default=None,
|
'-e', '--engine', dest='engine_uri', default=None,
|
||||||
help=u'By default, all commands try to use a SQLite database '
|
help=u'By default, all commands try to use a SQLite database '
|
||||||
u'in the pokedex install directory. Use this option (or '
|
u'in the pokedex install directory. Use this option (or '
|
||||||
u'a POKEDEX_DB_ENGINE environment variable) to specify an '
|
u'a POKEDEX_DB_ENGINE environment variable) to specify an '
|
||||||
u'alternate database.',
|
u'alternate database.',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
common_parser.add_argument(
|
||||||
'-i', '--index', dest='index_dir', default=None,
|
'-i', '--index', dest='index_dir', default=None,
|
||||||
help=u'By default, all commands try to put the lookup index in '
|
help=u'By default, all commands try to put the lookup index in '
|
||||||
u'the pokedex install directory. Use this option (or a '
|
u'the pokedex install directory. Use this option (or a '
|
||||||
u'POKEDEX_INDEX_DIR environment variable) to specify an '
|
u'POKEDEX_INDEX_DIR environment variable) to specify an '
|
||||||
u'alternate loction.',
|
u'alternate loction.',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
common_parser.add_argument(
|
||||||
'-q', '--quiet', dest='verbose', action='store_false',
|
'-q', '--quiet', dest='verbose', action='store_false',
|
||||||
help=u'Don\'t print system output. This is the default for '
|
help=u'Don\'t print system output. This is the default for '
|
||||||
'non-system commands and setup.',
|
'non-system commands and setup.',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
common_parser.add_argument(
|
||||||
'-v', '--verbose', dest='verbose', default=False, action='store_true',
|
'-v', '--verbose', dest='verbose', default=False, action='store_true',
|
||||||
help=u'Print system output. This is the default for system '
|
help=u'Print system output. This is the default for system '
|
||||||
u'commands, except setup.',
|
u'commands, except setup.',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
prog='pokedex', description=u'A command-line Pokédex interface',
|
||||||
|
parents=[common_parser],
|
||||||
|
)
|
||||||
|
|
||||||
cmds = parser.add_subparsers(title='Commands')
|
cmds = parser.add_subparsers(title='Commands')
|
||||||
cmd_help = cmds.add_parser('help', help=u'Display this message')
|
cmd_help = cmds.add_parser(
|
||||||
|
'help', help=u'Display this message',
|
||||||
|
parents=[common_parser])
|
||||||
cmd_help.set_defaults(func=command_help)
|
cmd_help.set_defaults(func=command_help)
|
||||||
|
|
||||||
cmd_lookup = cmds.add_parser('lookup', help=u'Look up something in the Pokédex')
|
cmd_lookup = cmds.add_parser(
|
||||||
|
'lookup', help=u'Look up something in the Pokédex',
|
||||||
|
parents=[common_parser])
|
||||||
cmd_lookup.set_defaults(func=command_lookup)
|
cmd_lookup.set_defaults(func=command_lookup)
|
||||||
cmd_lookup.add_argument('criteria', nargs='+')
|
cmd_lookup.add_argument('criteria', nargs='+')
|
||||||
|
|
||||||
cmd_search = cmds.add_parser('search', help=u'Find things by various criteria')
|
cmd_search = cmds.add_parser(
|
||||||
|
'search', help=u'Find things by various criteria',
|
||||||
|
parents=[common_parser])
|
||||||
pokedex.cli.search.configure_parser(cmd_search)
|
pokedex.cli.search.configure_parser(cmd_search)
|
||||||
|
|
||||||
cmd_load = cmds.add_parser('load', help=u'Load Pokédex data into a database from CSV files')
|
cmd_load = cmds.add_parser(
|
||||||
|
'load', help=u'Load Pokédex data into a database from CSV files',
|
||||||
|
parents=[common_parser])
|
||||||
cmd_load.set_defaults(func=command_load, verbose=True)
|
cmd_load.set_defaults(func=command_load, verbose=True)
|
||||||
# TODO get the actual default here
|
# TODO get the actual default here
|
||||||
cmd_load.add_argument(
|
cmd_load.add_argument(
|
||||||
|
@ -90,7 +104,9 @@ def create_parser():
|
||||||
'tables', nargs='*',
|
'tables', nargs='*',
|
||||||
help="list of database tables to load (default: all)")
|
help="list of database tables to load (default: all)")
|
||||||
|
|
||||||
cmd_dump = cmds.add_parser('dump', help=u'Dump Pokédex data from a database into CSV files')
|
cmd_dump = cmds.add_parser(
|
||||||
|
'dump', help=u'Dump Pokédex data from a database into CSV files',
|
||||||
|
parents=[common_parser])
|
||||||
cmd_dump.set_defaults(func=command_dump, verbose=True)
|
cmd_dump.set_defaults(func=command_dump, verbose=True)
|
||||||
cmd_dump.add_argument(
|
cmd_dump.add_argument(
|
||||||
'-d', '--directory', dest='directory', default=None,
|
'-d', '--directory', dest='directory', default=None,
|
||||||
|
@ -102,13 +118,19 @@ def create_parser():
|
||||||
'tables', nargs='*',
|
'tables', nargs='*',
|
||||||
help="list of database tables to load (default: all)")
|
help="list of database tables to load (default: all)")
|
||||||
|
|
||||||
cmd_reindex = cmds.add_parser('reindex', help=u'Rebuild the lookup index from the database')
|
cmd_reindex = cmds.add_parser(
|
||||||
|
'reindex', help=u'Rebuild the lookup index from the database',
|
||||||
|
parents=[common_parser])
|
||||||
cmd_reindex.set_defaults(func=command_reindex, verbose=True)
|
cmd_reindex.set_defaults(func=command_reindex, verbose=True)
|
||||||
|
|
||||||
cmd_setup = cmds.add_parser('setup', help=u'Combine load and reindex')
|
cmd_setup = cmds.add_parser(
|
||||||
|
'setup', help=u'Combine load and reindex',
|
||||||
|
parents=[common_parser])
|
||||||
cmd_setup.set_defaults(func=command_setup, verbose=False)
|
cmd_setup.set_defaults(func=command_setup, verbose=False)
|
||||||
|
|
||||||
cmd_status = cmds.add_parser('status', help=u'Print which engine, index, and csv directory would be used for other commands')
|
cmd_status = cmds.add_parser(
|
||||||
|
'status', help=u'Print which engine, index, and csv directory would be used for other commands',
|
||||||
|
parents=[common_parser])
|
||||||
cmd_status.set_defaults(func=command_status, verbose=True)
|
cmd_status.set_defaults(func=command_status, verbose=True)
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
Loading…
Reference in a new issue