Commit ca0032a4 authored by S. Zeid's avatar S. Zeid

[cli/engine/commands/shell] Add --version/-V flag (and CLI.version attribute)

parent 22009e58
...@@ -39,6 +39,7 @@ import types ...@@ -39,6 +39,7 @@ import types
from ..app import App from ..app import App
from ..applist import AppList from ..applist import AppList
from ..container import ContainerRoot from ..container import ContainerRoot
from .. import __version__ as pkg_version
from engine import CLI, CLIError, Command, output, debug from engine import CLI, CLIError, Command, output, debug
...@@ -52,6 +53,7 @@ class CLI(CLI): ...@@ -52,6 +53,7 @@ class CLI(CLI):
description = __doc__ description = __doc__
easter_eggs = True easter_eggs = True
program = "iosapplist" program = "iosapplist"
version = pkg_version
app_class = App app_class = App
app_root = None app_root = None
......
...@@ -78,6 +78,7 @@ def make_CLI_class(): ...@@ -78,6 +78,7 @@ def make_CLI_class():
description = None description = None
easter_eggs = False easter_eggs = False
program = None program = None
version = None
def start(self, argv, default=None.__class__): def start(self, argv, default=None.__class__):
argv = (["shell"] if not self.__started_any else []) + argv argv = (["shell"] if not self.__started_any else []) + argv
......
...@@ -69,6 +69,7 @@ class ShellCommand(Command): ...@@ -69,6 +69,7 @@ class ShellCommand(Command):
usage = "[options [...]] [command [args [...]]]" usage = "[options [...]] [command [args [...]]]"
__is_shell = True __is_shell = True
__want_help = False
__robot_easter_egg_triggers = ("true", "yes", "on", "y", "1") __robot_easter_egg_triggers = ("true", "yes", "on", "y", "1")
__use_real_output_format = True __use_real_output_format = True
...@@ -81,14 +82,14 @@ class ShellCommand(Command): ...@@ -81,14 +82,14 @@ class ShellCommand(Command):
if not self.__is_shell: if not self.__is_shell:
p.usage = self.usage p.usage = self.usage
p.description = cli.description p.description = cli.description
if self.__is_shell: if self.__is_shell or self.__want_help:
p.add_argument("--help", "-h", action="store_true", p.add_argument("--help", "-h", action="store_true",
help='show this help and exit') help='show this help and exit')
if self.easter_eggs: if self.easter_eggs:
p.add_argument("--hep", action="store_true", help=argparse.SUPPRESS) p.add_argument("--hep", action="store_true", help=argparse.SUPPRESS)
p.add_argument("--robot", default="", metavar='<format>', if self.__want_help and not self.__is_shell and getattr(cli, "version", None):
help='Produce output suitable for robots.' p.add_argument("--version", "-V", action="store_true",
' Format should be "plist" or "json".') help="show program's version number and exit")
if self.__is_shell: if self.__is_shell:
p.add_argument("--ps1", default="> ", p.add_argument("--ps1", default="> ",
help='The string to use to prompt for shell input ("> " by' help='The string to use to prompt for shell input ("> " by'
...@@ -97,8 +98,11 @@ class ShellCommand(Command): ...@@ -97,8 +98,11 @@ class ShellCommand(Command):
help='Prompt for and terminate shell input with a null byte' help='Prompt for and terminate shell input with a null byte'
' instead of, respectively, ps1 and a newline. Useful' ' instead of, respectively, ps1 and a newline. Useful'
' in conjunction with --robot.') ' in conjunction with --robot.')
p.add_argument("--robot", default="", metavar='<format>',
help='Produce output suitable for robots.'
' Format should be "plist" or "json".')
p.formatter_class = argparse.RawDescriptionHelpFormatter p.formatter_class = argparse.RawDescriptionHelpFormatter
if not self.__is_shell: if not self.__is_shell and self.__want_help:
p.epilog = "commands" p.epilog = "commands"
if cli.default_command: if cli.default_command:
p.epilog += " (default is `%s`)" % cli.default_command p.epilog += " (default is `%s`)" % cli.default_command
...@@ -169,6 +173,11 @@ class ShellCommand(Command): ...@@ -169,6 +173,11 @@ class ShellCommand(Command):
if help_flag or (help_arg and not self.__is_shell): if help_flag or (help_arg and not self.__is_shell):
yield self.do_help(cli) yield self.do_help(cli)
if not self.__is_shell:
if len(self.extra) and self.extra[0] in ("-V", "--version"):
yield output.normal(self.version_string(cli))
raise StopIteration(0)
build = "" build = ""
real_command = None real_command = None
while True: while True:
...@@ -216,6 +225,10 @@ class ShellCommand(Command): ...@@ -216,6 +225,10 @@ class ShellCommand(Command):
if self.easter_eggs and argv[0] == "hep": if self.easter_eggs and argv[0] == "hep":
argv[0] = "--hep" argv[0] = "--hep"
argv = ["sh"] + argv argv = ["sh"] + argv
if argv[0] == "version":
real_command = False
message = self.version_string(cli)
output.OutputCommand(cli).run([self.argv[0], "0", message])
elif not len(argv) and one_command == False: elif not len(argv) and one_command == False:
continue continue
r = 127 r = 127
...@@ -254,7 +267,7 @@ class ShellCommand(Command): ...@@ -254,7 +267,7 @@ class ShellCommand(Command):
def format_cli_error(self, cli, exc): def format_cli_error(self, cli, exc):
argv0 = self.argv[0] if self.__is_shell else cli.program argv0 = self.argv[0] if self.__is_shell else cli.program
return "%s: error: %s" % (argv0 or self.names[0], str(exc)) return "%s: error: %s" % (argv0 or self.names[0], str(exc))
def do_help(self, cli, for_program=None): def do_help(self, cli, for_program=None):
self.__use_real_output_format = True self.__use_real_output_format = True
try: try:
...@@ -270,6 +283,10 @@ class ShellCommand(Command): ...@@ -270,6 +283,10 @@ class ShellCommand(Command):
cmd = self.__class__(cli) cmd = self.__class__(cli)
cmd.argv = [self.argv[0], "--help"] cmd.argv = [self.argv[0], "--help"]
cmd.__is_shell = not for_program cmd.__is_shell = not for_program
cmd.__want_help = True
for i in cmd._parse_args(cli): for i in cmd._parse_args(cli):
pass pass
return cmd.arg_parser.format_help() return cmd.arg_parser.format_help()
def version_string(self, cli):
return "%s %s" % (str(cli.program), str(cli.version))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment