mirror of
https://gerrit.googlesource.com/git-repo
synced 2024-12-21 07:16:21 +00:00
repo: refactor internal --help/--version parsing
The _ParseArgs method parses the arguments and processes some of the options, with the rest left to the _Run method. Simplify the _ParseArgs method to only parse arguments and have _Run handle all actual processing. This will make it easier to add more terminal options (ones that exit immediately without a subcommand), and makes it easier to understand the overall code flow. Change-Id: I47f7274c3f2b59378fd479e403e70fb24b681536 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/312906 Reviewed-by: Xin Li <delphij@google.com> Tested-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
parent
cfa00d6e3d
commit
968d646f04
42
main.py
42
main.py
@ -130,33 +130,22 @@ class _Repo(object):
|
|||||||
|
|
||||||
def _ParseArgs(self, argv):
|
def _ParseArgs(self, argv):
|
||||||
"""Parse the main `repo` command line options."""
|
"""Parse the main `repo` command line options."""
|
||||||
name = None
|
for i, arg in enumerate(argv):
|
||||||
glob = []
|
if not arg.startswith('-'):
|
||||||
|
name = arg
|
||||||
for i in range(len(argv)):
|
|
||||||
if not argv[i].startswith('-'):
|
|
||||||
name = argv[i]
|
|
||||||
if i > 0:
|
|
||||||
glob = argv[:i]
|
glob = argv[:i]
|
||||||
argv = argv[i + 1:]
|
argv = argv[i + 1:]
|
||||||
break
|
break
|
||||||
if not name:
|
else:
|
||||||
|
name = None
|
||||||
glob = argv
|
glob = argv
|
||||||
name = 'help'
|
|
||||||
argv = []
|
argv = []
|
||||||
gopts, _gargs = global_options.parse_args(glob)
|
gopts, _gargs = global_options.parse_args(glob)
|
||||||
|
|
||||||
|
if name:
|
||||||
name, alias_args = self._ExpandAlias(name)
|
name, alias_args = self._ExpandAlias(name)
|
||||||
argv = alias_args + argv
|
argv = alias_args + argv
|
||||||
|
|
||||||
if gopts.help:
|
|
||||||
global_options.print_help()
|
|
||||||
commands = ' '.join(sorted(self.commands))
|
|
||||||
wrapped_commands = textwrap.wrap(commands, width=77)
|
|
||||||
print('\nAvailable commands:\n %s' % ('\n '.join(wrapped_commands),))
|
|
||||||
print('\nRun `repo help <command>` for command-specific details.')
|
|
||||||
global_options.exit()
|
|
||||||
|
|
||||||
return (name, gopts, argv)
|
return (name, gopts, argv)
|
||||||
|
|
||||||
def _ExpandAlias(self, name):
|
def _ExpandAlias(self, name):
|
||||||
@ -186,12 +175,23 @@ class _Repo(object):
|
|||||||
|
|
||||||
if gopts.trace:
|
if gopts.trace:
|
||||||
SetTrace()
|
SetTrace()
|
||||||
if gopts.show_version:
|
|
||||||
if name == 'help':
|
# Handle options that terminate quickly first.
|
||||||
name = 'version'
|
if gopts.help:
|
||||||
else:
|
global_options.print_help()
|
||||||
|
commands = ' '.join(sorted(self.commands))
|
||||||
|
wrapped_commands = textwrap.wrap(commands, width=77)
|
||||||
|
print('\nAvailable commands:\n %s' % ('\n '.join(wrapped_commands),))
|
||||||
|
print('\nRun `repo help <command>` for command-specific details.')
|
||||||
|
return 0
|
||||||
|
elif gopts.show_version:
|
||||||
|
if name and name != 'help':
|
||||||
print('fatal: invalid usage of --version', file=sys.stderr)
|
print('fatal: invalid usage of --version', file=sys.stderr)
|
||||||
return 1
|
return 1
|
||||||
|
name = 'version'
|
||||||
|
elif not name:
|
||||||
|
# No subcommand specified, so show the help/subcommand.
|
||||||
|
name = 'help'
|
||||||
|
|
||||||
SetDefaultColoring(gopts.color)
|
SetDefaultColoring(gopts.color)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user