@ -29,6 +29,13 @@ class SortingHelpFormatter(argparse.HelpFormatter):
super ( SortingHelpFormatter , self ) . add_arguments ( actions )
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 ) :
class PrependListAction ( argparse . Action ) :
""" A near clone of ``argparse._AppendAction``, but designed to prepend list values
""" A near clone of ``argparse._AppendAction``, but designed to prepend list values
instead of appending .
instead of appending .
@ -171,12 +178,13 @@ def version(prog=None):
# Functions to add pre-canned options to an OptionParser
# 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
Create an options parser for all ansible scripts
"""
"""
# base opts
# base opts
parser = argparse . ArgumentParser (
parser = argparse . ArgumentParser (
prog = prog ,
formatter_class = SortingHelpFormatter ,
formatter_class = SortingHelpFormatter ,
epilog = epilog ,
epilog = epilog ,
description = desc ,
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, " \
version_help = " show program ' s version number, config file location, configured module search path, " \
" module location, executable location and exit "
" 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 )
add_verbosity_options ( parser )
return parser
return parser