mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-21 20:30:21 -07:00
Split up the base_parser function
The goal of breaking apart the base_parser() function is to get rid of a bunch of conditionals and parameters in the code and, instead, make code look like simple composition. When splitting, a choice had to be made as to whether this would operate by side effect (modifying a passed in parser) or side effect-free (returning a new parser everytime). Making a version that's side-effect-free appears to be fighting with the optparse API (it wants to work by creating a parser object, configuring the object, and then parsing the arguments with it) so instead, make it clear that our helper functions are modifying the passed in parser by (1) not returning the parser and (2) changing the function names to be more clear that it is operating by side-effect. Also move all of the generic optparse code, along with the argument context classes, into a new subdirectory.
This commit is contained in:
parent
afdbb0d9d5
commit
7e92ff823e
21 changed files with 545 additions and 486 deletions
|
@ -15,39 +15,15 @@ running the ansible command line tools.
|
|||
These APIs are still in flux so do not use them unless you are willing to update them with every Ansible release
|
||||
"""
|
||||
|
||||
from ansible import arguments
|
||||
from ansible.arguments.context_objects import CLIArgs, GlobalCLIArgs
|
||||
|
||||
|
||||
# Note: this is not the singleton version. That is only created once the program has actually
|
||||
# parsed the args
|
||||
CLIARGS = arguments.CLIArgs({})
|
||||
|
||||
|
||||
class _Context:
|
||||
"""
|
||||
Not yet ready for Prime Time
|
||||
|
||||
Eventually this may allow for code which needs to run under different contexts (for instance, as
|
||||
if they were run with different command line args or from different current working directories)
|
||||
to exist in the same process. But at the moment, we don't need that so this code has not been
|
||||
tested for suitability.
|
||||
"""
|
||||
def __init__(self):
|
||||
global CLIARGS
|
||||
self._CLIARGS = arguments.CLIArgs(CLIARGS)
|
||||
|
||||
@property
|
||||
def CLIARGS(self):
|
||||
return self._CLIARGS
|
||||
|
||||
@CLIARGS.setter
|
||||
def CLIARGS_set(self, new_cli_args):
|
||||
if not isinstance(new_cli_args, arguments.CLIArgs):
|
||||
raise TypeError('CLIARGS must be of type (ansible.arguments.CLIArgs)')
|
||||
self._CLIARGS = new_cli_args
|
||||
# Note: this is not the singleton version. The Singleton is only created once the program has
|
||||
# actually parsed the args
|
||||
CLIARGS = CLIArgs({})
|
||||
|
||||
|
||||
def _init_global_context(cli_args):
|
||||
"""Initialize the global context objects"""
|
||||
global CLIARGS
|
||||
CLIARGS = arguments.GlobalCLIArgs.from_options(cli_args)
|
||||
CLIARGS = GlobalCLIArgs.from_options(cli_args)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue