mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 21:32:49 +00:00
Restore ansible --version output (#55728)
* Add custom action class for version info * Use args from CLI as prog for ArgumentParser object * Make prog a required parameter of create_base_parser() and update all uses to pass in the newly required parameter. * Add unit test for checking ansible --version * Update other related unit tests
This commit is contained in:
@@ -61,6 +61,9 @@ class CLI(with_metaclass(ABCMeta, object)):
|
||||
Base init method for all command line programs
|
||||
"""
|
||||
|
||||
if not args:
|
||||
raise ValueError('A non-empty list for args is required')
|
||||
|
||||
self.args = args
|
||||
self.parser = None
|
||||
self.callback = callback
|
||||
@@ -275,7 +278,7 @@ class CLI(with_metaclass(ABCMeta, object)):
|
||||
ansible.arguments.option_helpers.add_runas_options(self.parser)
|
||||
self.parser.add_option('--my-option', dest='my_option', action='store')
|
||||
"""
|
||||
self.parser = opt_help.create_base_parser(usage=usage, desc=desc, epilog=epilog)
|
||||
self.parser = opt_help.create_base_parser(os.path.basename(self.args[0]), usage=usage, desc=desc, epilog=epilog, )
|
||||
|
||||
@abstractmethod
|
||||
def post_process_args(self, options):
|
||||
|
||||
@@ -29,6 +29,13 @@ class SortingHelpFormatter(argparse.HelpFormatter):
|
||||
super(SortingHelpFormatter, self).add_arguments(actions)
|
||||
|
||||
|
||||
class AnsibleVersion(argparse.Action):
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
ansible_version = to_native(version(getattr(parser, 'prog')))
|
||||
print(ansible_version)
|
||||
parser.exit()
|
||||
|
||||
|
||||
class PrependListAction(argparse.Action):
|
||||
"""A near clone of ``argparse._AppendAction``, but designed to prepend list values
|
||||
instead of appending.
|
||||
@@ -171,12 +178,13 @@ def version(prog=None):
|
||||
# Functions to add pre-canned options to an OptionParser
|
||||
#
|
||||
|
||||
def create_base_parser(usage="", desc=None, epilog=None):
|
||||
def create_base_parser(prog, usage="", desc=None, epilog=None):
|
||||
"""
|
||||
Create an options parser for all ansible scripts
|
||||
"""
|
||||
# base opts
|
||||
parser = argparse.ArgumentParser(
|
||||
prog=prog,
|
||||
formatter_class=SortingHelpFormatter,
|
||||
epilog=epilog,
|
||||
description=desc,
|
||||
@@ -184,7 +192,8 @@ def create_base_parser(usage="", desc=None, epilog=None):
|
||||
)
|
||||
version_help = "show program's version number, config file location, configured module search path," \
|
||||
" module location, executable location and exit"
|
||||
parser.add_argument('--version', action='version', version=to_native(version("%(prog)s")), help=version_help)
|
||||
|
||||
parser.add_argument('--version', action=AnsibleVersion, nargs=0, help=version_help)
|
||||
add_verbosity_options(parser)
|
||||
return parser
|
||||
|
||||
|
||||
Reference in New Issue
Block a user