1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

go back to using a normal optparser to add options instead of the dict

interface.

add very small subclass of OptionParser to sort the options so mdehaan is happy
This commit is contained in:
Seth Vidal 2012-04-10 13:51:58 -04:00
parent 6749903e57
commit 8ae71cc7b1
3 changed files with 49 additions and 82 deletions

View file

@ -47,23 +47,12 @@ class Cli(object):
def parse(self): def parse(self):
''' create an options parser for bin/ansible ''' ''' create an options parser for bin/ansible '''
parser = utils.base_parser(constants=C, runas_opts=True, async_opts=True,
options = { output_opts=True, usage='ansible <host-pattern> [options]')
'-a' : dict(long='--args', dest='module_args', parser.add_option('-a', '--args', dest='module_args',
help="module arguments", default=C.DEFAULT_MODULE_ARGS), help="module arguments", default=C.DEFAULT_MODULE_ARGS)
'-m' : dict(long='--module-name', dest='module_name', parser.add_option('-m', '--module-name', dest='module_name',
help="module name to execute", default=C.DEFAULT_MODULE_NAME) help="module name to execute", default=C.DEFAULT_MODULE_NAME)
}
parser = utils.make_parser(
options,
usage='ansible <host-pattern> [options]',
runas_opts=True,
async_opts=True,
output_opts=True,
)
options, args = parser.parse_args() options, args = parser.parse_args()
self.callbacks.options = options self.callbacks.options = options

View file

@ -32,14 +32,13 @@ def main(args):
''' run ansible-playbook operations ''' ''' run ansible-playbook operations '''
# create parser for CLI options # create parser for CLI options
usage = "ansible-playbook playbook.yml [options]" usage = "ans-playbook playbook.yml"
options = { parser = utils.base_parser(constants=C, usage=usage)
'-e' : dict(long='--extra-vars', dest='extra_vars', parser.add_option('-e', '--extra-vars', dest='extra_vars',
help='pass in extra key=value variables from outside the playbook'), help='arguments to pass to the inventory script')
'-O' : dict(long='--override-hosts', dest="override_hosts", default=None, parser.add_option('-O', '--override-hosts', dest="override_hosts", default=None,
help="run playbook against only hosts, ignorning the inventory file") help="run playbook against these hosts regardless of inventory settings")
}
parser = utils.make_parser(options, constants=C, usage=usage)
options, args = parser.parse_args(args) options, args = parser.parse_args(args)
if len(args) == 0: if len(args) == 0:

View file

@ -24,7 +24,7 @@ import re
import jinja2 import jinja2
import yaml import yaml
import optparse import optparse
from operator import methodcaller
try: try:
import json import json
except ImportError: except ImportError:
@ -273,70 +273,49 @@ def parse_kv(args):
options[k]=v options[k]=v
return options return options
def make_parser(add_options, constants=C, usage="", output_opts=False, runas_opts=False, async_opts=False): class SortedOptParser(optparse.OptionParser):
''' create an options parser w/ common options for any ansible program ''' '''Optparser which sorts the options by opt before outputting --help'''
def format_help(self, formatter=None):
self.option_list.sort(key=methodcaller('get_opt_string'))
return optparse.OptionParser.format_help(self, formatter=None)
options = base_parser_options( def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, async_opts=False):
constants=constants, ''' create an options parser for any ansible script '''
output_opts=output_opts,
runas_opts=runas_opts,
async_opts=async_opts
)
options.update(add_options)
parser = optparse.OptionParser() parser = SortedOptParser(usage)
names = sorted(options.keys()) parser.add_option('-D','--debug', default=False, action="store_true",
for n in names: help='enable standard error debugging of modules.')
data = options[n].copy() parser.add_option('-f','--forks', dest='forks', default=constants.DEFAULT_FORKS, type='int',
long = data['long'] help='number of parallel processes to use')
del data['long'] parser.add_option('-i', '--inventory-file', dest='inventory',
parser.add_option(n, long, **data) help='inventory host file', default=constants.DEFAULT_HOST_LIST)
return parser parser.add_option('-k', '--ask-pass', default=False, action='store_true',
help='ask for SSH password')
def base_parser_options(constants=C, output_opts=False, runas_opts=False, async_opts=False): parser.add_option('-M', '--module-path', dest='module_path',
''' creates common options for ansible programs ''' help="path to module library", default=constants.DEFAULT_MODULE_PATH)
parser.add_option('-T', '--timeout', default=constants.DEFAULT_TIMEOUT, type='int',
options = { dest='timeout', help='set the SSH timeout in seconds')
'-D': dict(long='--debug', default=False, action="store_true", parser.add_option('-p', '--port', default=constants.DEFAULT_REMOTE_PORT, type='int',
help='show debug/verbose module output'), dest='remote_port', help='set the remote ssh port')
'-f': dict(long='--forks', dest='forks', default=constants.DEFAULT_FORKS, type='int',
help='number of parallel processes to use'),
'-i': dict(long='--inventory-file', dest='inventory',
help='path to inventory host file', default=constants.DEFAULT_HOST_LIST),
'-k': dict(long='--ask-pass', default=False, action='store_true',
help='ask for SSH password'),
'-M': dict(long='--module-path', dest='module_path',
help="path to module library directory", default=constants.DEFAULT_MODULE_PATH),
'-T': dict(long='--timeout', default=constants.DEFAULT_TIMEOUT, type='int',
dest='timeout', help='set the SSH connection timeout in seconds'),
'-p': dict(long='--port', default=constants.DEFAULT_REMOTE_PORT, type='int',
dest='remote_port', help='use this remote SSH port'),
}
if output_opts: if output_opts:
options.update({ parser.add_option('-o', '--one-line', dest='one_line', action='store_true',
'-o' : dict(long='--one-line', dest='one_line', action='store_true', help='condense output')
help='condense output'), parser.add_option('-t', '--tree', dest='tree', default=None,
'-t' : dict(long='--tree', dest='tree', default=None, help='log output to this directory')
help='log results to this directory')
})
if runas_opts: if runas_opts:
options.update({ parser.add_option("-s", "--sudo", default=False, action="store_true",
'-s' : dict(long="--sudo", default=False, action="store_true", dest='sudo', help="run operations with sudo (nopasswd)")
dest='sudo', help="run operations with sudo (nopasswd)"), parser.add_option('-u', '--user', default=constants.DEFAULT_REMOTE_USER,
'-u' : dict(long='--user', default=constants.DEFAULT_REMOTE_USER, dest='remote_user', help='connect as this user')
dest='remote_user', help='connect as this user'),
})
if async_opts: if async_opts:
options.update({ parser.add_option('-P', '--poll', default=constants.DEFAULT_POLL_INTERVAL, type='int',
'-P' : dict(long='--poll', default=constants.DEFAULT_POLL_INTERVAL, type='int', dest='poll_interval', help='set the poll interval if using -B')
dest='poll_interval', help='set the poll interval if using -B'), parser.add_option('-B', '--background', dest='seconds', type='int', default=0,
'-B' : dict(long='--background', dest='seconds', type='int', default=0, help='run asynchronously, failing after X seconds')
help='run asynchronously, failing after X seconds'),
})
return options return parser